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

2017/4/22

"YouLost" not working

1
2
Ostarius Ostarius

2017/4/22

#
danpost wrote...
Ostarius wrote...
Danpost you here?
For the moment. I am getting ready to depart for a while. Line 6 of what Hippo supplied should be:
((Boss)boss).loseLife(1);
Thank you very much it works!
Ostarius Ostarius

2017/4/22

#
danpost wrote...
Change line 10 to:
if (getObjects(DonkeyKong.class).isEmpty() && getObjects(YouLost.class).isEmpty())
You only want to create and add a YouLost object when the player is not in the world AND a YouLost object has not been added yet.
this is still not working, it just pauses my game and i need to click "run" again for the "you lost" screen to appear
Super_Hippo Super_Hippo

2017/4/22

#
If it is pausing your game (and you don't use 'Greenfoot.stop()' somewhere), then there should be an error message showing where exactly what error happened.
Ostarius Ostarius

2017/4/22

#
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:711)
	at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913)
	at DonkeyKong.onGround(DonkeyKong.java:105)
	at DonkeyKong.act(DonkeyKong.java:33)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
Super_Hippo Super_Hippo

2017/4/22

#
Show the DonkeyKong code.
Ostarius Ostarius

2017/4/22

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DonkeyKong here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DonkeyKong extends Animal
{
    /**
     * Act - do whatever the DonkeyKong wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed = 5;
    private int vSpeed = 5;
    private boolean spaceDown; 
    private int timer = 0;
    public static int score = 0;
    private GreenfootImage left = new GreenfootImage("DKleft.png");
    private GreenfootImage right = new GreenfootImage("DKright.png");
    private GreenfootImage up = new GreenfootImage("DKjumping.png");
    
    public DonkeyKong()
    {
        this.setImage(right);
    }   
    
    public void act() 
    {
        this.controls();
        this.moveVertically();
        this.onGround();
        this.setLocation(getX(), getY()+vSpeed);
        this.touchingBricks();
        this.score();
        this.brickFix();
        this.atWorldEdge();
        this.changeWorld();
        //if (getOneIntersectingObject(Bricks.class) != null) move(-speed);         
        //if (getOneIntersectingObject(Bricks.class) != null) move(+speed); 
        
        //if (getOneIntersectingObject(Blocks.class) != null) move(-speed);         
        //if (getOneIntersectingObject(Blocks.class) != null) move(+speed);
    }    
    
    public void changeWorld()
   {
       World world = null;
       if (getX() == getWorld().getWidth()-1)
       {
           if (getWorld() instanceof MyWorld) world = new MyWorld2();
           else if (getWorld() instanceof MyWorld2) world = new MyWorld3();
           else if (getWorld() instanceof MyWorld3) world = new MyWorld4();
           else if (getWorld() instanceof MyWorld4) world = new MyWorld5();
           else if (getWorld() instanceof MyWorld5) world = new MyWorld6();
           else if (getWorld() instanceof MyWorld6) world = new MyWorld7();
           else if (getWorld() instanceof MyWorld7) world = new MyWorld8();
           Greenfoot.setWorld(world);
       }
   }
    
    public void controls()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            this.setImage(right);
            this.move(speed);
        }
        
        if (Greenfoot.isKeyDown("left"))
        {
            this.setImage(left);
            this.move(-speed);
        }
        
        Actor bullet = new Weapons();
        if (getWorld() instanceof MyWorld8) bullet = new DkBoss();
        
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown = true;

            getWorld().addObject(bullet, getX() + 60, getY() - 10);
            
            if (this.getImage() == left) bullet.setRotation(180);
            else if (this.getImage() == up) bullet.setRotation(270);
        }
        
        if (spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        
        }
        
        if (this.isTouching(Enemies.class))
        {
            getWorld().removeObject(this);
        }
    }
    
    public boolean onGround()  
    {  
        int height = getImage().getHeight();  
        Actor ground = getOneObjectAtOffset(5, height/2 + 5, Bricks.class);  
        return ground != null;   
    }  
    
    public void touchingBricks()
    {
        
        if (!getIntersectingObjects(Bricks.class).isEmpty()) 
        {
            this.setLocation(getX(), getY()-5);
            //this.setImage("DKright.png");
        } 
 
    }

    public void moveVertically()
    {
        
        if( Greenfoot.isKeyDown("up"))
        {
            this.setImage(up);
            this.setLocation(getX(), getY()-20);
        }  
    }
    
    public void score()
    {
        if (this.isTouching(Blocks.class))
        {
            this.score++;
        }
    }
    
    public void brickFix()
    {
        if (isTouching(Bricks.class) && (getImage().equals(right)))
        {
            move(-speed);
        } 
        
        if (isTouching(Bricks.class) && (getImage().equals(left)))
        {
            move(speed);
        }    
        
        if (isTouching(Bricks.class) && (getImage().equals(up)))
        {
            this.setLocation(getX(), getY()+25);
        }  
    }
    
    
}
Super_Hippo Super_Hippo

2017/4/22

#
After line 31, add this line:
if (getWorld() == null) return;
Ostarius Ostarius

2017/4/22

#
Not working, still the same
Ostarius Ostarius

2017/4/22

#
Wait nvm it's suddenly working?
You need to login to post a reply.
1
2