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

Report as inappropriate.

BenMcBen
BenMcBen presents ...

2014/10/26

ScrollingWorld showcase

You can use the ScrollingWorld class provided with this project to make scrolling worlds. The AppleWorld that the project starts up with is a simple example.

Use the arrow keys to scroll around and check out all the drifting apples. If you'd like to use this code in your own project, check out the comments in the code for the ScrollingWorld class, because there are a few idiosyncrasies about the way it works.


Questions I suspect you might have:

Q: Is this a repost of danpost's Scrolling SuperWorld?
A: No. This was created independently of danpost's project. It's kind of similar, except probably not as good.

Q: Is this adaptable to [x kind of project]?
A: Yes! The ScrollingWorld is a very general class. Here, we only see it used to examine apples drifting across a grid of smiley faces in space, but it could be used in any kind of project: sidescrolling, 2D RPG / adventure / exploration, arcade, etc. There is no "ScrollingActor" -- any kind of actor you wish to use can be put in a ScrollingWorld. Also, scrolling isn't necessarily controlled by the arrowkeys -- that's just for the purpose of this example. Normally the scrolling would be controlled by code.

Q: What about something where you have to click on things?
A: Sorry, no. At least not yet. Since the actors aren't *actually* in the world -- just being *drawn* to the world, while being *stored* in *another* world that's *technically* not currently on-screen -- using mouse events will give inaccurate results at best and no results at worst. I have something in mind that might be able to fix it, but that will happen later.

Q: Can the background scroll?
A: Nope. Making a background that big would be a huge memory sink. Besides, as long as you have enough actors to build a coherent world, a motionless background hopefully shouldn't be too much of an issue. I might address this later as well.

Q: Why aren't my actors scrolling?
A: Read the comments in the source code for ScrollingWorld. You can't just add actors directly into the world -- well, you can, but they're just going to stay on the screen. You have to add actors into the world that dwells *within* the world. You can access the world that lives in the world with getUnderlying().

Q: What do I do if my question isn't listed?
A: Leave a comment below and I'll be sure to get back to you!

3904 views / 24 in the last 7 days

Tags: game simulation demo with-source example scrolling code scroll source cabrillo

open in greenfoot
Your browser does not support the canvas tag.
A new version of this scenario was uploaded on Mon Oct 27 01:33:36 UTC 2014 UPDATE: Moved updateBackground() to the end of the ScrollingWorld's act cycle so that actors can display images or text to the foreground. Also changed the way the act cycle works to allow for the possibility of actors being removed from the world while the act cycle is running.
danpostdanpost

2014/10/29

From the information I have gathered from your 'Questions I suspect you might have' section, I would think that this works in a way similar to my 'PIP Actor class' scenario. Am I correct?
BenMcBenBenMcBen

2014/10/29

Just took a look at your PIP scenario's source. Seems so! Very similar, actually. List of similarities: - We both use a world to display the state of another world. - We both have to draw that world with our own code, since the WorldHandler class is part of the "core" package and therefore has no source code available, so we don't know what method we could use to get the complete image of the world, including the actors. - In the process of drawing the world, we both thought to use the world's paint order. We both had to work around the fact that the World's access method to its paint order is package-protected. Your workaround was to include the paint order as a parameter to the PIP's constructor, whereas mine was to override setPaintOrder in the inner world class so that it updates the stored paint order in the outer world. - We both had to make it so that the outer world's act method made the inner world and all its actors act as well. It's actually kind of kooky how similar our projects are.
A new version of this scenario was uploaded on Thu Oct 30 18:21:42 UTC 2014 Updated so that if actors turn, the sprites displayed for them will be updated accordingly. Thanks to danpost's PIP scenario for making me realize mine didn't do this. I hadn't even thought about it until I saw his.

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.