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

2018/4/14

Scrolling world

AAA355 AAA355

2018/4/14

#
I am making a crossy road style game and i have no idea how to get the world to scroll when my character hits the edge. please help. world code:
public class MyWorld extends World
{
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(1000, 800, 1); 
        prepare();
    }
    
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) == 0)
        {  
            addObject(new RedCar(), getWidth() -930,1);  
        }
        if (Greenfoot.getRandomNumber(100) == 0)  
        {  
            addObject(new GreenCar(), getWidth() -830 , 795);  
        }
        if (Greenfoot.getRandomNumber(100) == 0)  
        {  
            addObject(new BlueCar(), getWidth() -880 , 1);  
        }
        if (Greenfoot.getRandomNumber(100) == 0)  
        {  
            addObject(new Ambulance(), getWidth() -680 , 1);  
        }

        
        
       
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
         Lizard lizard = new Lizard();
         addObject(lizard,-900,400);

        Door door = new Door();
        addObject(door,922,759);
        lizard.setLocation(23,398);
    }
}
code for character:
public class Lizard extends Actor
 {
    GreenfootImage image1 = new GreenfootImage("LizardDown.png");
    GreenfootImage image2 = new GreenfootImage("LizardLeft.png");
    GreenfootImage image3 = new GreenfootImage("LizardBack.png");
    GreenfootImage image4 = new GreenfootImage("LizardRight.png");
    /**
     * Act - do whatever the Lizard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Move();
        Hit();
    }    
    public void Move()
    {
        if (Greenfoot.isKeyDown("down"))
        {
            if (Greenfoot.getKey()!=null)
            {
               down(); 
               setImage(image1);
            }
        }
        if (Greenfoot.isKeyDown("up"))
        {
            if (Greenfoot.getKey()!=null)
            {
               up();
               setImage(image3);
            }
        }
        if (Greenfoot.isKeyDown("right"))
        {
            if (Greenfoot.getKey()!=null)
            {
               right(); 
               setImage(image4);
            }
        }
        if (Greenfoot.isKeyDown("left"))
        {
            if (Greenfoot.getKey()!=null)
            {
               left(); 
               setImage(image2);
            }
        }
    }
    
    public void down()
    {
        setLocation(getX(), getY() +50);
    }
    public void up()
    {
        setLocation(getX(), getY() -50);
    }
    public void right()
    {
        setLocation(getX() + 50, getY());
    }
    public void left()
    {
        setLocation(getX() - 50, getY());
    }
    
    public void Hit()
    {
        if ( isTouching(RedCar.class) )   {
            removeTouching(Lizard.class);
            if (isTouching(RedCar.class) )setLocation(23, 398);
            // Greenfoot.setWorld(new Level2());
        }
        if ( isTouching(Ambulance.class) )   {
            removeTouching(Lizard.class);
            if (isTouching(Ambulance.class) )setLocation(23, 398);
            // Greenfoot.setWorld(new Level2());
        }
        if ( isTouching(GreenCar.class) )   {
            removeTouching(Lizard.class);
            if (isTouching(GreenCar.class) )setLocation(23, 398);
            // Greenfoot.setWorld(new Level2());
        }
        if ( isTouching(BlueCar.class) )   {
            removeTouching(Lizard.class);
            if (isTouching(BlueCar.class) )setLocation(23, 398);
            // Greenfoot.setWorld(new Level2());
        }
        if ( isTouching(Door.class))   {
            removeTouching(Door.class);
            //Level2.backgroundMusic.stop();
            Greenfoot.setWorld(new Level2());
        }
        if ( isTouching(Door2.class))   {
            removeTouching(Door.class);
            //Level2.backgroundMusic.stop();
            Greenfoot.setWorld(new Level3());
        }
        if ( isTouching(Door3.class))   {
            removeTouching(Door.class);
            //Level2.backgroundMusic.stop();
            Greenfoot.setWorld(new Level4());
        }
    }
    
}
danpost danpost

2018/4/14

#
AAA355 wrote...
I am making a crossy road style game and i have no idea how to get the world to scroll when my character hits the edge. please help. << Code Omitted >>
Please refer to my Scrolling Tutorial scenario.
AAA355 AAA355

2018/4/15

#
thanks heaps, i followed the tutorial and there are a few errors in the code but i copied it exactly,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    public static final int WIDE = 1000;
    public static final int HIGH = 800;
    
    Lizard scroller;
    Actor scrollActor;
   
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(WIDE, HIGH, 1, false);
        GreenfootImage bg=new GreenfootImage("bg.png");
        int bgWide=bg.getWidth();
        int bgHigh=bg.getHeight();
        scroller =newScroller(this,bg,bgWide, bgHigh);
        scrollActor=new Lizard();
        addObject(scrollActor, bgWide/2,bgHigh/2);
        scroll();
        prepare();
    }
   
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) == 0)
        {  
            addObject(new RedCar(), getWidth() -930,1);  
        }
        if (Greenfoot.getRandomNumber(100) == 0)  
        {  
            addObject(new GreenCar(), getWidth() -830 , 795);  
        }
        if (Greenfoot.getRandomNumber(100) == 0)  
        {  
            addObject(new BlueCar(), getWidth() -880 , 1);  
        }
        if (Greenfoot.getRandomNumber(100) == 0)  
        {  
            addObject(new Ambulance(), getWidth() -680 , 1);  
        }
        if(scrollActor!=null)scroll();

        
        
       
    }
    private void scroll()
    {
       int dsx=scrollActor.getX()-WIDE/2;
       int dsy=scrollActor.getY()-HIGH/2;
       scroller.scroll(dsx,dsy);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
         Lizard lizard = new Lizard();
         addObject(lizard,-900,400);

        Door door = new Door();
        addObject(door,922,759);
        lizard.setLocation(23,398);
    }
}
there is an error on line 25 saying cannot find symbol and an error on line 60 also saying cannot find symbol..
danpost danpost

2018/4/15

#
AAA355 wrote...
thanks heaps, i followed the tutorial and there are a few errors in the code but i copied it exactly, << Code Omitted >> there is an error on line 25 saying cannot find symbol and an error on line 60 also saying cannot find symbol..
Well, not exactly. Also, the errors should be on lines 27 and 62. Line 27 should have a space between "new" and "Scroller" and line 14 should be:
Scroller scroller;
since you want scroller to refer to a Scroller object. Then, since your Lizard object was already added to the world before calling the prepare method, you need to remove lines 71, 72 and 76, as shown above (unless you want another lizard in your world that the view does not stay on).
You need to login to post a reply.