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

2022/2/9

Scrolling World

1
2
3
danpost danpost

2022/3/1

#
jdkdoen05 wrote...
Can we make a limit for the world? The school pc are trash and the performence isnt very well!
See line 47 of the Scroller class.
jdkdoen05 jdkdoen05

2022/3/2

#
ok we have already tried to change everything but it doesn't work
danpost danpost

2022/3/2

#
jdkdoen05 wrote...
ok we have already tried to change everything but it doesn't work
To be sure, the Scroller class is not meant to be added to, remove from or modified in any way. Show your world class codes for review.
jdkdoen05 jdkdoen05

2022/3/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * CLASS:OpenScrollWorld
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OpenScrollWorld extends Levels
{
    public static final int WIDE=1000;
    public static final int HIGH=866;
    Scroller scroller;
    Actor scrollActor;
    HealthBar healthbar = new HealthBar();
    Health_Text health_text = new Health_Text();
    static GreenfootSound music = new GreenfootSound("GameMusic.wav");
    /**
     * Constructor for objects of class ActorScrollWorld.
     * 
     */
    public OpenScrollWorld()
    {    
        GreenfootImage image=new GreenfootImage("Clowd.png");
        scroller=new Scroller(this,image);
        scroller.scroll(0,0);
        prepare();
        
    }
    
    public void act()
    {
        if(scrollActor!=null)
        {
            scroll();
        }
        else 
        {
            keyScroll();
        }
        music.play();
    }
    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=15;
        scroller.scroll(dsx*rate,dsy*rate);
    }
    private void scroll()
    {
        int loX=200;
        int hiX=WIDE-200;
        int loY=200;
        int hiY=HIGH-200;
        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=15;
        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);
    }
}
Here is the code for the world
danpost danpost

2022/3/3

#
jdkdoen05 wrote...
Can we make a limit for the world?
Line 25 creates a Scroller object of the wrong type. That is, you are using the wrong constructor of the class. Add two int arguments to the list of parameters (on line 25) for the width and height of the total scrolling area.
jdkdoen05 jdkdoen05

2022/3/3

#
Can you give me an example ?
danpost danpost

2022/3/3

#
jdkdoen05 wrote...
Can you give me an example?
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
scroller=new Scroller(this,image,imageWidth,imageHeight);
or just place the dimension required in the constructor call. For example:
scroller=new Scroller(this,image,2000,600);
If dimensions given is larger than image, the image will be tiled onto the background.
You need to login to post a reply.
1
2
3