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

2019/1/24

I am trying to make the enemy hinder dissapear after a multiple hits. This code doesn't even remove the Enemy.??

JoNGrEeNfOoT JoNGrEeNfOoT

2019/1/24

#
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(); 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); } } public void destroyHinder() { Actor hinder = getOneIntersectingObject(Hinder.class); if(hinder != null){ getWorld().removeObject(hinder); Myworld myworld = (Myworld)getWorld(); myworld.addScore(20); } } private int speed = 10; private int health = 30; }
danpost danpost

2019/1/24

#
I am trying to make the enemy hinder dissapear after a multiple hits. This code doesn't even remove the Enemy.?? << Code Omitted >>
From where are you calling the destroyHinder method?
JoNGrEeNfOoT JoNGrEeNfOoT

2019/1/25

#
In my bullet class (misille)
danpost danpost

2019/1/25

#
JoNGrEeNfOoT wrote...
In my bullet class (misille)
I do not see from where in the above given code.
JoNGrEeNfOoT JoNGrEeNfOoT

2019/1/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(); if (getWorld() != null) destroyHelicopter(); if (getWorld() != null) destroyHinder(); } //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); } } public void destroyHinder() { Actor hinder = getOneIntersectingObject(Hinder.class); if (this != null) { getWorld().removeObject(this); health--; if (health == 0) getWorld().removeObject(Hinder); } } private int speed = 10; private int health = 3; }
JoNGrEeNfOoT JoNGrEeNfOoT

2019/1/26

#
Forgot to call the method, thanks. But I still can't figure it out?
danpost danpost

2019/1/27

#
JoNGrEeNfOoT wrote...
Forgot to call the method, thanks. But I still can't figure it out?
Apparently, you have modified the destroyHinder method to the following:
public void destroyHinder()
{
   Actor hinder = getOneIntersectingObject(Hinder.class);
   if (this != null)
   {
        getWorld().removeObject(this);
        health--;
        if (health == 0) getWorld().removeObject(Hinder);
   }
}
Line 4 is peculiar as this is never null. Plus, it does not make sense to remove a Hinder object when the health of a missile goes to zero (line 8). Anyway, getWorld on line 8 will fail to a NullPointerException as the missile will not be in a world to get at that point (because of line 6).
JoNGrEeNfOoT JoNGrEeNfOoT

2019/1/29

#
Okay thanks
JoNGrEeNfOoT JoNGrEeNfOoT

2019/1/29

#
public void destroyHinder() { Actor Missile = getOneIntersectingObject(Missile.class); if (Missile != null) { getWorld().removeObject(Missile); health--; if (health == 0) getWorld().removeObject(this); } } private int health = 3; I changed the code and put it in my Hinder class instead
JoNGrEeNfOoT JoNGrEeNfOoT

2019/1/29

#
Then I called the method in my public void act! if(getWorld() != null)destroyHinder();
danpost danpost

2019/1/29

#
JoNGrEeNfOoT wrote...
Then I called the method in my public void act! if(getWorld() != null)destroyHinder();
That should work.
You need to login to post a reply.