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

2015/2/26

shooting help!

Moho Moho

2015/2/26

#
hello i am making a game and i added shooting but i am stuck on 2 things first off i want so i cant hold down space and it constantly shoots and i also want it where it shoots in the direction i am looking my code rn looks like this
public void attack()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            Attack1 attack1 = new Attack1();
            getWorld().addObject(attack1, getX(), getY());
            
        }
        
        
        
    }
danpost danpost

2015/2/26

#
You can set the rotation of the 'attack1' object at line 7 above. To limit the shooting to once per press of the "space" key, you will need a boolean field to track the state of the key. Basically, you use it to compare the previous state with the new state and anytime they differ, the state of the key has changed and you can shoot if the new key state is down and, regardless of new state, save the new state in the field to be the previous state on the next act cycle.
Moho Moho

2015/2/26

#
how would that look like ? and last question my second attack is supposed to shoot bullets all around you but doesnt travel nearly as far as the first attack how would i do that ? thanks dawg
danpost danpost

2015/2/26

#
Line 7 would start off like:
attack1.setRotation(... 
For the trigger key, it would be something like:
// add the instance field
private boolean spaceDown; // tracks the state of the "space" key

// in act
if (spaceDown != Greenfoot.isKeyDown("space")) // is there a change in the state of the "space" key
{
    spaceDown = ! spaceDown; // save new state
    if (spaceDown) // if new state is down
    {  // etc. 
danpost danpost

2015/2/26

#
As far as the distance your bullet travels, would need to see the code for the class of the bullet.
You need to login to post a reply.