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

2019/3/23

Getting actors to shoot

I have a multiplayer game with two actors who shoot lasers. Both actors have pretty much identical code, yet one of them doesn't shoot. Here's the what I have for the one that works: in act... if ("down".equals(Greenfoot.getKey())) { shoot(); } and then at the end... public void shoot() { getWorld().addObject(new Laser(), getX() , getY()); } And here's the code for the one that doesn't work: In act... if ("s".equals(Greenfoot.getKey())) { shoot(); } later on... public void shoot() { getWorld().addObject(new JayLaser(), getX() , getY()); }
danpost danpost

2019/3/23

#
You cannot use the getKey method multiple times in one act cycle. Use isKeyDown in conjunction with boolean fields that track the state of the keys.
Thank you! It's working now :)
You need to login to post a reply.