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

2024/5/24

How to move onto next level without creating an infinite next level loop

classKniv classKniv

2024/5/24

#
Problem: My scorecounter keeps track of mobs killed. If mobs killed in myWorld is 3, then the score resets to 0 and world level2 is created. However when the score again reaches 3, it resets level2. Why is this?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Main Player class
 * 
 * @author 
 * @version v1.0-beta
 */
public class player extends Actor
{
    private int energy = 300; //player energy (used when pressing space
    private int health = 100; //player health
    private int coolDown = 100; //cooldown for blocks
    private int enmHealth = 100; //enemy health track
    private int numOfBars = 200; //number of temp blocks
    private int level = 1; //player current level
    /*
     * @author Agniva
     * @description Pretty much the main method
     */
    public void act()
    {
        controls();
        getWorld().showText("Energy: " + energy/10, 55,30);
        setLocation(getX(), getY());
        eat();
        getEaten();
    }
    /*
     * @author Agniva
     * @description Player attack simulations
     */
    public void getEaten(){
        Actor enemy;
        enemy= getOneObjectAtOffset(0,0,mob.class);
        if(enemy !=null){
            health -=5;
            Greenfoot.delay(5);
            getWorld().showText("Health: " + health, 55,15);
            if(health==0){
                World world;
                world = getWorld();
                getWorld().showText("GAME OVER", 75 , 100);
                world.removeObject( this );
                Greenfoot.stop();
            }
        }
    }
    /*
     * @author Agniva
     * @description Eating process of all food for player
     */
    public void eat(){
        Actor fuud;
        fuud = getOneObjectAtOffset(0,0,food.class);
        if(fuud !=null){
            getWorld().removeObject(fuud);
            energy +=100;
            health +=10;
            getWorld().showText("Health: " + health, 55,15);
        }
    }
    /*
     * @author Agniva
     * @param Valid X/Y Coords
     * @description Prevents movability of worldBricks for player
     */
    
    public void setLocation(int x, int y){
        if(getWorld().getObjectsAt(x,y,worldBrick.class).isEmpty()){
            super.setLocation(x,y);
        }
    }
    /*
     * @author Agniva
     * @description Controls player given keyboard inputs only
     */
    public void controls(){
        if(Greenfoot.isKeyDown("a")){
            turn(-2);
        }
        if(Greenfoot.isKeyDown("d")){
            turn(2);
        }
        if(Greenfoot.isKeyDown("w")){
            if(Greenfoot.isKeyDown("space")&& (energy != 0)){
                move(4);
                energy--;
            }
            else{
                move(2);   
            }
        }
        if(Greenfoot.isKeyDown("s")){
            if(Greenfoot.isKeyDown("space") && (energy != 0)){
                move(-4);
                energy--;
            }
            else{
                move(-2);   
            }
        }        
        //if 1 is pressed, brick is placed.
        if(Greenfoot.isKeyDown("q")){
            if(coolDown == 1000 && numOfBars >0){
                playerBrick br = new playerBrick();
                getWorld().addObject(br, getX()+2, getY()+2);
                coolDown = 0;
                numOfBars--;
                getWorld().showText("Blocks: " + numOfBars, 60 , 70);
            }
            else{
                while(coolDown !=1000){
                    coolDown++;
                }
            }
        }        
        //if button 2 is pressed, enemy health is reduced by 50%. 
        //enough health will restore next to 100
        if(Greenfoot.isKeyDown("e")){
            Actor enemy;
            ArrayList<scoreCount> scoreD = new ArrayList<scoreCount>();
            for (scoreCount obj : getWorld().getObjects(scoreCount.class)) 
                scoreD.add((scoreCount)obj);
            enemy= getOneObjectAtOffset(0,0,mob.class);
            scoreCount object = scoreD.get(0);

            if(enemy !=null){
                enmHealth-=50;
                if(enmHealth <0){
                    getWorld().removeObject(enemy);
                    energy +=50;
                    enmHealth = 100;
                    object.incrementScore();
                }
            }
            if(object.score() == 3 && level == 1){
                level++;
                object.setScore(0);
                level2 world2 = new level2();
                Greenfoot.setWorld(world2);
            }
        }
        if(Greenfoot.isKeyDown("r")){
            Actor bricker;
            bricker = getOneObjectAtOffset(0, 0, worldBrick.class);
            if(isTouching(worldBrick.class)){
                removeTouching(worldBrick.class);
                numOfBars +=50;
                getWorld().showText("Blocks: " + numOfBars, 60 , 70);
            }
        }
    }
}
Fran40 Fran40

2024/5/24

#
Could you post the code of the level2? I think it may be because you are creating a new player in level2, so the attribute level of the player is one and when you check that condition the world will restart
danpost danpost

2024/5/25

#
I don't think you should have a level field in the player class. The world it is in can determine (is) the level. Remove the field (and line 138) and replace line 137 with the following:
if (object.score() == 3 && (getWorld() instanceof level1)) {
(presuming level1 is the name of the first level world)
classKniv classKniv

2024/8/8

#
Erroring code seems to be from LN 120-143
danpost danpost

2024/8/8

#
classKniv wrote...
Erroring code seems to be from LN 120-143
Lines 121 to 126 can be simplified to the following:
scoreCount object = (scoreCount)getWorld().getObjects(scoreCount.class).get(0);
Actor enemy = getOneObjectAtOffset(0, 0, mob.class);
You need to login to post a reply.