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

2016/10/14

My car doesn't behave like it should.

josjuuNL josjuuNL

2016/10/14

#
I am making a game where you have to jump over the cars. (It's my first game) But I add the cars with a number of direction.
/**
     *  Spawns Cars
     */
    public void spawnCar()
    {
        if(Greenfoot.getRandomNumber(1000) % 19 == 0)
        {
            if(Greenfoot.getRandomNumber(250) % 5 == 0)
            {
                if(steps >= rememberSteps)
                {
                    direction = Greenfoot.getRandomNumber(2);
                    rememberSteps = steps + Greenfoot.getRandomNumber(100) +50;

                    if(direction == 1)
                    {
                        getWorld().addObject(new Car(direction), 950, 425);
                    }else if(direction == 0)
                    {
                        getWorld().addObject(new Car(direction), 50, 425);
                    }
                }
            }
        }

        steps++;
    }
With that direction I give it the way it should go, but it spawns on the wrong site and moves the wrong way.
public void makeMove(int direction)
    {
        if(steps == 0)
        {
            speed = Greenfoot.getRandomNumber(10)+5;
        }
        if(direction == 1)
        {
            move(speed-speed-10);
        }else if(direction == 0)
        {
            move(speed);
        }
    }
I still don't know what goes wrong. All help is welcome.
danpost danpost

2016/10/14

#
It would probably help if you posted the entire Car class code. That way we can follow the values and see how you store them and where all they are being used. There may be other things that may influence the movement, also.
NAMYA1403 NAMYA1403

2016/10/14

#
hey danpost can you help me with a issue?
NAMYA1403 NAMYA1403

2016/10/14

#
i am just creating a discussion pls help
josjuuNL josjuuNL

2016/10/14

#
Here is everything that contains car I think.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car here.
 * 
 * @author Jos
 * @version 12-10-16
 */
public class Car extends Mover
{
    int points  = 0;
    private int speed;
    private int steps = 0;
    private int direction;

    private GreenfootImage car1L;
    private GreenfootImage car2L;
    private GreenfootImage car3L;
    private GreenfootImage car4L;
    private GreenfootImage car1R;
    private GreenfootImage car2R;
    private GreenfootImage car3R;
    private GreenfootImage car4R;

    /**
     * Act - do whatever the Car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        makeMove(direction);
        switchImage();
        destroyAtEdge();
        steps++;
    } 

    /**
     * Something
     */
    public void makeMove(int direction)
    {
        if(steps == 0)
        {
            speed = Greenfoot.getRandomNumber(10)+5;
        }
        if(direction == 1)
        {
            move(speed-speed-10);
        }else if(direction == 0)
        {
            move(speed);
        }
    }

    /**
     * Gives a new image facing left
     */
    public void switchImage()
    {
        if(steps == 0)
        {
            if(direction == 1){
                switch(Greenfoot.getRandomNumber(3))
                {
                    case 0:
                    setImage(car1L);
                    break;
                    case 1:
                    setImage(car2L);
                    break;
                    case 2:
                    setImage(car3L);
                    break;
                    case 3:
                    setImage(car4L);
                    break;
                }
            }else{
                switch(Greenfoot.getRandomNumber(3))
                {
                    case 0:
                    setImage(car1R);
                    break;
                    case 1:
                    setImage(car2R);
                    break;
                    case 2:
                    setImage(car3R);
                    break;
                    case 3:
                    setImage(car4R);
                    break;
                }
            }
        }
    }

    /**
     * Destroys the Car when it is at an edge
     */
    public void destroyAtEdge()
    {
        if(atWorldEdge())
        {
            getWorld().removeObject(this);
            points++;
        }
    }

    public Car(int direction)
    {
        // direction = this.direction;
        car1L = new GreenfootImage("L Grey SUV.png");
        car2L = new GreenfootImage("L Red SUV.png");
        car3L = new GreenfootImage("L Green SUV.png");
        car4L = new GreenfootImage("L Blue SUV.png");
        car1R = new GreenfootImage("R Grey SUV.png");
        car2R = new GreenfootImage("R Red SUV.png");
        car3R = new GreenfootImage("R Green SUV.png");
        car4R = new GreenfootImage("R Blue SUV.png");
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Let's every subclass have these methodes
 * 
 * @author Jos 
 * @version 0.1
 */
public class Mover extends Actor
{
     public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    
    public void destroy(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    
    public void act() 
    {
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * In this clas it will make new objects.
 * 
 * @author Jos 
 * @version 12-10-16
 */
public class Spawner extends Actor
{
    private int steps = 1;
    private int rememberSteps = 0;
    private int direction;

    /**
     * Act - do whatever the Spawner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        spawnCar();  
    }  

    /**
     *  Spawns Cars
     */
    public void spawnCar()
    {
        if(Greenfoot.getRandomNumber(1000) % 19 == 0)
        {
            if(Greenfoot.getRandomNumber(250) % 5 == 0)
            {
                if(steps >= rememberSteps)
                {
                    direction = Greenfoot.getRandomNumber(2);
                    rememberSteps = steps + Greenfoot.getRandomNumber(100) +50;

                    if(direction == 1)
                    {
                        getWorld().addObject(new Car(direction), 950, 425);
                    }else if(direction == 0)
                    {
                        getWorld().addObject(new Car(direction), 50, 425);
                    }
                }
            }
        }

        steps++;
    }

    public Spawner()
    {
        setImage("Spawner.png");
    }
}
danpost danpost

2016/10/14

#
The Mover class does not provide anything more than what is already available for Actor objects. The 'isAtEdge' method of the Actor class can be used in place of the 'atWorldEdge' method of the Mover class; the 'removeTouching' method can be used in place of the 'destroy' method; and, 'isTouching' can be used in place of 'canSee'. The touching methods work just a little different from your Mover methods; but, they probably work better for your purposes. This all means that you can remove the Mover class and have 'Car extend Actor'. In the Car class, you are checking for (step == 0) in several places. You can accomplish the same thing without the checks by doing those actions when the actor is created by coding those processes in the constructor of the class. By performing these setup procedures in the constructor, you can eliminate the 'step' and 'direction' fields. Also, since the image will be set there also, you do not need any of the GreenfootImage fields. Currently, the 'points' field is not being used (the object is no longer in the world when it is incremented). So, it should be removed (until such time as you make it useful, if needed). You can even create the random direction value within the constructor, instead of before creating the Car and passing that value by way of a parameter. This all would make the class more simply like this:
import greenfoot.*;

public class Car extends Actor
{
    private int speed;
    
    public Car()
    {
        String direction = "";
        if (Greenfoot.getRandomNumber(2) == 0)
        {
            speed = 1;
            direction = "R";
        }
        else
        {
            speed = -1;
            direction = "L";
        }
        speed = speed*(5+Greenfoot.getRandomNumber(10));
        String[] colors = { "Grey", "Red", "Green", "Blue" };
        setImage(new GreenfootImage(direction+" "+colors[Greenfoot.getRandomNumber(4)]+" SUV.png"));
    }

    public void act()
    {
        move(speed);
        if (isAtEdge())
        {
            getWorld().removeObject(this);
            // points++;
        }
    }
}
Maybe moving the 'points' field to your world class would be better and then using something like:
((WorldClassName)getWorld()).points++;
josjuuNL josjuuNL

2016/10/14

#
It is quitte late, I will check it tomorrow. Thanks in advance.
danpost danpost

2016/10/15

#
I forgot to include code to place the actor appropriately for the direction it will be moving. In the above suggested Car class, add the following:
protected void addedToWorld(World world)
{
    if (speed > 0) setLocation(50, 425); else setLocation(950, 425);
The 'addedToWorld' method is part of the Actor class -- and like the 'act' method, it is called by greenfoot. It is called when an actor is added into any world (when using the 'addObject' method of the World class).
You need to login to post a reply.