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

2017/4/24

I need a loop

BrianOK BrianOK

2017/4/24

#
I need my floor to keep moving without it stopping. at the moment it goes for 1 second but ends, i need that to keep repeating
danpost danpost

2017/4/24

#
Show what code you have for the floor and any related codes (like world class codes that create and add objects of that class into the world and any other codes that deal with the movement of the floor).
BrianOK BrianOK

2017/4/24

#
danpost wrote...
Show what code you have for the floor and any related codes (like world class codes that create and add objects of that class into the world and any other codes that deal with the movement of the floor).
This is the Floor
   public void act() 
    {
     {
        setLocation(getX() -8 , getY()); 
    }    
}
}
This is the world
public Game()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false); 
        prepare();
i will have objects going across the floor but i need to have the floor in a loop first.
danpost danpost

2017/4/24

#
Show the prepare method and give what size image you are using for the floor.
BrianOK BrianOK

2017/4/24

#
danpost wrote...
Show the prepare method and give what size image you are using for the floor.
Not sure what the prepare method is but the size of the image is 2000x93 pixels
danpost danpost

2017/4/24

#
BrianOK wrote...
Not sure what the prepare method is
You have a call to the method at line 5 in Game class code given above.
BrianOK BrianOK

2017/4/24

#
danpost wrote...
BrianOK wrote...
Not sure what the prepare method is
You have a call to the method at line 5 in Game class code given above.
i dont understand?
danpost danpost

2017/4/24

#
BrianOK wrote...
i dont understand?
Then post the entire Game class code.
BrianOK BrianOK

2017/4/24

#
 public Game()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false); 
        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()
    {
        Floor floor = new Floor();
        addObject(floor,337,207);
        floor.setLocation(314,201);
^ floor.setLocation goes down alot more
danpost danpost

2017/4/24

#
The code starting at line 12 in your last post is the beginning of a method called 'prepare' -- it is the prepare method within your Game class. What was so difficult to understand about that??? Okay, so currently you are only adding one huge Floor object into the world (supposedly). I will presume that the final setLocation line has a second parameter that is greater than 353. It would be best if you could reduce the width of the floor image by about 60%; and its height should be such that none (or very little) of its image is below the bottom edge of the world window. The less height it has and the closer to the width of the world window (without going under that value) its width is, the less chance of lagging occurring during game-play. Change the prepare method to this:
private void prepare()
{
    Floor floor = new Floor();
    int floorW = floor.getImage().getWidth();
    int floorH = floor.getImage().getHeight();
    addObject(floor, floorW/2, getHeight()-floorH/2);
    floor = new Floor(); // create a second floor
    addObject(floor, floorW+floorW/2, getHeight()-floorH/2);
}
Then change your Floor class 'act' method to this:
public void act()
{
    move(-8);
    if (getX() < -getImage().getWidth()/2) move(getImage().getWidth()*2);
}
You need to login to post a reply.