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

2013/12/9

Background Scroll

1
2
GaspTW GaspTW

2013/12/9

#
Hey guys, For my game I want my Actor to move to the right before it hits the wall; I want my background to scroll along. So basically a background code for scrolling to the right. I made my own image: 10000 x 600 px My playing field is 600 x 600 I tried some codes but none of them works properly. Don't know how to translate it into my own Actors and BG. Thanks a lot, Gasp
SPower SPower

2013/12/9

#
There are already some engines on greenfoot.org to do that for you, this is mine: http://www.greenfoot.org/scenarios/5806
GaspTW GaspTW

2013/12/9

#
Thanks for replying. I can't figure out how to implement the code into my own project. I'm just looking for an background which scrolls automatically to the right if the character moves to the right. An code for scrolling an handmade background horizontally. This code can moves the actor almost anywhere..
SPower SPower

2013/12/9

#
It does what you want though: it scrolls the background and moves all the actors along with it.
GaspTW GaspTW

2013/12/9

#
Okay I will try it again :) I let you know, thnx for helping!
GreenHouse GreenHouse

2013/12/9

#
Demo Engine: http://www.greenfoot.org/scenarios/10094 See 'Endless World' in the documentation, it is easy too use ;)
danpost danpost

2013/12/10

#
I just uploaded a fairly simple side-scrolling scenario that would probably work for you. It is here.
GaspTW GaspTW

2013/12/10

#
danpost, your scenario seems fine. I looked at the code, you added some comments in it, but I still don't know exactly what to 'copy' or use. My scenario is similar to the game copter. I used my own background, and will make my own collision parts. The background in the posted scenario scrolls to the right as soon as the buggy almost hits the wall. I want my character to start on the left. And as soon as he hits the middle or just over it, the background scrolls. As you can see, I'm a starter so it's very hard to see a code and determine which parts are useful...
danpost danpost

2013/12/10

#
You would need the entire Scroll class, and in your main world, which in mine, it is the View class, an instance field to hold a Scroll world object, your 10000x600 sized image, the line in the constructor to create the Scroll world object and assign it to the instance field, and in the act (or a method it call -- which my View class does) code to call the Scroll class method 'changeScroll' and adjust the position of all actors in your world. The code to scroll and move actors should be on the conditions that you require. In mine, I compared the x-location of the main actor to a specific x value which was close to the end of the world. You could compare your to, say, 'getWidth()*3/5' to start scrolling when your main actor has traversed 60 percent of the world.
GaspTW GaspTW

2013/12/10

#
Gonna try, thanks a lot. You will hear from me!
GaspTW GaspTW

2013/12/10

#
danpost wrote...
You would need the entire Scroll class, and in your main world, which in mine, it is the View class, an instance field to hold a Scroll world object, your 10000x600 sized image, the line in the constructor to create the Scroll world object and assign it to the instance field, and in the act (or a method it call -- which my View class does) code to call the Scroll class method 'changeScroll' and adjust the position of all actors in your world. The code to scroll and move actors should be on the conditions that you require. In mine, I compared the x-location of the main actor to a specific x value which was close to the end of the world. You could compare your to, say, 'getWidth()*3/5' to start scrolling when your main actor has traversed 60 percent of the world.
I copied the complete Scroll class. I made an instance field with scroll = new Scroll(getScrollBg(), this); In the view class I don't know how to set my own picture, in other words: if I just set image to my own picture, how do I refer to it into my own code ('your 10000x600 sized image') The line in the constructor to create the Scroll World object? If my background is scrollable I will look at positioning the actors, the last part of your comment is understandable :)
danpost danpost

2013/12/10

#
If the image is located in an image file, then:
1
2
GreenfootImage bgScroll = new GreenfootImage("/* image filename */");
scroll = new Scroll(bgScroll, this);
If you have code that creates the image, you can do it similar to my demo.
GaspTW GaspTW

2013/12/12

#
It says I need to define 'scroll' (it highlights the scroll word in the act class) Your code describes the scroll class as: public void scroll() { int ds = buggy.getX()-560; if (ds > 0) { int scrolledAmt = scroll.changeScroll(ds); for (Object obj : getObjects(null)) { Actor actor = (Actor)obj; actor.setLocation(actor.getX()-scrolledAmt, actor.getY()); } } } But how can I delete your buggy and add my own boat to it?
danpost danpost

2013/12/12

#
You should have instance fields:
1
2
Scroll scroll;
Boat boat; // your main actor
Change every occurrence of 'buggy' with 'boat' and 'Buggy' with 'Boat' (or the class name of your boat). Adjust the second line above the same (which was 'Buggy buggy;', which I have attempted to do). Also, if you do not have it yet, the first statement in your world constructor should be like this: 'super(int, int, int, false)'. The 'false' part allows your actors to move outside the viewed area.
GaspTW GaspTW

2013/12/13

#
I can compile everything without any errors! When I click on 'run' it says: java.lang.NullPointerException at ScrollWereld.scroll(ScrollWereld.java:44) at ScrollWereld.act(ScrollWereld.java:39) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at ScrollWereld.scroll(ScrollWereld.java:44) at ScrollWereld.act(ScrollWereld.java:39) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at ScrollWereld.scroll(ScrollWereld.java:44) at ScrollWereld.act(ScrollWereld.java:39) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
There are more replies on the next page.
1
2