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

2019/7/17

setPaintOrder is making my objects faster?

puggo7 puggo7

2019/7/17

#
Hello there! I'm creating a "Crossy Road"-ish game and some of the vehicles tend to overlap one another (an actor goes underneath another actor). In order to solve this, I put this method called "setPaintOrder" to hopefully align my actor classes correctly. However, it was working for all of the cars til I got to the police car (and all the cars after I implemented the police car, as well). I have the police car set at a specific speed (5), but for some reason it got faster once I inserted it into the method parameter list of setPaintOrder. FYI, I have the speed set at 5 for another car that's ALSO in setPaintOrder, but it's slower than the police car. Why is this happening and is there a way to fix this? Thank you, and my code for the police car along with the code for my setPaintOrder will be posted below! Code for the police car:
public void act() 
    {
        setLocation(getX() - 5, getY());
        wrapAtEdge();
    }; 
Code for my setPaintOrder:
setPaintOrder(Police.class, Car2.class, Car4.class, Car1.class, Car3.class, Car5.class, 
            Train.class, Trailer.class, Van.class, Motorcycle.class, Firetruck.class, Police.class, Log.class, Lilypad.class, Chicken.class, Water.class);
danpost danpost

2019/7/17

#
You should probably show the entire Police class.
puggo7 puggo7

2019/7/17

#
public class Police extends Enemies
{
    /**
     * Act - do whatever the Police wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 5, getY());
        wrapAtEdge();
    }    
}
The rest of it is just the automatic Greenfoot code for the Police class. That's why I didn't put the entirety of it down. Maybe putting in my entire code for MyWorld class will help more? (P.S. if the code for MyWorld class looks a bit messy, it's because I'm still a novice at the Java language!)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    GifImage myGif = new GifImage("water.gif");

    int [] [] map = 
        {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
            {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
            {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
            {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(750, 750, 1, false); 
        for(int row = 0; row < 16; row++)
            for(int col = 0; col < 16; col++)
                if (map [row][col] == 0)
                    addObject(new Image("grass.jpg"), col * 50, row * 50); 
                else if(map[row][col] == 1)
                    addObject(new Image("border.jpg"), col * 50, row * 50);
                else if (map[row][col] == 2)
                    addObject(new Image("road.jpg"), col * 50, row * 50);
                else if(map[row][col] == 3)
                    addObject(new Image("railroad.png"), col * 50, row * 50);
        //else if(map[row][col] == 4)
        //else if(map[row][col] == 5)
        //else if(map[row][col] == 6)
        //else if(map[row][col] == 7)
        //else if(map[row][col] == 8)
        addObject(new Chicken(), 380, 710);
        setPaintOrder(Police.class, Car2.class, Car4.class, Car1.class, Car3.class, Car5.class, 
            Train.class, Trailer.class, Van.class, Motorcycle.class, Firetruck.class, Police.class, Log.class, Lilypad.class, Chicken.class, Water.class);
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Water water = new Water();
        addObject(water,748,499);
        Water water2 = new Water();
        addObject(water2,698,499);
        Water water3 = new Water();
        addObject(water3,650,499);
        Water water4 = new Water();
        addObject(water4,600,499);
        Water water5 = new Water();
        addObject(water5,550,499);
        Water water6 = new Water();
        addObject(water6,500,499);
        Water water7 = new Water();
        addObject(water7,449,499);
        Water water8 = new Water();
        addObject(water8,400,499);
        Water water9 = new Water();
        addObject(water9,350,499);
        Water water10 = new Water();
        addObject(water10,301,499);
        Water water11 = new Water();
        addObject(water11,250,499);
        Water water12 = new Water();
        addObject(water12,200,499);
        Water water13 = new Water();
        addObject(water13,148,499);
        Water water14 = new Water();
        addObject(water14,97,499);
        Water water15 = new Water();
        addObject(water15,47,499);
        Water water16 = new Water();
        addObject(water16,0,499);
        Water water17 = new Water();
        addObject(water17,384,499);
        Water water18 = new Water();
        addObject(water18,122,499);
        Water water19 = new Water();
        addObject(water19,275,499);
        Water water20 = new Water();
        addObject(water20,470,499);
        removeObject(water18);
        Water water21 = new Water();
        addObject(water21,171,499);
        Water water22 = new Water();
        addObject(water22,126,499);
        Water water23 = new Water();
        addObject(water23,748,250);
        Water water24 = new Water();
        addObject(water24,697,250);
        Water water25 = new Water();
        addObject(water25,648,250);
        Water water26 = new Water();
        addObject(water26,598,250);
        Water water27 = new Water();
        addObject(water27,724,250);
        Water water28 = new Water();
        addObject(water28,548,250);
        Water water29 = new Water();
        addObject(water29,499,250);
        Water water30 = new Water();
        addObject(water30,450,250);
        Water water31 = new Water();
        addObject(water31,399,250);
        Water water32 = new Water();
        addObject(water32,351,250);
        Water water33 = new Water();
        addObject(water33,301,250);
        Water water34 = new Water();
        addObject(water34,251,250);
        Water water35 = new Water();
        addObject(water35,429,250);
        Water water36 = new Water();
        addObject(water36,201,250);

        Water water38 = new Water();
        addObject(water38,53,250);
        Water water39 = new Water();
        addObject(water39,10,250);
        Water water37 = new Water();

        removeObject(water37);

        Water water40 = new Water();
        addObject(water40,101,250);
        Water water41 = new Water();
        addObject(water41,119,250);
        Water water42 = new Water();
        addObject(water42,162,250);
        Water water59 = new Water();
        addObject(water59,749,549);
        Water water60 = new Water();
        addObject(water60,700,549);
        Water water61 = new Water();
        addObject(water61,650,549);
        Water water62 = new Water();
        addObject(water62,600,549);
        Water water63 = new Water();
        addObject(water63,550,549);
        Water water64 = new Water();
        addObject(water64,500,549);
        Water water65 = new Water();
        addObject(water65,450,549);
        Water water66 = new Water();
        addObject(water66,400,549);
        Water water67 = new Water();
        addObject(water67,350,549);
        Water water68 = new Water();
        addObject(water68,300,549);
        Water water69 = new Water();
        addObject(water69,251,549);
        Water water70 = new Water();
        addObject(water70,202,549);
        Water water71 = new Water();
        addObject(water71,152,549);
        Water water72 = new Water();
        addObject(water72,102,549);
        Water water73 = new Water();
        addObject(water73,52,549);
        Water water74 = new Water();
        addObject(water74,3,549);
        Car1 car1 = new Car1();
        addObject(car1,730,627);
        Car2 car2 = new Car2();
        addObject(car2,504,646);
        Car4 car4 = new Car4();
        addObject(car4,231,646);
        Car3 car3 = new Car3();
        addObject(car3,473,625);
        Car5 car5 = new Car5();
        addObject(car5,151,625);
        Water water75 = new Water();
        addObject(water75,725,200);
        Water water76 = new Water();
        addObject(water76,677,200);
        Water water77 = new Water();
        addObject(water77,628,200);
        Water water78 = new Water();
        addObject(water78,578,202);
        Water water79 = new Water();
        addObject(water79,528,203);
        Water water80 = new Water();
        addObject(water80,479,200);
        Water water81 = new Water();
        addObject(water81,432,200);
        Water water82 = new Water();
        addObject(water82,384,200);
        Water water83 = new Water();
        addObject(water83,335,200);
        Water water84 = new Water();
        addObject(water84,286,200);
        Water water85 = new Water();
        addObject(water85,237,200);
        Water water86 = new Water();
        addObject(water86,189,200);
        Water water87 = new Water();
        addObject(water87,138,200);
        Water water88 = new Water();
        addObject(water88,89,200);
        Water water89 = new Water();
        addObject(water89,39,200);

        Water water90 = new Water();
        addObject(water90,3,200);
        Water water91 = new Water();
        addObject(water91,526,200);
        Water water92 = new Water();
        addObject(water92,579,200);
        Water water93 = new Water();
        addObject(water93,165,200);

        Train train = new Train();
        addObject(train,40,568);
        Train train2 = new Train();
        addObject(train2,745,68);
        Trailer trailer = new Trailer();
        addObject(trailer,719,323);
        Van van = new Van();
        addObject(van,441,322);
        Car5 car52 = new Car5();
        addObject(car52,151,325);
        Car2 car22 = new Car2();
        addObject(car22,294,346);
        Police police = new Police();
        addObject(police,632,349);
    }
}
danpost danpost

2019/7/17

#
Please show the Enemies class. Also provide a list of its subclasses.
puggo7 puggo7

2019/7/17

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

/**
 * Write a description of class Enemies here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemies extends Movers
{
    /**
     * Act - do whatever the Enemies wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    

}
Car1 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car1 extends Enemies
{
    /**
     * Act - do whatever the Car1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 3, getY());
        wrapAtEdge();
    }    
}
Car2 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car2 extends Enemies
{
    /**
     * Act - do whatever the Car2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 5, getY());
        wrapAtEdge();
    }    
}
Car3 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car3 extends Enemies
{
    /**
     * Act - do whatever the Car3 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 3, getY());
        wrapAtEdge();
    }    
}
Car4 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car4 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car4 extends Enemies
{
    /**
     * Act - do whatever the Car4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 5, getY());
        wrapAtEdge();
    }    
}
Car5 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car5 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car5 extends Enemies
{
    /**
     * Act - do whatever the Car5 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 3, getY());
        wrapAtEdge();
    }    
}
Firetruck class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Firetruck here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Firetruck extends Enemies
{
    /**
     * Act - do whatever the Firetruck wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 5, getY());
        wrapAtEdge();
    }    
}
Motorcycle class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Motorcycle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Motorcycle extends Enemies
{
    /**
     * Act - do whatever the Motorcycle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 9, getY());
        wrapAtEdge();
    }    
}
Police class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Police here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Police extends Enemies
{
    /**
     * Act - do whatever the Police wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 5, getY());
        wrapAtEdge();
    }    
}
Trailer class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Trailer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Trailer extends Enemies
{
    /**
     * Act - do whatever the Trailer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 3, getY());
        wrapAtEdge();
    }    
}
Train class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Train here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Train extends Enemies
{
    /**
     * Act - do whatever the Train wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(9);
        wrapAtEdge();
    }
}  
Van class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Van here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Van extends Enemies
{
    /**
     * Act - do whatever the Van wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 3, getY());
        wrapAtEdge();
    }    
}
Water class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Water here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Water extends Enemies
{
    GifImage myGif = new GifImage("water.gif");
    /**
     * Act - do whatever the Water wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(myGif.getCurrentImage() );
    }
}
danpost danpost

2019/7/17

#
Your setPaintOrder statement can be reduced to the following:
setPaintOrder(Enemies.class, Log.class, Lilypad.class, Chicken.class);
It may be more reducible if Log and Lilypad both extend the same intermediate class before the Actor class. I do not know if this change will have any effect on your issue (it should not; but you proposed a change in that line caused it).
puggo7 puggo7

2019/7/17

#
A couple of cars still over lap, but other than that, my police car works and the most of them don't overlap anymore :). Thank you!
You need to login to post a reply.