Jump to content

tweening to set positions


temporalix
 Share

Recommended Posts

 Hello. I've been trying to setup some way to tween my sprite to 3 set areas on the screen, but it ends up going from 1 side to the other.
 
left = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);                left.onDown.add(function () {                    pos > -2 ? pos-- : pos;                    if (pos == 1) {                        game.add.tween(ufo).to({ x: 400 }, 1000, Phaser.Easing.Linear.None, true, 0);                    }                    else {                        game.add.tween(ufo).to({ x: 250 }, 1000, Phaser.Easing.Linear.None, true, 0);                    }                }, this);right = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);                right.onUp.add(function () {                    pos<2?pos++:pos;                    if(pos==-1)                    {                        game.add.tween(ufo).to({ x: 400 }, 1000, Phaser.Easing.Linear.None, true, 0);                    }                    else                    {                        game.add.tween(ufo).to({ x: 950 }, 1000, Phaser.Easing.Linear.None, true, 0);                    }                }, this);
 
what am I doing wrong?
 
 

 

 

Link to comment
Share on other sites

I got it working after messing around with it a bit , and seeing that the logic inside the button press loops was interfering with the tweens.  I ended up moving all the logic into a seperate function which I think makes is cleaner .

 

code:

function getPos(dir) {                                        if (dir == 0) {                        if (moveX > 100)                            moveX = moveX - 500;                        spinSpeed = spinSpeed -5;                    }                    else {                                                if (moveX < 1000)                            moveX = moveX + 500;                        spinSpeed = spinSpeed + 5;                    }                }                left = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);                left.onDown.add(function () { getPos(0) }, this);                left.onUp.add(function () {                    leftTween = game.add.tween(ufo).to({ x: moveX }, 1000).start();                                    }, this);                right = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);                right.onDown.add(function () { getPos(1); }, this);                right.onUp.add(function () {                    rightTween = game.add.tween(ufo).to({ x: moveX }, 1000).start();                                    }, this);
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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