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

2013/12/16

help with car game please

Hiimachicken Hiimachicken

2013/12/16

#
I am making a game and you are a car and there's cars that randomly coming down at you the car that you are is called Car.class and the other one is Karr.class and they add randomly but they overlap the same Karr and in the booklet it gives you a clue on how to do it and it is
getOneIntersectingObject
can anyone help thanks Karr class also the problem is it stops the game?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


 
public class Karr extends Actor
{
    private int vSpeed = 2;
    public void act() 
    {
//         addObject(new Karr(), Greenfoot.getRandomNumber(200)+200, 0);

        moveDown();
       setRotation(90);
       checkPosition();
       checkCollision1();
       
    }    
    private void moveDown()
    {
        setLocation(getX(), getY()+vSpeed);
    }
    private void checkPosition()
    {
        if(getY()>= getWorld().getHeight()-1)
        getWorld().removeObject(this);
       
    }
    private void checkCollision1()
    {
        
      Actor karr = getOneIntersectingObject(Karr.class);
        
        if(karr !=null)
        {
           
           getWorld().removeObject(this);
        }
        
    }
}
code
danpost danpost

2013/12/16

#
The reason the game stops is because you cannot use 'getOneIntersectingObject' (or any other Actor class method that requires the actor to be in the world) after removing the actor from the world. Your 'act' method calls 'checkPosition', which will remove the actor when it hits the bottom of the screen, followed by 'checkCollision1', which calls 'getOneIntersectingObject' and requires that the actor be in the world or it will cause an error. You need to make sure the actor is still in the world after calling 'checkPosition' and before calling 'checkCollision1':
if (getWorld() == null) return;
Inserting the statement above between the two method calls should solve the problem.
Hiimachicken Hiimachicken

2013/12/16

#
where abouts would i put this code?
Hiimachicken Hiimachicken

2013/12/16

#
its okay i got it thanks for the help :D
You need to login to post a reply.