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

2018/12/26

My missile continues in all eternity and destroys object out of the frame after I shoot my missiles?

JoNGrEeNfOoT JoNGrEeNfOoT

2018/12/26

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Missile here. * * @author (your name) * @version (a version number or a date) */ public class Missile extends Actor { /**int h = 0;**/ public void act() { setLocation(getX() + speed, getY()); checkBoundaries(); destroyHelicopter(); } //we add a method "checkBoundaries()" that destroys bullets that are off screen. public void checkBoundaries() { if(getX() > getWorld().getWidth() - 1) { getWorld().removeObject(this); } else if(getX() < 1) getWorld().removeObject(this); if(getY() > getWorld().getHeight() - 1) getWorld().removeObject(this); else if(getY() < 1) getWorld().removeObject(this); } //"destroyEnemies()" destroys enemies. public void destroyHelicopter() { //"Enemy" can be any class that you want the bullet to destroy. Actor helicopter = getOneIntersectingObject(Helicopter.class); if(helicopter != null) { getWorld().removeObject(helicopter); Myworld myworld = (Myworld)getWorld(); myworld.addScore(20); } } private int speed = 10; } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * This is a white blood cell. This kind of cell has the job to catch bacteria and * remove them from the blood. * * @author Michael Kölling * @version 1.0 */ public class Aircraft extends Actor { Boolean spaceDown = false; /** * Act: move up and down when cursor keys are pressed. */ public void act() { checkKeyPress(); checkCollision(); checkFire(); //leave the rest of your player's act method alone } //after the "act()" method, add a new method: public void checkFire() { if (!spaceDown && Greenfoot.isKeyDown("space")) { spaceDown = true; getWorld().addObject(new Missile(), getX(), getY()); } if (spaceDown && !Greenfoot.isKeyDown("space")) { spaceDown = false; } /*if(Greenfoot.isKeyDown("space")) { getWorld().addObject(new Missile(), getX(), getY()); }*/ } /** * Check whether a keyboard key has been pressed and react if it has. */ private void checkKeyPress() { if (Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-8); } if (Greenfoot.getRandomNumber(100) < 99) { setLocation(getX(), getY()+3); } if (Greenfoot.getRandomNumber(100) < 99) { setLocation(getX(), getY()); } if (Greenfoot.isKeyDown("left")) { } } /** * Check whether we are touching a bacterium or virus. Remove bacteria. * Game over if we hit a virus. */ private void checkCollision() { if (isTouching(Bacteria.class)) { Greenfoot.playSound("slurp.wav"); removeTouching(Bacteria.class); Myworld myworld = (Myworld)getWorld(); myworld.addScore(20); } if (isTouching(Helicopter.class)) { removeTouching(Helicopter.class); Myworld myworld = (Myworld)getWorld(); myworld.addScore(-20); } } }
JoNGrEeNfOoT JoNGrEeNfOoT

2018/12/26

#
Ignore the comments
danpost danpost

2018/12/27

#
In your checkBoundaries method, the 1st and 3rd if conditions will never be true. A world 600 pixels wide will have x-coordinate values in the range from 0, inclusive, up to 600, exclusive.
JoNGrEeNfOoT JoNGrEeNfOoT

2018/12/27

#
I dont really understand." if(getX() > getWorld().getWidth() ) { getWorld().removeObject(this);" Doesn't that code mean that if the missiles x value passes the worlds width it will get destroyed ?
danpost danpost

2018/12/27

#
JoNGrEeNfOoT wrote...
I dont really understand." if(getX() > getWorld().getWidth() ) { getWorld().removeObject(this);" Doesn't that code mean that if the missiles x value passes the worlds width?
Yes -- well, almost (close enough). However, when is an actor ever passed the edge of a bounded world? Never. You should be asking if the actor is at the edge of the world:
if (getX() == getWorld().getWidth()-1)
(and similar with y and height). There is a much easier way, though:
public void checkBoundaries()
{
    if (isAtEdge()) getWorld().removeObject(this);
}
This checks all four edges for you.
JoNGrEeNfOoT JoNGrEeNfOoT

2018/12/28

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:714) at greenfoot.Actor.getOneIntersectingObject(Actor.java:965) at Missile.destroyHelicopter(Missile.java:34) at Missile.act(Missile.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) I got this error message when I used your last code; public void checkBoundaries() { if (isAtEdge()) getWorld().removeObject(this); }
danpost danpost

2018/12/28

#
JoNGrEeNfOoT wrote...
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. ... at greenfoot.Actor.getOneIntersectingObject(Actor.java:965) at Missile.destroyHelicopter(Missile.java:34) ... I got this error message when I used your last code;
The error occurred in your destroyHelioptor method while using getOneIntersectingObject (line 34 in the Missile class). You are trying to destroy a helicoptor that has already been removed from the world (due to world edge collision). In the act method, change the last line to:
if (getWorld() != null) destroyHelicoptor();
JoNGrEeNfOoT JoNGrEeNfOoT

2018/12/28

#
Now i got this error message; java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:714) at greenfoot.Actor.getOneIntersectingObject(Actor.java:965) at Missile.destroyHelicopter(Missile.java:37) at Missile.act(Missile.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2018/12/28

#
JoNGrEeNfOoT wrote...
Now i got this error message; << Error Omitted >>
Looks like the same error, except that a few extra lines were added to the class. Please show the entire class code. Oh, and please use code tags about your code.
JoNGrEeNfOoT JoNGrEeNfOoT

2018/12/28

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Missile here. * * @author (your name) * @version (a version number or a date) */ public class Missile extends Actor { /**int h = 0;**/ public void act() { setLocation(getX() + speed, getY()); checkBoundaries(); destroyHelicopter(); if (getWorld() != null) destroyHelicopter(); } //we add a method "checkBoundaries()" that destroys bullets that are off screen. public void checkBoundaries() { if (isAtEdge()) getWorld().removeObject(this); } //"destroyEnemies()" destroys enemies. public void destroyHelicopter() { Actor helicopter = getOneIntersectingObject(Helicopter.class); if(helicopter != null) { getWorld().removeObject(helicopter); Myworld myworld = (Myworld)getWorld(); myworld.addScore(20); } } private int speed = 10; }
danpost danpost

2018/12/29

#
Remove the line:
destrroyHelicoptor();
(my instruction was to change it, not add something after it).
JoNGrEeNfOoT JoNGrEeNfOoT

2018/12/30

#
Okay, thank you alot
You need to login to post a reply.