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

2016/6/17

Game over through multiple levels and when Actor/Objects are colliding

kinghunter kinghunter

2016/6/17

#
So I am currently trying to make a gameover when my actor comes into contact with bullets, the alien and the edge but am running into an error: non-static method addObject(greenfoot.Actor,int,int) cannot be referenced from a static context non-static method getWidth(); cannot be referenced from a static context // non-static method getHeight(); cannot be referenced from a static context heres the code:
public void checkCollision()
    {
        if(getOneIntersectingObject(Bullet.class) != null)
        {
            ((World)getWorld()).removeObject(this);
            GameOver gameover = new GameOver();
            World.addObject(gameover, World.getWidth()/2, getHeight()/2);
        }

        if(getOneIntersectingObject(Alien.class) != null)
        {
            ((World)getWorld()).removeObject(this);
             ((World)getWorld()).removeObject(this);
            GameOver gameover = new GameOver();
            World.addObject(gameover, World.getWidth()/2, getHeight()/2);
        }

        if (isAtEdge()) 
        {
            ((World)getWorld()).removeObject(this);
             ((World)getWorld()).removeObject(this);
            GameOver gameover = new GameOver();
            World.addObject(gameover, World.getWidth()/2, getHeight()/2);
        }
    }
game over class:
public class GameOver extends Actor
{
    public GameOver()
    {
      setImage(new GreenfootImage("Game Over", 50, Color.RED, Color.BLACK));   
    }      
}
any ideas on what is causing this / how to fix it is much appreciated
valdes valdes

2016/6/17

#
Yes, the error is because of lines World.addObject( World.getWidth( World.getHeight( those methods are not static (you cannot call them using the name of the class, you need an instance of the class) Do the following: first line inside checkCollision
World w = getWorldOfType(World.class);
then change all the sentences that you wrote with World.something with w.something That will remove the error, but then you will have other errors
valdes valdes

2016/6/17

#
the next error you will have is because of lines removeObject(this) if you remove the object, the object is no longer in the world, making a few methods fail, for example getOneIntersectingObject() and isAtEdge(). To correct that, change the if's to if-else or add inside each if
if (getWorld() != null && your_condition)
Also, remove the repeated lines of removeObject(), only one per if. You can modify that line also to
w.removeObject(this);
using the local variable w created in the previous post
kinghunter kinghunter

2016/6/18

#
Thanks a bunch! I fixed all the errors you pointed out and implemented your fixes and was able to complete my project!
danpost danpost

2016/6/18

#
valdes wrote...
if you remove the object, the object is no longer in the world, making a few methods fail, for example getOneIntersectingObject() and isAtEdge(). To correct that, change the if's to if-else or add inside each if
if (getWorld() != null && your_condition)
An alternative to this is to combine the three if statements, since the code within each block is the same:
if (getOneIntersectingObject(Bullet.class) != null || getOneIntersectingObject(Alien.class) != null || isAtEdge())
{
    World w = getWorld();
    w.removeObject(this);
    w.addObject(new GameOver(), w.getWidth()/2, w.getHeight()/2);
}
You need to login to post a reply.