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

2025/1/16

my program stops after an actor reaches the edge

mobichl mobichl

2025/1/16

#
hi! im coding a game where the player has to dodge falling asteroids. i changed my code to have a 1/100 chance to spawn a new asteroid, but now my program stops after the asteroid reaches the edge of the world. asteroid code:
public void act()
    {
        move();
        
        Player p = (Player)getWorld().getObjects(Player.class).get(0);
        p = (Player)getOneIntersectingObject(Player.class);
        if (p != null && GameWorld.getImmunity() == false)
        {
            int hp = p.getHP();
            hp--;
            GameWorld.changePoints(-50);
            GameWorld.hpCounter.setValue(hp);
            getWorld().removeObject(this);
            
            if(hp == 0)
            {
                Greenfoot.setWorld(new LoseWorld());
            }
        }
    }
    
    public void move()
    {
        setLocation(getX(), getY() + speed);
        
        if(isAtEdge())
        {
            getWorld().removeObject(this);
            GameWorld.changePoints(5);
        }
    }
game world code:
public void act()
    {
        int random = Greenfoot.getRandomNumber(100);
        if (random == 0)
        { 
            addAsteroid();
        }
        
        timer.setValue(timeLeft - t.millisElapsed()/1000);
        
        if(timer.getValue() == 0)
        {
            Greenfoot.setWorld(new LoseWorld());
        }
    }
    
    public void addAsteroidp()
    {
        int randX = Greenfoot.getRandomNumber(getBackground().getWidth());
        addObject(new Asteroid(), randX, WIDTH/10);
    }
danpost danpost

2025/1/16

#
mobichl wrote...
hi! im coding a game where the player has to dodge falling asteroids. i changed my code to have a 1/100 chance to spawn a new asteroid, but now my program stops after the asteroid reaches the edge of the world. << Code Omitted >>
If an error message shows, copy and paste here. If not, try commenting out lines 15 thru 19 in Asteroid class code above. Or try commenting out lines 11 thru 14 in the GameWorld code above. Hopefully that will give indication as to which code triggered the game over. Then, go from there.
You need to login to post a reply.