Need help with sprites

Hello guys

I’m a begginer to Phaser and game dev in general. I am working on a project which is a sea warfare game. We start pretty basic, two boats in the sea. Here comes the challenge.

I have a boat sprite (png) and a cannon sprite (another png). The boat is physics enabled (matter). What I need to acomplish is having the cannon sprite on top of the boat (as if it were the boat’s cannon) but it has to move independently with a different set of keys (the movement will be restricted to just rotating 360 degrees for aiming). Apart from this independent rotation, the cannon has to be on top of the boat all the time, as part of it.

can this be done? and if so… how?

Thanks a lot in advance!

Hi @Tico1711
It would be something like this:

function update(){
    cannon.x = boat.x + offsetX;
    cannon.y = boat.y + offsetY;

    if(yourKey.isDown){
        cannon.angle++;
    }
    // More code ...
}

I hope it helps you.
Regards.

2 Likes

Nice! I can clearly understand this code and it works like a charm!
Another question if I may:

  1. How can I fix the angle of the cannon so it is always pointing forward unless moved by the key?

cannon.setAngle(deg) or cannon.setRotation(rad) if you prefer working in radians.

1 Like

Your game sounds cool!

Be sure to post a link back to it whenever you have completed it. I’d love to check it out.

1 Like