Jump to content

Unable to make a Sprite fly without any angle after changing its velocity


Simit
 Share

Recommended Posts

Hi Experts,

 

I am a newbie in phaser and this is my first post on this site so please excuse in case I miss any mandatory information. Was just playing around with the Flappy Bird Code available at: http://blog.lessmilk.com/how-to-make-flappy-bird-in-html5-1/

 

I went through the article and started tweaking the code for better understanding but got stuck mid way.

 

I wanted the bird to fly in air (No Gravity) and change it's direction either ways (Up or Down) when I hit the Up/Down keys and then continue in the same lane. performed the following steps:

 

Removed Gravity by changing it's value to 0

this.bird.body.gravity.y = -350;

this.bird.body.gravity.y = 0; 

 

Replaced the 'Spacebar' key with 'Up/Down' keys, removed angles and anchors.

//var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

var UPKey = this.game.input.keyboard.addKey(Phaser.Keyboard.UP);

UPKey.onDown.add(this.jumpUp, this);

 

and similarly for the Down Key. So, now when the Up key is hit, the bird would fly upwards and when the Down key is hit, the bird will fly downwards.

 

    jumpUp: function() {
        // If the bird is dead, he can't jump
        if (this.bird.alive == false)
            return; 
 
        this.bird.body.velocity.y = -100;
        this.jumpSound.play();
    }

 

 

But, the problem is that when I hit the Up/Down key, the bird continues to fly in the respective direction and then flies out of the Game. Whereas I want that the bird should go only up to a certain distance in the upward/downward direction and then continue to move straight (Not diagonal)

 

Let's say the bird started at (0,0), then it should move to 1,1 and then continue in the same direction (1,1 | 2,1 | 3,1 | 4,1 and so on) and Not like 2,2 | 3,3 etc.

 

I tried the following but to no avail:

sprite.body.moves = false

this.bird.body.velocity.y = 0;

 

Please refer to the below snapshot and let me know how can this be achieved.

 

Appreciate your support!

 

Thanks

Simit

post-11863-0-24755500-1417128458.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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