Jump to content

How to block a function (i.e. don't return) until Tween finishes


TheIceMage
 Share

Recommended Posts

I'm making a 2D tile based game in Dart with the port of Phaser. My Character class has a moveTo function which does a number of things including making Tween calls to actually move the Sprite. For certain reasons concerning sprites (of enemies) that will be moving without player input, I want my moveTo function to only return when the tween it does is complete. I tried what I imagine is a naive approach of having a finished var and putting this line in my moveTo function

//the stuff inside add call is Dart function shorthandtween.onComplete.add((_) => finished = 1);

and then having the function end with a while loop

while(finished != 1);

but this seems to just get the game stuck without even moving the sprite once I try to move a player for example.

So how do I actually achieve what I want?

Link to comment
Share on other sites

Yes, a lot of the Phaser callback based functions have the next parameter to set the context.  Pretty much why I get confused over scope as Phaser tends to hide all of that from me!  

 

I can't help with dart I'm afraid.  Can you not mix pure js with it?  I can't think how else you could achieve your aim without it.  I suppose a fudge would be to have a timer as long as the tween  and have that trigger the return.  Could get a bit awkward though if the length of time/tween is dynamic though.

Link to comment
Share on other sites

It's not this 'this' that delays the return, 'this' just keeps the scope of whatever you return to the same as the function in which the tween is called. The anonymous function within the onComplete callback isn't called until the onComplete fires which the does the return - which is scoped to the main function by the 2nd parameter.

Link to comment
Share on other sites

I've never used Dart, so maybe it's fundamentally different than JS... but I don't think it is.

 

JavaScript's execution model is single-threaded, based on an event loop, and its functions have run-to-completion semantics. The upshot is that "while(true);" at the end of your function will prevent anything else from running... including the Phaser game loop. Chances are the browser will shut your script down after a little bit as well. The Mozilla Developer network has a great write-up of all of that. So that's not what you want.

 

You need to write your code so that it's event-based rather than multi-threaded. When the interpreter is running your function *nothing else is running*. That means that the update methods of your player and your enemy need to be written as though they're going to be run many times before your player's tweens stop running.

 

It *sounds* like you don't want your enemies to move until the player is done moving. Is that right? So the callback in the tween.onComplete signal needs to set a variable somewhere that says it's done... and you need an if statement, maybe-probably in your enemies' update methods, that checks that variable to see if the player is done moving yet and, if so, do something.

 

You'll probably end up making some third object somewhere that coordinates the turns of the player and the enemies to make sure they run in the same order every time.

 

EDIT: The equivalent of a "Future" in Dart is the JS "Promise". Support coming to a browser near you soon.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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