Jump to content

Handling multiple tweens with loop


abs1234
 Share

Recommended Posts

Okay i have started phaser a month ago.I am stuck here:

 

Aim: i am creating an a tween like this..

red1 is my sprite
function reddown(){

demoTween1 = game.add.tween(red1).to({x:420,y:250},1800);
    demoTween1.onComplete.add(function(){
      
 
        if (red1.x=420) {
            score=score+2;
             red1.x=0
               } 
 
 
                else{
                red1.x=0;
                    alert("game over");
 
         }
                
                    
              
        });
demoTween1.start();
 
}}
 
 
Which works perfectly when its called. But i want my sprite to move again with a loop and show me moving like the first one
 
var x=1;
while(x<4){
 
if (x==1) {
reddown();
};
if (x==2) {
reddown();
};
 
 
 
x++;
}
 
this does not work and condition is not satisfied in reddown() it executes game over 3 times.
Would be very grateful if someone could help.!
 
 
 

 

 

Link to comment
Share on other sites

hi note, in the forum post editor you can use the < > icon to post code. 

function reddown(){    demoTween1 = game.add.tween(red1).to({x:420,y:250},1800);    demoTween1.onComplete.add(function(){              if (red1.x==420) {            score=score+2;            red1.x=0        }         else {            red1.x=0;            alert("game over");         }    });    demoTween1.start();}}

your first error there is that it should be 

if (red1.x==420) {

although i assume that it was a typo, otherwise I don't see how the alert would ever execute

 

if you write if(red1.x=420), it's essentially writing the same as 

red1.x=420; if(red1.x==420) {

in your case however it'll be 420 at the end of the tween anyway so it's a redundant condition.

 

 

secondly, you don't say what this is doing..

var x=1;while(x<4){

but that loop you have with the if statements would just call reddown() immediately twice. javascript "for" loops aren't tied to any animation.. what you've essentially done with that loop is written the equivalent of

reddown() // x=1reddown() // x=2

with the third time (x=3) not doing anything

but I don't get the alert happening at all with your code, so you must not have posted your code correctly, or you've left something else out

 

please can you elaborate?

J

 

 

 

 

Link to comment
Share on other sites

this is what i *think* you're trying to do. I could be wrong

 

it's TypeScript sorry but it should be obvious enough

score=0loopCount = 0numRepeats = 3 /*function*/ reddown() {    var demoTween1 = this.game.add.tween(this.hero).to({x:420,y:250},1800, null, true, 0);    demoTween1.onComplete.add(this.tweenComplete.bind(this));    demoTween1.start();}// when the tween is complete (ie the hero reaches 420)/* function */ tweenComplete() {    // add 2 to the score     this.score=this.score+2;    // reset his position to 0    this.hero.x=0    // add one to the loop counter    this.loopCount++   // if we've done this 3 times   if(this.loopCount==3) {       // it's game over       alert("game over!")    } else {       // loop again       this.reddown();    }}/* function */ onClick() {    this.reddown()}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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