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

2019/3/26

Getting actors to shoot one laser at a time?

Need help getting my actor to shoot one laser at a time as opposed to 4 or 5 at once. Here's my code: public class JayLaser extends Actor { public JayLaser() { GreenfootImage image = getImage(); // Setting the image (which is the GIHS logo) image.scale(50,50); setImage(image); } public void act() { setLocation(getX() - 5, getY()); // Laser shoots from left of player if (isTouching(Hakim.class)) // If laser touches opponent { World myWorld = getWorld(); MyWorld myworld = (MyWorld)myWorld; Counter counter = myworld.getCounter(); counter.loseHealth(-1); // The opponent's counter loses health getWorld().removeObject(this); // And the laser is removed from the world } } }
danpost danpost

2019/3/26

#
charliejustshrekedyou wrote...
Need help getting my actor to shoot one laser at a time as opposed to 4 or 5 at once. << Code Omitted >>
Incorrect code given for the given issue.
How do I fix it?
Super_Hippo Super_Hippo

2019/3/27

#
The issue is related to the creation of the Laser, not to the behavior of the Laser itself. So show how you create the Laser. You basically need another condition. One is a for example a button you need to press (you probably have this) and the other one is a timer so there is a minimum gap between shots.
Right, what I have is this: public void shoot() { getWorld().addObject(new JayLaser(), getX() , getY()); // Create and shoot laser on command And in act... if (Greenfoot.isKeyDown("s")) { shoot() }
danpost danpost

2019/3/27

#
Add a boolean field to track the state of the "s" key. Anytime the actual state of the key is not equal to the field, change the field value and check that new value as the condition to shoot.
You need to login to post a reply.