Jump to content

Update function isnt working well with '.contains'


beuleal
 Share

Recommended Posts

Hi guys, its my 2nd time developing games with phaser, and i like so much!

 

Ive this in my code:

function update () {        if(circle_base.contains(player.disco.x, player.disco.y) && player.possuiBatata == true)    {        player.possuiBatata = false;        delivered();             }    //more code...}

The problem is: after i enter in the circle, this condition is always true until i get out, so... it doest work how i would like to work :( I meam, "player.possuiBatata" will be true just when i be out of the circle

 

What should i do?

Link to comment
Share on other sites

Maybe you should do this.

function update () {        if(!circle_base.contains(player.disco.x, player.disco.y) && player.possuiBatata !== true)    {        player.possuiBatata = true;                   }else{        player.possuiBatata = false;        delivered();    }    //more code...}

Or maybe something slightly similar depending on what is inside the delivered function, or other code.

Link to comment
Share on other sites

Maybe you should do this.

function update () {        if(!circle_base.contains(player.disco.x, player.disco.y) && player.possuiBatata !== true)    {        player.possuiBatata = true;                   }else{        player.possuiBatata = false;        delivered();    }    //more code...}

Or maybe something slightly similar depending on what is inside the delivered function, or other code.

 

1st, tks for ur answer!

 

delivered function just show some text for the player, and the problem is that i can have "player.possuiBatata = true;" cuz the player is comming to the circle, but he isnt completely inside of the circle.

Link to comment
Share on other sites

No, i want it 'possuiBatata=true' just when the sprite is inside of the other.

 

Like:

 

   Box

[=======]

[               ]

[=======] 

 

Box Contains 'A', so possuiBatata need to be true. 'A' is inside of the box

[=======]

[      A      ]

[=======] 

Link to comment
Share on other sites

This is the way I would do it.

function update () {        player.possuiBatata = false;    if(circle_base.contains(player.disco.x, player.disco.y))    {        player.possuiBatata = true;        delivered();             }    //more code...}

Are you sure you are using the correct x, y properties, and all your objects are created correctly?

 

Also you may need to set your player anchor to 0.5 to get better results.

 

Use player.anchor.set(0.5,0.5).

 

Make your circle smaller if you have to. Don't worry about the symantics. If your player is half out the circle, and possuiBatata is true then make your circle small enough to set possuiBatata true in an imaginary circle larger than the real circle.

 

If not try:

function update () {    player.disco.x = player.x;    player.disco.y = player.y;    player.possuiBatata = false;    if(circle_base.contains(player.disco.x, player.disco.y))    {        player.possuiBatata = true;        delivered();             }    //more code...}

If not just use player.x, and player.y.

Link to comment
Share on other sites

Hm.. I did it!

 

I think that phaser considers the archor... while my "player.disco" was inside the circle, the function 'delivered' was called, but in a short space of time, and then it stoped how it should be.

 

I dont know how phaser really works, but i think that update function creates forks to increase the perception, so while one fork is processing "if...", the other fork is starting to process.... I cant be more fast than i am setting 'player.possuibatata = true' What u think about? 

Link to comment
Share on other sites

Yes, and no. You could consider requestAnimationFrame as being a new job so it kind of is a fork, but not literally a fork because it's not executed simultaneously.

 

To slow down execution so things are not set too soon use a setTimeout. That might be too slow though. Any code already executing will be done before the setTimeout callback ever gets called even if you have 1 millisecond time set for it. Depends on your situation.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...