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

2013/1/7

2D sidescroll help

Sergio Sergio

2013/1/7

#
Hey guys, I have a problem which I'll try to explain as good as possible. I'm creating a 2D side scroll helicopter rescue game. The helicopter always stays in the middle of the screen. The only problem is I don't always want it to! When I reach either side of the map I want the scrolling to stop and the helicopter to fly either left or right, depending on which side you are, and no longer staying at the middle. Now I have several objects in the game, like houses, trees, water etc. and all those objects move when the helicopter is moving to simulate as if you are actually flying, however the helicopter doesn't move, everything else moves. The problem is how to check if you're at the far left side or the far right side, making all other objects stop and stuff are not the problem but simply how to check if you're at the end and passing it to all actors. Since the helicopter always stays in the middle it's really hard to do. Hope it's clear enough for you!
danpost danpost

2013/1/7

#
Take a look at the code of the 'SWorld' class of my Scrolling SuperWorld scenario. It is well documented to explain how everything was done. (also 'like' the scenario if you find it helpful).
Sergio Sergio

2013/1/8

#
Thanks really helpful so far, I liked your scenario! I ave 1 question though, With your code when you start te game it already is scrolled to the right a bit, I want it to start at the far left though. I searched but can't find it, figured it would have something to do with the AMT_X in scrollBackground() but haven't got any further!
danpost danpost

2013/1/8

#
Actually, the actor is centered in the world and the image of the background is centered in the world. Moving the actor in the constructor of your sub-class of SWorld and calling 'super.act;' may allow starting your character at a different location.
Sergio Sergio

2013/1/8

#
I'm quite new to programming so forgive me, but I do not fully understand the super.act part, please explain?
danpost danpost

2013/1/8

#
In your world constructor, after everything is placed in the world, relocate the actor to where you want him to start and call the act method in 'SWorld'. That should initialize everything the way you want. Adding the 'super' keyword before a method call ensures that the method located in the super-class is executed (especially needed if the method has been over-ridden in the current class, as the 'act' method often is).
Sergio Sergio

2013/1/8

#
Yes but there is the problem, I don't want it to be placed after all objects have been created because nog my whole level is not the way I intended it to, since x=0 does not mean it's on the very left but it's left of the middle. When I place an object at x=0 I want it to be at the far left side of the world.
danpost danpost

2013/1/8

#
Then relocate and scroll after setScrollingBackground and before adding the other objects.
Sergio Sergio

2013/1/8

#
So what's wrong here:
public MyWorld()
    {    
        super(750, 500, 1, 1500); // scroll world constructor call; last parameter is scroll width
        // in the following statement, the main actor is placed in the center of the window
        setMainActor(new helicopter(), 100, 342); // the int parameters are centered window x and y ranges
        // to start the main actor elsewhere
        super.act();
       // mainActor.setLocation(100, 342);
       addObject(new Helikopterplatform(), 162, 446);
        
        addObject(new Drooggebied(), 164, 459);
        
        addObject(new Water(), 890, 474);
        
        addObject(new Boom(), 440, 398);
        
        addObject(new Boom(), 660, 398);
        
        addObject(new Boom(), 880, 398);
        
        addObject(new Boom(), 1100, 398);
        
        addObject(new Boom(), 1320, 398);
        
        addObject(new Villa(), 550, 474);
        
        addObject(new Villa(), 770, 474);
        
        addObject(new Villa(), 990, 474);
        
        addObject(new Villa(), 1210, 474);
        GreenfootImage bg = new GreenfootImage("Achtergrondlevel1.png");
        setScrollingBackground(bg); // set the scolling background image

}
danpost danpost

2013/1/8

#
danpost wrote...
Then relocate and scroll after setScrollingBackground and before adding the other objects.
The key word was after.
You need to login to post a reply.