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

2019/5/26

Boundaries and Scrolling World

Joyfu1 Joyfu1

2019/5/26

#
Hey guys, we have a scrolling world but our actors occasionally go off the screen. Is there a way we can have set parameters or boundaries? Thank you! This is the code for MyWorld
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
{
    // Defines the background image as a variable
    private Background scrollworld, scrollworld2;
    // Add healthbar as an object to the world
    private HealthBar healthbar = new HealthBar();
    private ScoreBoard score;

    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(980, 575, 1, false); 

        score = new ScoreBoard(this);
        prepare();

        // Sets order of actors in world
        setPaintOrder(Ship.class, Laser.class, Alien.class, Star.class, HealthBar.class, Background.class);

        // Adds background to world and sets it to the middle 
        scrollworld = new Background();
        addObject(scrollworld, getWidth()/2, getHeight()/2);

        // Continues the scrolling effect
        scrollworld2 = new Background(); 
        addObject(scrollworld2, getWidth() + getWidth()/2, getHeight()/2);
    }

    public void act() {
        // Allows background to move infinitely
        if (true) {
            scrollworld.scroll();
            scrollworld2.scroll(); 
        }
    }

    public HealthBar getHealthBar() {
        // Updates healthbar
        return healthbar;
    }

    private void prepare()
    {
        // Adds all objects to world
        
        WaveCounter wavecounter = new WaveCounter(score);
        addObject(wavecounter, 0, 0); 
        
        Ship ship = new Ship(score);
        addObject(ship,490,300);
        
        addObject(healthbar,100,550);
        
        BigAlien bigalien = new BigAlien();
        addObject(bigalien, 900, 100);
        BigAlien bigalien2 = new BigAlien();
        addObject(bigalien2, 900, 500);
        BigAlien bigalien3 = new BigAlien();
        addObject(bigalien3, 100, 500);
        BigAlien bigalien4 = new BigAlien();
        addObject(bigalien4, 100, 100);
    }

    
}
Super_Hippo Super_Hippo

2019/5/26

#
Show the class code of the actor which leaves the world. In your world, you don't have anything which would prevent objects from moving outside the world boundaries.
You need to login to post a reply.