import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * CLASS:OpenScrollWorld * * @author (your name) * @version (a version number or a date) */ public class OpenScrollWorld extends World { public static final int WIDE=800; public static final int HIGH=600; Scroller scroller; Actor scrollActor; /** * Constructor for objects of class ActorScrollWorld. * */ public OpenScrollWorld() { super(WIDE,HIGH,1,false); GreenfootImage image=new GreenfootImage("background"); scroller=new Scroller(); scrollActor=new Kampfjet(); scrollActor.setImage("Kampfjet Links.png"); addObject(scrollActor,WIDE/2,HIGH/2); Actor kampfjet=new Kampfjet(); kampfjet.setImage("Kampfjet Links.png"); addObject(kampfjet,WIDE/2,100); kampfjet=new Kampfjet(); kampfjet.setImage("Kampfjet Rechts.png"); addObject(kampfjet,WIDE/2,500); scroller.scroll(0,0); } public void act() { if(scrollActor!=null)scroll();else keyScroll(); if(Greenfoot.mouseClicked(this))scrollActor=null; } private void keyScroll() { int dsx=0,dsy=0; if(Greenfoot.isKeyDown("right"))dsx++; if(Greenfoot.isKeyDown("left"))dsx--; if(Greenfoot.isKeyDown("down"))dsy++; if(Greenfoot.isKeyDown("up"))dsy--; int rate=2; scroller.scroll(dsx*rate,dsy*rate); } private void scroll() { int loX=100; int hiX=WIDE-100; int loY=100; int hiY=HIGH-100; int dsx=0; int 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; int rate=3; int signX=(int)Math.signum(dsx),signY=(int)Math.signum(dsy); dsx=signX*Math.min(Math.abs(dsx),rate); dsy=signY*Math.min(Math.abs(dsy),rate); scroller.scroll(dsx,dsy); } }