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

2017/1/20

Shooting

Hondrok Hondrok

2017/1/20

#
When the space key and the left key are pressed simultaneously I want the actor to shoot Sphere_left, here's what I've got so far
if("space".equals(Greenfoot.getKey()) && Greenfoot.isKeyDown("left"))
        {
            getWorld().addObject(new Sphere_left(), getX(), getY());
        }
danpost danpost

2017/1/20

#
What you have should not compile. You have an extra closing parenthesis after 'getKey()'.
Hondrok Hondrok

2017/1/20

#
danpost wrote...
What you have should not compile. You have an extra closing parenthesis after 'getKey()'.
it compiles
Hondrok Hondrok

2017/1/20

#
Hondrok wrote...
danpost wrote...
What you have should not compile. You have an extra closing parenthesis after 'getKey()'.
it compiles but it doesn'y work
Hondrok Hondrok

2017/1/20

#
danpost wrote...
What you have should not compile. You have an extra closing parenthesis after 'getKey()'.
it compiles but it doesn't work
danpost danpost

2017/1/20

#
Hondrok wrote...
it compiles but it doesn't work
You should not use 'getKey': * if you use is somewhere else; or * if you have more than one actor object in the world of that type (created from the class that describes the actor that shoots these spheres); I will not use 'getKey' at all, unless I am waiting for textual keyboard input (like in an editor or a text inputbox).
Hondrok Hondrok

2017/1/20

#
danpost wrote...
Hondrok wrote...
it compiles but it doesn't work
You should not use 'getKey': * if you use is somewhere else; or * if you have more than one actor object in the world of that type (created from the class that describes the actor that shoots these spheres); I will not use 'getKey' at all, unless I am waiting for textual keyboard input (like in an editor or a text inputbox).
what shoud I use instead of it? I used it because otherwise the actor won't shoot just 1 bullet when I press the space bar
danpost danpost

2017/1/20

#
Add a boolean field to track the state of the 'space' key. Use it in conjuction with 'isKeyDown':
// with instance field
private boolean spaceDown;

// check to shoot
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    shoot(); // supply shoot method or code it here
    spaceDown = true;
}
if (spaceDown && !Greenfoot.isKeyDown("space")) spaceDown = false;
Hondrok Hondrok

2017/1/20

#
danpost wrote...
Add a boolean field to track the state of the 'space' key. Use it in conjuction with 'isKeyDown':
// with instance field
private boolean spaceDown;

// check to shoot
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    shoot(); // supply shoot method or code it here
    spaceDown = true;
}
if (spaceDown && !Greenfoot.isKeyDown("space")) spaceDown = false;
is gives me the error that spaceDown can not be found
private boolean spaceDown()
    {
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
            setImage("Goku_attack.png");
            spaceDown = true;
        }
        
        if (spaceDown && !Greenfoot.isKeyDown("space")) 
        {
            spaceDown = false;
        }
    }
Nosson1459 Nosson1459

2017/1/20

#
spaceDown is not supposed to be a method (though you could have a method for checking the key), it's a boolean variable which you define in the beginning of your class (if you define it in the method you're calling then it won't do anything because you keep on resetting it).
Hondrok Hondrok

2017/1/20

#
Nosson1459 wrote...
spaceDown is not supposed to be a method (though you could have a method for checking the key), it's a boolean variable which you define in the beginning of your class (if you define it in the method you're calling then it won't do anything because you keep on resetting it).
danpost wrote...
Add a boolean field to track the state of the 'space' key. Use it in conjuction with 'isKeyDown':
// with instance field
private boolean spaceDown;

// check to shoot
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    shoot(); // supply shoot method or code it here
    spaceDown = true;
}
if (spaceDown && !Greenfoot.isKeyDown("space")) spaceDown = false;
it works now thank youu
You need to login to post a reply.