Fire wrote...
How would I fix the error message with Scroller, Range, and scroll classes?
/**
* Write a description of class Scroller here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Scroller extends ImageScrollWorld
{
// instance variables - replace the example below with your own
private [b]World[/b] world;
private [b]GreenfootImage[/b] scrollImage;
private boolean limited;
private int scrolledX, scrolledY;
private int wide, high;
/**
* Constructor for objects of class Scroller
*/
public Scroller([b]World [/b]viewWorld, [b]GreenfootImage [/b]image)
{
world=viewWorld;
scrollImage=image;
if(image !=null)
{
wide=image.getWidth();
high=image.getHeight();
}
scroll(0, 0);
}
public Scroller([b]World[/b] veiwWorld, [b]GreenfootImage[/b] image, int wide, int high)
{
this.wide=wide;
this.high=high;
limited=true;
World=veiwWorld;
if(image !=null)
{
[b]srollImage[/b]=new [b]GreenfootImage[/b](wide*world.getCellSize(), high*world.getCellSize());
for(int x=0;x<wide*world.getCellSize(); x+=image.gtWidth())
for (int y=0; y<high*world.getCellSize(); y+=image.getHeight())
scrollImage.drawImage(image,x,y);
scroll(0,0);
}
}
public void scroll(int dsx, int dsy)
{
//adjust scroll amounts and scroll background image
if(limited)
{
//calculate limits of scrolling
int maxX=wide-world.getWidth();
int maxY=high-world.getHieghyt();
//apply limits to distances to scroll
if (scrolledX+dsx<0)dsx=-scrolledX;
if(scrolledX+dsx>=maxX) dsx=maxX-scrolledX;
if(scrolledY+dsy<0)dsy=-scrolledY;
if(scrolledY+dsy>=maxY)dsy=maxY-scrolledY;
//update scroll positions
scrolledX+=dsx;
scrolledY+=dsy;
//scrollbackground image
if(scrollImage!=null)
{
world.getBackground().drawImage
(
scrollImage,
-scrolledX*world.getCellSize(),
-scrolledY*world.getCellSize()
);
}
}
else //unlimited image wrapping
{
//update scroll positions
scrolledX+=dsx;
scrolledY+=dsy;
//scroll background image
if(scrollImage!=null)
{
//create working variables of scroll positions
int imageX=scrolledX*world.getCellSize();
int imageY=scrolledY*world.getCellSize();
//get near-zero starting positions for drawing 'srollImage' onto it
imageX=imageX%wide;
imageY=imageY%high;
// adjust negative values as needed
if(imageX<0)imageX+=wide;
if(imageY<0)imageY+=high;
// create image of approiate size and tile fill 'scrollImage' onto it
[b]GreenfootImage[/b] hold=new [b]GreenfootImage[/b](scrollImage);
hold.drawImage(scrollImage, -imageX, -imageY);
if(imageX>0)hold.drawImage(scrollImage,wide-imageX,-imageY);
if(imageY>0)hold.drawImage(scrollImage,-imageX,high-imageY);
if(imageX>0 && imageY>0)
hold.drawImage(scrollImage, wide-imageX, high-imageY);
// set image to Background of 'world'
world.setBackground(hold);
}
}
// adjust position of all actors (that can move with 'setLocation')
for (Object obj : world.getObjects(null))
{
[b] Actor[/b] actor=([b]Actor[/b])obj;
actor.setLocation(actor.getX()-dsx, actor.getY()-dsy);
}
}
public int getScrolledX()
{
return scrolledX;
}
public int getScrolledY()
{
return scrolledY;
}
}
import greenfoot.*;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Scroller here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Scroller
{
// instance variables - replace the example below with your own
private int MyWorldworld;
private GreenfootImage scrollImage;
private boolean limited;
private int scrolledX, scrolledY;
private int wide, high;
/**
* Constructor for objects of class Scroller
*/
public Scroller(MyWorld veiwWorld, GreenfootImage image)
{
world=veiwWorld;
scrollImage=image;
if(image!=null)
{
wide=image.getWidth();
high=image.getHeight();
}
scroll(0,0);
}
public Scroller(MyWorld veiwWorld, GreenfootImage image, int wide, int high)
{
this.wide=wide;
this.high=high;
limited=true;
world=veiwWorld;
if(image!=null)
{
scrollImage= new GreenfootImage(wide*wide.getCellSize(),high*world.getCellSize());
for (int x=0;x<wide*world.getCellSize();x+=image.getWidth())
for(int y=0; y<high*world.getCellSize();y+=image.getHeight())
scrollImage.drawImage(image,x,y);
}
}
public void scroll(int dsx,int dsy)
{
if(limited)
{
int maxX=wide-world.getWidth();
int maxY=high-world.getHeight();
if(scrolledX+dsx<0)dsx=-scrolledX;
if(scrolledX+dsx>=maxX)dsx=maxX-scrolledX;
if(scrolledY+dsy<0)dsy=-scrolledY;
if(scrolledY+dsy>=maxY)dsy=maxY-scrolledY;
scrolledX+=dsx;
scrolledY+=dsy;
if(scrollImage !=null)
{
World.getBackground().drawImage
(scrollImage,
-scrolledX*world.getCellSize(),
-scrolledY*world.getCellSize()
);
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Scroller here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Scroller
{
// instance variables - replace the example below with your own
private int MyWorldworld;
private GreenfootImage scrollImage;
private boolean limited;
private int scrolledX, scrolledY;
private int wide, high;
private World world;
/**
* Constructor for objects of class Scroller
*/
public Scroller(World veiwWorld, GreenfootImage image)
{
world=veiwWorld;
scrollImage=image;
if(image!=null)
{
wide=image.getWidth();
high=image.getHeight();
}
scroll(0,0);
}
public Scroller(World veiwWorld, GreenfootImage image, int wide, int high)
{
this.wide=wide;
this.high=high;
limited=true;
world=veiwWorld;
if(image!=null)
{
scrollImage= new GreenfootImage(wide*wide.getCellSize(),high*world.getCellSize());
for (int x=0;x<wide*world.getCellSize();x+=image.getWidth())
for(int y=0; y<high*world.getCellSize();y+=image.getHeight())
scrollImage.drawImage(image,x,y);
}
}
public void scroll(int dsx,int dsy)
{
if(limited)
{
int maxX=wide-world.getWidth();
int maxY=high-world.getHeight();
if(scrolledX+dsx<0)dsx=-scrolledX;
if(scrolledX+dsx>=maxX)dsx=maxX-scrolledX;
if(scrolledY+dsy<0)dsy=-scrolledY;
if(scrolledY+dsy>=maxY)dsy=maxY-scrolledY;
scrolledX+=dsx;
scrolledY+=dsy;
if(scrollImage !=null)
{
World.getBackground().drawImage
(scrollImage,
-scrolledX*world.getCellSize(),
-scrolledY*world.getCellSize()
);
}
}
else
{
scrolledX+=dsx;
scrolledY+=dsy;
if(scrollImage!=null)
{
int imageX=scrolledX*world.getCellSize();
int imageY=scrolledY*world.getCellSize();
imageX=imageX%wide;
imageY=imageY%high;
if(imageX<0)imageX+=wide;
if(imageY<0)imageY+=high;
GreenfootImage hold=new GreenfootImage(scrollImage);
hold.drawImage(scrollImage,-imageX, -imageY);
if(imageX>0)hold.drawImage(scrollImage,wide-imageX,-imageY);
if(imageY>0)hold.drawImage(scrollImage,-imageX,high-imageY);
if(imageX>0&&imageY>0)
hold.drawImage(scrollImage, wide-imageX, high-imageY);
world.setBackground(hold);
}
}
for(Object obj :world.getObjects(null))
{
Actor actor=(Actor)obj;
actor.setLocation(actor.getX()-dsx, actor.getY()-dsy);
}
}
public int getScrolledX()
{
return scrolledX;
}
public int getScrolledY()
{
return scrolledY;
}
}
import greenfoot.*;
/**
* CLASS: ImageScrollWorld
*
* DESCRIPTION: creates a limited (to scrolling background image size) center screen actor
* following scrolling world
*/
public class ImageScrollWorld extends MyWorld
{
public static final int WIDE = 600; // world width (viewport)
public static final int HIGH = 400; // world height (viewport)
Scroller scroller; // the object that performs the scrolling
Actor scrollActor; // an actor to stay in view
boolean roaming;
public ImageScrollWorld()
{
[u]super[/u](600, 400, 1, false); // creates an unbounded world
setPaintOrder(Player.class);
GreenfootImage bg = new GreenfootImage(100, 100); // creates an image to scroll
bg.fill();
bg.setColor(Color.LIGHT_GRAY);
bg.fillRect(2, 2, 96, 96);
bg.scale(1000, 1000);
bg.setColor(Color.DARK_GRAY);
for (int i=100; i<1000; i+=100)
{
bg.drawLine(i, 0, i, 1000);
bg.drawLine(0, i, 1000, i);
}
int bgWide = bg.getWidth(); // scrolling image width
int bgHigh = bg.getHeight(); // scrolling image height
scroller = new Scroller(this, bg, bgWide, bgHigh); // creates the Scroller object
scrollActor = new Player(); // creates the actor to maintain view on
addObject(scrollActor, bgWide/2, bgHigh/2); // add actor at center of scrolling area (wherever)
addObject[u](new Range(roaming)[/u], WIDE/2, HIGH/2);
scroll(); // sets initial background image and puts main actor in view if needed
}
public void act()
{
[u]checkEscape()[/u]; // to return to demo menu
if (Greenfoot.mouseClicked(null))
{
roaming = !roaming;
removeObjects(getObjects(Range.class));
addObject[u](new Range(roaming)[/u], WIDE/2, HIGH/2);
}
if (scrollActor != null)
{
if (roaming) scroll2(); else scroll();
}
}
// attempts scrolling when actor is not in center of visible world
private void scroll()
{
// determine scrolling offsets and scroll
int dsx = scrollActor.getX()-WIDE/2; // horizontal offset from center screen
int dsy = scrollActor.getY()-HIGH/2; // vertical offset from center screen
scroller.scroll(dsx, dsy);
}
// attempts scrolling when actor is outside roaming limits
private void scroll2()
{
// roaming limits of actor
int loX = 100;
int hiX = WIDE-100;
int loY = 50;
int hiY = HIGH-50;
// determine offsets and scroll
int dsx = 0, dsy = 0;
if (scrollActor.getX() < loX) dsx = scrollActor.getX()-loX;
if (scrollActor.getX() > hiX) dsx = scrollActor.getX()-hiX;
if (scrollActor.getY() < loY) dsy = scrollActor.getY()-loY;
if (scrollActor.getY() > hiY) dsy = scrollActor.getY()-hiY;
scroller.scroll(dsx, dsy);
}
}