Coder Social home page Coder Social logo

Comments (14)

derrick56007 avatar derrick56007 commented on May 4, 2024

Not entirely sure what you implied by "I want my moveTo function to only return when the tween it does is complete" but you could just call a function on the onComplete signal.

create() {
  image = game.add.sprite(game.world.centerX, game.world.centerY, 'car');
  image.anchor.set(0.5);

  Tween tween = game.add
      .tween(image)
      .to({'x': 300,}, 500, Easing.Linear.None, false, 0, 0, false)
      .yoyo(true)
      .repeat(1)
      .start();

  tween.onComplete.add((_) {
    functionToCall('args');
  });
}

void functionToCall(args) {
  //move player
}

from play_phaser.

TheIceMageX50 avatar TheIceMageX50 commented on May 4, 2024

Perhaps seeing the code would help? I dunno
It's here

The return type is void but it still "returns" in the sense that it goes back to whatever called it. What I was saying is I want it to return only once the tween is finished, i.e. if it was main() that called moveTo() then when the code is running main() should call moveTo() and it should not get back to main until the tween is all done. Does that make it any clearer?

from play_phaser.

derrick56007 avatar derrick56007 commented on May 4, 2024

I think I understand now. This can be done using Futures.

Future<String> waitForTween(Tween tween, returnValue) async{
  Completer completer = new Completer();
  tween..start()..onComplete.add((_){
    completer.complete(returnValue);
  });

  return completer.future;
}

create() async {

  sprite = game.add.sprite(100, 100, 'diamond');

  Tween tween = game.add.tween(sprite).to({
      'x': 300,
  }, 500, Easing.Linear.None, false, 0, 0, false)
  .yoyo(true)
  .repeat(1);

  String returnValue = await waitForTween(tween, 'returned');

  print(returnValue);
  print('continuing on');
}

In this case the function "waitForTween" would be your moveTo function.
Make sure to import dart:async

from play_phaser.

TheIceMageX50 avatar TheIceMageX50 commented on May 4, 2024

Oh yes, thanks! I'll try this then. I actually have used Futures in one or two parts of this project already but I guess having used it so infrequently I didn't come up with a solution like this...I tried messing around earlier with Futures but I guess I never thought to try something with a Completer.

from play_phaser.

derrick56007 avatar derrick56007 commented on May 4, 2024

No problem! Good luck on your project.

from play_phaser.

TheIceMageX50 avatar TheIceMageX50 commented on May 4, 2024

Hmm. I think I am going in a good direction with this but now I have a follow up question if you don't mind since this doesn't seem to have entirely solved my "issue". To elaborate, why I wanted to do this in the first place is for making enemy characters move, and only move one at a time so that the second waits for the first to finish moving and so on.

Now, having implemented code like what you showed above they do seem to wait but only until the first "step" (this is a tile-based game) of the move. Is it something to do with how I'm making multiple tween.to() calls, set up via a for loop, and for some reason that makes the onComplete fire when the result of the first tween.to() is done? If so, what would I need to do to actually make it fire just when all those tweens are done?

from play_phaser.

derrick56007 avatar derrick56007 commented on May 4, 2024

Do you want for the enemy to finish their movement completely, and then go on to the next? Or each enemy takes a step first?

from play_phaser.

TheIceMageX50 avatar TheIceMageX50 commented on May 4, 2024

I want the enemy to finish moving completely and then go to the next.

from play_phaser.

derrick56007 avatar derrick56007 commented on May 4, 2024

It seems like there is no such thing as an onComplete for a chain of tweens. You are either going to have to manage tween completions yourself or request this feature.

from play_phaser.

TheIceMageX50 avatar TheIceMageX50 commented on May 4, 2024

That's unfortunate...how would I go about managing Tween completions?
Or alternatively would there be any other way to (even if somewhat hackily) achieve what I want?

from play_phaser.

derrick56007 avatar derrick56007 commented on May 4, 2024

Not at my computer right now, but you could make all of the tweens separate and store them in a List. Start the first one and when it finishes call the next in that list and make it so that it returns on the last tween onComplete.

from play_phaser.

TheIceMageX50 avatar TheIceMageX50 commented on May 4, 2024

Oooh, thanks. Hopefully that'll work out. :)

from play_phaser.

TheIceMageX50 avatar TheIceMageX50 commented on May 4, 2024

Well yea, that approach seems to have worked perfectly.
The reformed code can be seen over here.
Once again, thanks for the help! 😄

from play_phaser.

derrick56007 avatar derrick56007 commented on May 4, 2024

No problem! :)

from play_phaser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.