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

2017/3/10

Why this won't work

DuckGod DuckGod

2017/3/10

#
So I have this code and no errors pop up, but nothing happens when its supposed to reset the level when the fall is completed. I think it might be the way I ordered things, but then I tried to reorder things and messed it up even more so I put it back to normal. Here's the code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fall here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fall extends Effects
{
    private GreenfootImage EggFall = new GreenfootImage ("FallingEgg.png");
    private WorldLoader world;
    float ys = -4;
    float y =0;
    
    public void addedToWorld(World world) {
        this.world = (WorldLoader) world;
        y = getY();
    }
    
    public Fall ()
    {
        EggFall.scale(25,50);
        setImage(EggFall);
    }

    /**
     * Act - do whatever the Fall wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        y += ys;
        fallPhysics();
        setLocation(getX(),(int) y);
        retryLevel();
    }    

    public void fallPhysics()
    {
        if (ys< 6) 
        {
            ys+=0.3;
        }

        if(y<0) 
        {
            y = 0; 
            ys = 1;
        }
    }
    
    public void retryLevel()
    {
        if(y>500)
        {
            world.restartLevel();
        }
    }
}
DuckGod DuckGod

2017/3/10

#
If you were wondering what was in the restartLevel() code here it is
public void restartLevel()
    {
        setLevel(currentLevel);
    }
    
    public void restartGame()
    {
        currentLevel=0;
        setLevel(currentLevel);
    }
    
    public void setLevel(int i)
    {
        if (i <0 || i > maxLevel) {return;}
        cleanUp();
        currentLevel=i;
        switch(i)
        {
            case 0: testLvl(); break;
            case 1: testLvl2(); break;
        }
    }
danpost danpost

2017/3/10

#
I tested the code out and it seems okay to me. The Fall is falling and afterwards the world resets the level. Of course, I had to presume what codes you had for 'cleanUp' and for 'testLvl' plus a couple of other minor adjustments that was in no way related to falling or resetting the level. The only other thing I had to do was change what class the Fall class extends to Actor (I did not have the Effects class codes). So, in light of my results, I would say that cause of the issue you are having resides either in the Effects class or the WorldLoader class.
You need to login to post a reply.