
Scrolls either independent of any actor or by following an actor adjust position of all actors (that can move with 'setLocation')įor (Object obj : world.getObjects(null))ĪtLocation(actor.getX()-dsx, actor.A support superclass for an infinite scrolling world Ĭapable of horizontal, vertical or universal scrolling Hold.drawImage(scrollImage, wide-imageX, high-imageY) Hold.drawImage(scrollImage, -imageX, high-imageY) Hold.drawImage(scrollImage, wide-imageX, -imageY) get near-zero starting positions for drawing 'scrollImage' Int imageY = scrolledY*world.getCellSize() Int imageX = scrolledX*world.getCellSize() create working variables of scroll positions ScrollImage = new GreenfootImage(wide*world.getCellSize(), high*world.getCellSize()) create an image as large as scrolling area tiled, if needeed Public Scroller(World viewWorld, GreenfootImage image, int wide, int high) * the given value must be at least equal to the height of 'viewWorld' and

* high the height of the visible area encompassed through scrolling * is given in world cells (not in pixels) * the given value must be at least equal to the width of 'viewWorld' and * wide the width of the visible area encompassed through scrolling * image the background image that will be tiled, if needed, to fill the scrolling area * If 'image' is null, the background will not change * If 'image' is smaller than the given total scrolling area, it will be tiled * This constructor is for a limited scrolling world Scroll(0, 0) // sets initial background image Public Scroller(World viewWorld, GreenfootImage image) * image the background image that will be tiled, if needed, and wrap with scrolling * viewWorld the world that scrolling will be performed on * If 'image' is null, the background will not change else the given image is wrapped * This constructor is for an unlimited scrolling world Private int wide, high // if limited, dimensions of scrolling area else of image to wrap Private int scrolledX, scrolledY // current scrolled distances Private boolean limited // flag to indicate whether scrolling is limited or not Private GreenfootImage scrollImage // scrolling image * It is the responsibility of the World object that creates a Scroller object to determine when * are removed from the world if no longer needed when out of view will help to prevent lag, * should be unbounded, allowing actors to move beyond the visible area. * size of the world that is visible) smaller can help in CPU expense, also. * image, the smaller that background image to be tiled, the better. For unlimited scrolling using a background * probably worse than having the background scroll). * it is better not to have a scrolling background image (having an Actor for the background is Because image manipulation can hog up CPU time, it is important to remember that * one for unlimited scrolling and one for limited scrolling. * DESCRIPTION: This is a support class for a scrolling world.

#Scrolling world greenfoot source code how to#
Now we want to know how to add objects like a healthbar to the scrolling world so that they scroll too? We have implemented all your solutions and it works great. MyImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight ) MyImage.drawRect(0, 0, healthBarWidth +1, healthBarHeight + 1) SetImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2)) * the 'Act' or 'Run' button gets pressed in the environment. * Act - do whatever the HealthBar wants to do.

Int pixelsPerHealthPoint = (int)healthBarWidth/health * Write a description of class Healthfor here. Hello,now we have a new problem, they are in all lines, where "scroller.scroll" is. Int signX=(int)Math.signum(dsx),signY=(int)Math.signum(dsy) If(scrollActor.getY()hiY)dsy=scrollActor.getY()-hiY If(scrollActor.getX()hiX)dsx=scrollActor.getX()-hiX If(Greenfoot.mouseClicked(this))scrollActor=null If(scrollActor!=null)scroll() else keyScroll() GreenfootImage image=new GreenfootImage("background") * Constructor for objects of class ActorScrollWorld. Public class OpenScrollWorld extends World Import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
