Jump to content

turning a sprite multiple time


nazimboudeffa
 Share

Recommended Posts

i want to turn a sprite with keyboard hotkey every time i tape SPACEBAR,  how is it possible

the game is here http://asciimulation.link/codeinmass/mod/

and, this is a continuation of this thread

	turn90: function(){
			var a = Math.floor(this.player.x / gameOptions.tileSize);
			var b = Math.floor(this.player.y / gameOptions.tileSize);
			console.log(a, b);

			game.add.tween(this.tilesArray[b][a]).to( { angle: 90 }, 200, Phaser.Easing.Linear.None, true);
	}

 

Link to comment
Share on other sites

Turn in direction or just flip the graphic?
I won't use a tween to do this. If you want to animate it, it would be "a good way" to make some graphics, an animation and do an "animation.onComplete.add()" inside the following examples:

you could make a method for flip the graphic:

flipSprite () {

  this.scale.x >= 0 ? this.scale.x = -1 : this.scale = 1;

}

for turn the direction, I use a method like this:

MySprite {

  constructor() {
    ..some code..

    game.physics.arcade.enable(this);
    this.body.allowGravity = true;
    this.body.allowRotation = false;
    this.data.speed = 70;
    this.data.direction = 'right';
    this.body.velocity.x = this.data.speed;
  }


  turnSprite() {
    this.body.velocity.x = this.body.velocity.x > 0 ? this.body.velocity.x = -this.data.speed : this.data.speed;
  }

}

I hope that helps,

regards :)

Link to comment
Share on other sites

Worked for me. The only problem I saw is that you can only change the angle on the tile once (because it will always go to 90). I think this is your intention: 

var newAngle = this.tilesArray[b][a].angle + 90; 
            turnTween = game.add.tween(this.tilesArray[b][a]).to( {
                angle: newAngle
            }, 200, Phaser.Easing.Linear.None);

            if (!turnTween.isRunning)
            {
                    turnTween.start();
            }

That way you can change the angle on the tile underneath the circle as many times as you want. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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