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

2014/4/17

Help for Bomb Power-UP

1
2
3
danpost danpost

2014/4/20

#
You should clear your terminal so that all those old error messages are removed. The last error message indicates the problem is in your Player1 class.
Neymar.Jr Neymar.Jr

2014/4/21

#
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:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Enemy.checkPlayer1Collision(Enemy.java:44) at Enemy.act(Enemy.java:27) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) and my collision code is
private void checkPlayer1Collision() {
        Player1 player1 = (Player1)getOneIntersectingObject(Player1.class);
        if(player1 != null) {
            getWorld().removeObject(player1);
        }
    }
danpost danpost

2014/4/21

#
You will need to post the act method of the Enemy class at least up to line 27 in the class; and possibly the code for all the methods it calls prior to the call to 'checkPlayer1Collision'.
Neymar.Jr Neymar.Jr

2014/4/22

#
import greenfoot.*;
public class Enemy extends Actor {
    
   
    private static final GreenfootImage
    ENEMY_IMAGE = new GreenfootImage("robot.png");
    private ScoreManager score;
    private int maxHealth;
    private int health;
    private int speed;
    private int value;

    public Enemy(ScoreManager score, int maxHealth, int speed, int value) {
        this.score = score;
        this.maxHealth = maxHealth;
        this.speed = speed;
        this.value = value;
        health = maxHealth;
        setImage(ENEMY_IMAGE);
    }

    public void act() {
        setLocation(getX() - speed, getY());

        checkIsOnScreen();

        checkPlayer1Collision();
        

    }

    public void damage(int amount) {
        health -= amount;
        if(health <= 0) {
            score.add(value);
            getWorld().addObject(new Explosion2(), getX(), getY());
            getWorld().removeObject(this);
        } else {
            //healthBar.updateImage();
        }
    }

    private void checkPlayer1Collision() {
        Player1 player1 = (Player1)getOneIntersectingObject(Player1.class);
        if(player1 != null) {
            getWorld().removeObject(player1);
        }
    }
    
    

    private void checkIsOnScreen() {
        if(!isOnScreen()) {
            getWorld().removeObject(this);
        }
        
       
        
    }

    private boolean isOnScreen() {
        return getX() > 0;
    }
}
danpost danpost

2014/4/22

#
Add at line 26:
if (getWorld() == null) return;
No code in the method will execute beyond that point if the actor has been removed from the world ('checkIsOnScreen' may remove the actor and 'checkPlayer1Collision' cannot properly execute if the actor is not in the world).
Neymar.Jr Neymar.Jr

2014/4/25

#
Thanks its working great now but I am wondering how to set a sound limit/volume for my explosion sound.
danpost danpost

2014/4/25

#
The GreenfootSound class has a method to set the volume of a GreenfootSound object.
Neymar.Jr Neymar.Jr

2014/5/1

#
Okay thank-you, I have tried something new by adding in the bomb code that when player 1 or 2 collides with it, it will add 10 to the counter but now every single time it collides it stops the game.
public void Player1Collision()
    {
        if (getWorld() != null)
        {
            Player1 collision = (Player1) getOneIntersectingObject(Player1.class);
            if (collision != null) 
            {
                SpaceWorld space = (SpaceWorld)getWorld(); //gets the space world

                getWorld().addObject(new Explosion3(), getX(), getY());
                space.removeObject(this);
                space.removeObjects(space.getObjects(Enemy.class));
                ((Counter)getWorld().getObjects(Counter.class).get(0)).add(10); 
                space.removeObjects(space.getObjects(Enemy_Bullet.class));
            }
        }
    }
Neymar.Jr Neymar.Jr

2014/5/2

#
And the new code i added was:
((Counter)getWorld().getObjects(Counter.class).get(0)).add(10); 
danpost danpost

2014/5/2

#
On line 13, you have to use this:
((Counter)space.getObjects(Counter.class).get(0)).add(10);
as line 11 causes 'getWorld' to return a 'null' value.
You need to login to post a reply.
1
2
3