Comments (14)
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.
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.
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.
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.
No problem! Good luck on your project.
from play_phaser.
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.
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.
I want the enemy to finish moving completely and then go to the next.
from play_phaser.
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.
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.
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.
Oooh, thanks. Hopefully that'll work out. :)
from play_phaser.
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.
No problem! :)
from play_phaser.
Related Issues (20)
- ScaleManager depends on "if (Null)" for isFullScreen
- Unrecognized dart2js option "analyzeAll" HOT 2
- dart2js Uncaught NoSuchMethodError : method not found: 'Symbol("transitionStep")' HOT 2
- Can't use State subclass in different file HOT 4
- Issue #16 still present in 0.12.2 (Unrecognized dart2js option "analyzeAll") HOT 2
- Input events broken for Text objects
- physics.enable on Sprite with child/Group give RangeError
- ninja AABB vs AABB collision issue
- Transparency "true", doesn't have effect. HOT 4
- Question - Unable to get simple code akin to image loading example working. HOT 2
- Question - How to pass values into init function of a state? HOT 1
- Will play_phaser be rewritten for the upcoming Phaser 3? HOT 1
- Ninja AABB vs full tile
- Setting the world scale breaks arcade physics HOT 1
- Question - Getting error when trying to return from a function (via a Completer) once a sound stops. Why? HOT 4
- Canvas not being put into page when running compiled .dart.js file
- Delay before new animation plays
- dart:mirrors - is it really needed? HOT 1
- Upadate to Phaser 2.6.2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from play_phaser.