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

2014/2/9

how to make a moving ground

Hiimachicken Hiimachicken

2014/2/9

#
Can any one tell me how to make the ground move and then keep adding the same image behind it so its unlimited if anyone can then thanks :)
danpost danpost

2014/2/9

#
Create two ground actors whose length is larger than the width of the world. In their act method have them move 2 of its widths left or right if 'getX' returns a value more than a little past half their width off an edge. This re-using of the actor prevents unnecessary creating/adding and removing/garbage collecting of actors. The thinner the actors are height-wise will also help to prevent lag. If you can get away with a non-moving base for the moving ground, that would help.
Hiimachicken Hiimachicken

2014/2/9

#
can you post the code please?
Hiimachicken Hiimachicken

2014/2/9

#
?
danpost danpost

2014/2/9

#
I used a 32x32 pixel sized image for the ground (the default image I set manually). The exact size is not important, just that you know the default image is tiled onto the actors created. Anyway, I added the two ground objects into the unbounded world using the following in the world constructor:
1
2
3
Actor ground = new Ground();
addObject(ground, 0, getHeight()-ground.getImage().getHeight()/2);
addObject(new Ground(), ground.getImage().getWidth(), ground.getY());
and used this for the Ground class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;
 
public class Ground extends Actor
{
    private int unitWidth, imageWidth, worldWidth;
     
    public void addedToWorld(World world)
    {
        worldWidth = world.getWidth();
        GreenfootImage unit = getImage();
        unitWidth = unit.getWidth();
        imageWidth = (worldWidth/unitWidth+5)*unitWidth;
        GreenfootImage image = new GreenfootImage(imageWidth, unit.getHeight());
        for (int i=0; i<imageWidth; i+=unitWidth) image.drawImage(unit, i, 0);
        setImage(image);
    }
 
    public void act()
    {
        if (getX() < -(imageWidth/2+unitWidth*3)) setLocation(getX()+2*imageWidth, getY());
        if (getX() > worldWidth+(imageWidth/2+unitWidth*3)) setLocation(getX()-2*imageWidth, getY());
    }
}
Hiimachicken Hiimachicken

2014/2/9

#
nothing happens just works as normal but nothing is scrolling?
danpost danpost

2014/2/9

#
I did not put any movement code into it. I did not know if you wanted it to constantly scroll or scroll when your main actor moved. I had temporarily put in arrow key control for moving it for testing, but then removed it.
Hiimachicken Hiimachicken

2014/2/9

#
can you do this for me then and if you can then my game is complete haha, the ground to move the background to stay still and object coming towards the actor to try kill him i have the object that come towards him called Pipe1 and Pipe2 and the background called Background and the ground called Ground if you can do this then thank yu very so much
danpost danpost

2014/2/9

#
(1) the ground to move: I think you should be able to handle that (if you cannot at this point, you probably should not be programming!); (2) the background to stay still: to have something NOT happen requires no code; (3) the object coming towards the actor to try kill him: this should go along with the answer to (1). You have had at least two months to acquire some knowledge of java programming and using Greenfoot. Something as simple as adding an actor and giving it the ability to move is something you should be familiar enough with to tackle on your own.
danpost danpost

2014/2/10

#
I have noticed that there is a need to synchronize the movement of the two Ground objects. You can accomplish this by not altering the Ground class I provided above and by having the world act method iterate through all Ground objects and move all of them (both of them) at once:
1
2
3
4
5
for (Object obj : getObjects(Ground.class))
{
    Actor ground = (Actor)obj;
    // move ground
}
You need to login to post a reply.