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

2012/12/13

Add Object Question

mncomic mncomic

2012/12/13

#
I'm working on a game where 3 guns turn and fire at a tank moving within range...when I place three guns in world, they all three turn but only the first fires...how do I make the other two fire as well?
Gevater_Tod4711 Gevater_Tod4711

2012/12/13

#
You have to show us the code you are using. Only with this information we cant help you.
mncomic mncomic

2012/12/13

#
Code for Gun public class Gun extends Actor { /** * Act - do whatever the Gun wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (!getObjectsInRange(300, Sherman.class).isEmpty()) { Sherman sherman = (Sherman) getObjectsInRange(300, Sherman.class).get(0); turnTowards(sherman.getX(), sherman.getY()); } if ("space".equals(Greenfoot.getKey())) { fire(); } } private void fire() { Round round = new Round(); getWorld().addObject(round, getX(), getY()); round.setRotation(getRotation()); Greenfoot.playSound("Fire.wav"); } }
Gevater_Tod4711 Gevater_Tod4711

2012/12/13

#
Well I don't see why this shouldn't work. You could try to print a text using System.out.println() if the method fire is executed. If it's executed the mistake must be somewhere else.
mncomic mncomic

2012/12/13

#
Only one gun fires when space is pressed...should the other two be coded differently? They do turn towards tank but I can only get the original gun to fire...the other two were added...
Gevater_Tod4711 Gevater_Tod4711

2012/12/13

#
I think the code you posted was ok but it could be that the mistake is somewhere else. If you know that the fire method is executed your current code for the class gun is right and you have to surch for the mistake somwhere else (probably in the class Round).
vonmeth vonmeth

2012/12/13

#
The problem is how getKey works. After it has been called once, the next time it is called it will return null. So, Gun A, is returning "space" when getKey is called (if space has been pressed), but Gun B's getKey is returning "null" and Gun C's getKey is returning "null", etc. Use isKeyDown, or put something like this in your world's act.
        if ("space".equals(Greenfoot.getKey())){
        List<Gun> guns = (List<Gun>) getObjects(Gun.class);
        for(Gun gun : guns)
            gun.fire();
        }
danpost danpost

2012/12/13

#
The problem is with the way 'getKey' works. Once a keystroke is returned with 'getKey', that key cannot be returned again; therefore, only one of your guns are going to react to the keystroke, because the others will get a return of 'null' (not 'space' -- again). One way is to use 'isKeyDown' instead of 'getKey', but you will probably need to add a delay timer in the class. Another way is to detect the keystroke in the world class and tell all the guns to fire from there.
mncomic mncomic

2012/12/13

#
Thanks everyone for your help...
You need to login to post a reply.