Jump to content

sprite.rotation for shooting a bullet?


woratana
 Share

Recommended Posts

Hello,

 

I was studying from code in 'Tanks' example game that came with Phaser package in github. I was looking for line of code that set speed of enemy's bullet and how it shoot to player. I found this very line:

bullet.rotation = this.game.physics.moveToObject(bullet, this.player, 500);

It comes out that this line makes the bullet shoot to player. In my sense, bullet.rotation should only change the angle of the bullet, but didn't apply any velocity for it to move.

 

Could you explain how does this work?

 

p.s. Sorry for dumb question here. I am just trying to understand the code.

Link to comment
Share on other sites

It doesn't. Here this should help clarify it:

this.game.physics.moveToObject(bullet, this.player, 500);

The above is what makes the bullet move. The moveToObject call sets internal values on the Sprite.body object to get it moving in the right direction.

 

As a by-product of working out where to move to, it knows the angle the bullet ought to face, so it returns it. You don't have to use it though, the above line of code will work just fine on its own.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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