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

2013/2/21

Shot-Bar

I am trying to make a game where you play as a penguin and have to gather strawberries. i want an attack where you fire out snowballs in four directions. can anyone give me the codes. i'm stuck on the actual getting it to fire part. HEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Jonas234 Jonas234

2013/2/21

#
Something like this should be working
if (Greenfoot.isKeyDown("space"))
        {
            int rot =  penguin.getRotation();
            int x = penguin.getX();
            int y = penguin.getY();
            for (int i = 0; i<4;i++)
            {
                Snowball snow = new Snowball();
                getWorld().addObject(snow,x,y);
                snow.setRotation(rot+90*i);
                snow.move(3);
            } 
        }
You just need to get your penguin object into the method. Jonas
danpost danpost

2013/2/21

#
The 'isKeyDown' method should not be used for instance actions unless you use it with another condition, like a boolean to track the state of the trigger key (the 'isKeyDown' method is used for continuous actions when not used in conjuction with another condition). The code as given above will cause snowballs to be thrown in all four directions continuously until the key is released. The method normally used for instance actions is the 'getKey' method. If firing in all directions at once, the penguins rotation is not needed. Using the 'getKey' method, which triggers on the release of the key:
if ("space".equals(Greenfoot.getKey())
{
    for (int i=0; i<4; i++)
    {
        Snowball snow = new Snowball();
        getWorld().addObject(snow, getX(), getY());
        snow.setRotation(90*i);
        snow.move(3);
    }
}
If you want to use the 'isKeyDown' method to trigger on the press of the key:
// add this instance field to the class
private boolean spaceDown;
// then the code would be
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    for (int i=0; i<4; i++)
    {
        Snowball snow = new Snowball();
        getWorld().addObject(snow, getX(), getY());
        snow.setRotation(90*i);
        snow.move(3):
    }
    spaceDown = true;
}
if (spaceDown && !Greenfoot.isKeyDown("space"))
{
    spaceDown = false;
}
If the 'getKey' method is to check for more than one key, then the value of what 'getKey' returns must be placed in a field before any checking of its value, as any secondary calls to 'getKey' will return 'null':
String key = Greenfoot.getKey(); // saving the value returned
if ("space".equals(key)) {  /** code */ }
if ("enter".equals(key)) { /** code */ }
// etc.
thanks guys all sorted now i just need to make them vanish at the edge of the screen
You need to login to post a reply.