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

2015/3/27

Need help with this shoot code!

jocallaghan97 jocallaghan97

2015/3/27

#
code is for a shoot method, which shoots on left click, error terminal keeps coming up at random times. can anyone help?
public void shoot ()
    {
        if( shotTimer >0)
        {
            shotTimer = shotTimer -1;}
        else if( Greenfoot.getMouseInfo().getButton() == 1)
        {
            getWorld().addObject(new bullet(playerRotation), getX(), getY());
            shotTimer = 15;
            Greenfoot.playSound("bolt.wav");
        }
    }
Super_Hippo Super_Hippo

2015/3/27

#
It would be useful if you would say what error it is. I would guess that an error could occur either in line 6 if 'getMouseInfo' returns null (like the mouse is outside the world) or in line 8 if the object isn't in the world. If it is line 6 which produces the error, you could change the line to this:
else if (Greenfoot.mousePressed(null))
fejfo fejfo

2015/3/27

#
It could be to constuctor of bullet that produces an error to (or the bullet it self)
danpost danpost

2015/3/27

#
If it must be the left mouse button for the trigger, then use:
else if (Greenfoot.mousePressed(null) && Greenfoot.getMouseInfo().getButton() == 1)
jocallaghan97 jocallaghan97

2015/3/28

#
all working now, thanks
You need to login to post a reply.