This site requires JavaScript, please enable it in your browser!
Greenfoot back
sidetracked
sidetracked wrote ...

2016/3/22

Need help with getRotation();

sidetracked sidetracked

2016/3/22

#
I want the bullets to fire from the angle that my character is facing, I was told this is the best way but it just doesn't work. Any ideas?
import greenfoot.*;

public class PlayerOne extends Actor
{
    public int speed = 3;

    public void act() 
    {
        checkKeys();
    }    

    public void fireGun()
    {
        World myWorld = getWorld();
        Bullet bullet = new Bullet();
        bullet.setRotation(getRotation());
        getWorld().addObject(new Bullet(),getX(), getY());
    }

    private void checkKeys()
    {

        if (Greenfoot.isKeyDown("space"))
        {   
            fireGun();
        }
    }
}
Super_Hippo Super_Hippo

2016/3/22

#
Change the 'new Bullet()' in line 17 to 'bullet' to add the bullet with the correct rotation instead of a new one.
danpost danpost

2016/3/22

#
It is not working because you are not setting the rotation of the bullet you are adding into the world. You create one bullet at line 15 and set its rotation in line 16. Then you create a new (different) bullet and add it into the world on line 17. Change line 17 to:
getWorld().addObject(bullet, getX(), getY());
to add the rotated bullet into the world.
sidetracked sidetracked

2016/3/22

#
Hmm it's still only shooting to the right with the change
Super_Hippo Super_Hippo

2016/3/22

#
Maybe because the PlayerOne object is facing to the right as well. You never change the direction.
sidetracked sidetracked

2016/3/22

#
I see, let me make some changes thanks
sidetracked sidetracked

2016/3/22

#
.
sidetracked sidetracked

2016/3/22

#
I added
public int playerRotation;   

 public void checkKeys()
    {

        if (Greenfoot.isKeyDown("space"))
        {   
            fireGun();
        }

        if (Greenfoot.isKeyDown("a"))
        {
            int playerRotation = 180;
            setLocation(getX()-speed,getY());
        }

        if (Greenfoot.isKeyDown("d"))
        {
            int playerRotation = 0;
            setLocation(getX()+speed,getY());
        }

        if (Greenfoot.isKeyDown("s"))
        {
            int playerRotation = 90;
            setLocation(getX(),getY()+speed);
        }

        if (Greenfoot.isKeyDown("w"))
        {
            int playerRotation = 270;
            setLocation(getX(),getY()-speed);
        }
    }
and changed
bullet.setRotation(getRotation());
to
bullet.setRotation(playerRotation);
but it still just fires only to the right :(
Super_Hippo Super_Hippo

2016/3/22

#
Remove all 'int' inside the method.
sidetracked sidetracked

2016/3/22

#
Ah silly me, working perfectly! Thanks everyone.
remy88 remy88

2016/3/22

#
In the method fireGun() you are adding a new Bullet object instead of the bullet you already created. Change it to:
getWorld().addObject(bullet,getX(), getY());
You need to login to post a reply.