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

2012/5/3

Auto scrolling World

AJTJ AJTJ

2012/5/3

#
Hey all :) We are considering making a game were the world scrolls to the right constanly. So you can't go back in the map but you have to follow through or you will die. Is it possible to do such thing? The project is a exam project. Best Regards Asger
SPower SPower

2012/5/3

#
Just put all the objects that must move in an array, and then say:
for (Actor a : nameofarray) {
move(5);
}
Or something like that
matt.milan matt.milan

2012/5/3

#
sometimes i'll create 2 main subclasses of actor, choosing a boolean scenario to divide the two actor ---Scroller ---NonScroller and then do
public void scroll()
{
    int speed = 3;
    for (Scroller scroller: getObjects(Scroller.class))
    {
          scroller.setLocation(scroller.getX() - speed,scroller.getY());
    }
}
putting scroll(); in the act method of my world.
Duta Duta

2012/5/3

#
matt.milan wrote...
sometimes i'll create 2 main subclasses of actor, choosing a boolean scenario to divide the two actor ---Scroller ---NonScroller and then do
public void scroll()
{
    int speed = 3;
    for (Scroller scroller: getObjects(Scroller.class))
    {
          scroller.setLocation(scroller.getX() - speed,scroller.getY());
    }
}
putting scroll(); in the act method of my world.
Line 4 is missing a cast - it should be:
for(Scroller scroller : (List<Scroller>)getObjects(Scroller.class))
and remember to add
import java.util.List;
for this. If you don't want to have an import statement for some reason, do this:
for(Scroller scroller : (java.util.List<Scroller>)getObjects(Scroller.class))
Also why do you need a "NonScroller" class? surely they're just normal Actors. But yes, like SPower and matt.milan basically said, you simply include a loop in your act() method which iterates through all of the actors which can scroll, and moves them left (if you want to scroll right). Finally, because you're scrolling everything in the case of your scenario, matt's code can just be turned into:
public void scroll() {
	int speed = 3;
	for(Actor a : getObjects(null))
		a.setLocation(a.getX() - speed, a.getY());
}
matt.milan matt.milan

2012/5/3

#
I was thinking under Non_Scroll you would have things like score display, hit points, HUD, etc. things that you wouldnt want to scroll. Maybe instead of doing that i could modify your code, assuming we gave all actors a private boolean like the following
    public void scroll() 
    {  
        int speed = 3;  
        for(Actor a : getObjects(null))  
            if (a.canScroll())
                {
                    a.setLocation(a.getX() - speed, a.getY());  
                 }
    }  
edit - just realized this doesnt work since you cant add methods to actor. guess we'd still need to make a subclass of actor that would include all the scrolling items?
AJTJ AJTJ

2012/5/7

#
Thanks for the replies i will look further into it! :)
You need to login to post a reply.