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

2016/11/1

Need help creating the effect of moving ground

JWK3986 JWK3986

2016/11/1

#
I am using a png that should be edited to be the exact same length of my World and also to blend seamlessly (I hope) into one another when set end to end, and currently have it working that when I run the scenario it moves left on the X axis slowly to give the appearance it is moving, but I am having trouble finding a method to create another image continuously lined up end to end with the original sandy border the image makes. I would also like to adjust when my "Sand" object is removed at the edge of the world if possible, since the current centerpoint of the image hits the edge and causes it to be removed too early, as once another "Sand" object is created it would leave a large gap. Would it be improper coding technique to create a while loop to create "sand" objects and not increment the loop variable so it never stops creating the objects at certain intervals? What I am using so far is painfully simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Sand1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Sand1 extends Actor
{
    /**
     * Act - do whatever the Sand1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(-1);
         if (isAtEdge() == true)
        {
            getWorld().removeObject(this);
        }
         
             
             
        
    }   
}
JWK3986 JWK3986

2016/11/1

#
Wait.. Nevermind, I may have found a solution. I believe by making the world unbounded I may be able to achieve the effect I'm looking for, though I may be mistaken because that may affect other objects behavior in a way I don't want....
danpost danpost

2016/11/1

#
The first thing you may want to do is unbound your world. Change the super call in your world constructor:
1
2
3
4
// from
super(800, 600, 1); // for example
// to
super(800, 600, 1, false); // added bounding parameter (see World API)
Now, you will be able to leap-frog the ground and sand actors. You just need to make sure that you have enough of each in the world to span with width of the world plus one more; then when they are totally off the screen, getX() <= -getImage().getWidth(), move them (do not remove them)by their image width times the number of that type actor in the world to leap over to the side that will soon be in the world.
danpost danpost

2016/11/1

#
JWK3986 wrote...
Wait.. Nevermind, I may have found a solution. I believe by making the world unbounded I may be able to achieve the effect I'm looking for, though I may be mistaken because that may affect other objects behavior in a way I don't want....
Just make sure, instead of checking world edge with '==', that you use '>=' or '<=' and adjust the coordinate to be at least half the image width or height off the screen as needed for each type actor when appropriate.
JWK3986 JWK3986

2016/11/2

#
Thanks very much for the help, sorry for the late reply I was busy last night and all day. Between what you posted here and here I was able to get the ground scrolling just as intended. I'm wondering now if I want to have the same effect on some actors using Coral images to scroll at the same speed would adjusting the world code to include the first three lines you posted on Topic 5551, and the Coral actor code to include the second part work?
JWK3986 JWK3986

2016/11/2

#
hmmm not quite lol, I'll see what else I can try. Thanks for the rest!
JWK3986 JWK3986

2016/11/2

#
I suppose I can try to edit the ground image to include a couple corals then the code you led me to would probably suffice, am I right?
danpost danpost

2016/11/2

#
I guess you are now finding out that there are two parts to scrolling -- the background image and actors in the world. Actors should be coded to behave normally, as if the world does not scroll; and the scrolling system should provide the changes required to "move the camera" (adjusting the background image and the position of all actors in the world). The corals can then be either, like you said, put on the background image, or randomly added outside view to come into view (adding and removing them), or wrapped from gone out of view to coming into view (relocating them).
JWK3986 JWK3986

2016/11/2

#
As of now I'm using a stationary background image of an underwater ocean shelf scene, but I follow what you're saying as it applies to a scenario with a moving/changing background. thanks again for the speedy replies and info.
danpost danpost

2016/11/2

#
I just want to make it clear to others that my suggestions as far as scrolling actors here is specifically for an unlimited continuously scrolling world. For a limited scrolling world, usually all actors are placed into it to start, whether in view or not.
JWK3986 JWK3986

2016/11/2

#
In that case the world would be unbounded and some actors would be set at coordinates that are off screen until scrolled to, correct?
danpost danpost

2016/11/2

#
JWK3986 wrote...
In that case the world would be unbounded and some actors would be set at coordinates that are off screen until scrolled to, correct?
Correct -- so that actors are not unnaturally moved around when revisiting areas moved away from.
JWK3986 JWK3986

2016/11/2

#
Ok, makes sense. Thanks again!
You need to login to post a reply.