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

2013/12/29

Unlimited spawning and static world

chrl98 chrl98

2013/12/29

#
Sorry guys but I'm currently stuck on these two issues: 1) How should I program my scenario so that when the game starts, EnemyTruck objects will spawn repeatedly in the game world and continue to do so even when the player has reached the edge of the world and continued to travel to the rest of the game world (background updates automatically when player is at edgeofworld). Also, how do I make these new EnemyTruck objects have access to the same methods, and is there any method other than programming these EnemyTruck Objects to appear at specific locations in the World class, which is what I have currently done? 2) How can I lock worlds, so that when my game loads, the Start Menu world loads on top of the game world (this has already been achieved), and the functionality of the game world is locked until the start menu has been deactivated by the mouse click of the player . The player will not be able to move, and I think this has someone to do with worlds being given priority with the command "setPaintOrder(...)"
danpost danpost

2013/12/29

#
1) use the 'getRandomNumber' method of the Greenfoot class to (a) determine when to spawn and (b) determine where to spawn (this code should go in the world class 'act' method or a method it calls) 2) 'setPaintOrder' only deals with actors of the current world, not with different worlds. You should consider using different world objects for the start menu world and the game world. You can have your game world created and started first; then, have it create and set active your menu world from within its constructor or by using the 'started' method as a trigger. You can either re-create a new game world and make adjustments to it (depending upon selections in the start menu world) or return to the initial world (this would require the start menu world to hold a reference to the game world) after making adjustments to it. The game world will not run as long as the start menu world is active. BTW, my response to (2) was with the presumption that your 'start menu world' was actually an actor. If you were having a problem with your game world running while the 'start menu world' was showing, then this would most probably be why.
chrl98 chrl98

2013/12/30

#
Can you please advise me on how to lock the worlds by using the 'start' method? Thanks.
danpost danpost

2013/12/30

#
To your game world class:
// add instance field
private Boolean ranStartMenu;
// the 'started' method
public void started()
{
    if (ranStartMenu) return;
    ranStartMenu = true;
    Greenfoot.setWorld(new StartMenuWorld(this));
}
In your StartMenuWorld:
// add instance field
private World world;
// the constructor
public StartMenuWorld(World w)
{
    world = w;
    // construct world
}
where, 'construct world' would be things like setting the background image if necessary and adding objects into the world,
chrl98 chrl98

2014/1/3

#
Hi Danpost, Sorry for the late response, since I have been focusing on other elements of the game. I've tried out the code that you've given me, but it results in a NullPointerException. Have I done something wrong? Thanks. Returning a NullPointerException due to line 108 in code for Game World. Game World:

import greenfoot.World;
import greenfoot.GreenfootImage;
import greenfoot.Greenfoot;
import greenfoot.MouseInfo;
import greenfoot.UserInfo;
import java.util.List;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.Font;

/**
 * Write a description of class Terrain here.
 * 
 * @author Taylor Born
 * @version May 2012
 */
public class TerrainWorld extends World
{
    public static TerrainWorld main;
    public static AmmoCounter ammocounter = new AmmoCounter();
    public static ScoreCounter counter = new ScoreCounter();
    private Point lastMouse = new Point(getWidth() / 2, getHeight() / 2);
    private GreenfootImage grass = new GreenfootImage("Grass.jpg");
    //Timer timer = new Timer();
    private Unit player;
    private static int score = 0;
    //private Sector[][] sectors = new Sector[3][3];
    private Sector[] sectors = new Sector[5];
    private double screenX, screenY;
    private int saveIt;
    private Message saveMessage = new Message("Saving..");
    private AmmoCounter theAmmoCounter;
    private ScoreCounter theScoreCounter;
    private int timer = 0;
    private Boolean ranStartMenu;
    
    public TerrainWorld()
    {
        //
        super(600, 400, 1, false);
        main = this;
        CodeKeyUser.enter();
        Greenfoot.start();
        addObject(new AmmoCounter(), 120, 30);
        AmmoCounter ammocounter = new AmmoCounter();
        addObject(new ScoreCounter(),120,80);
        ScoreCounter scorecounter = new ScoreCounter();
        addObject(new AmmoSign(), 60, 30);
        AmmoSign ammosign = new AmmoSign();
        addObject(new LifeCounter(), 120, 55);
        LifeCounter lifecounter = new LifeCounter();
        addObject(new LiveSign(),60, 55);
        LiveSign livesign = new LiveSign();
        addObject(new ScoreSign(),60,80);
        ScoreSign scoresign = new ScoreSign();
        addObject(new StoreButton(), 560,30);
        StoreButton storebutton = new StoreButton();
        //addObject(new Timer(),120, 80);
        //Timer clock = new Timer();
        //timer.setText("Time left"+ (timer/60));
        //addObject(new CDSign(), 60, 80);
        //CDSign cdsign = new CDSign();
        addObject(new Play(), 300, 140);
        Play playbutton = new Play();
        addObject(new Clock(true, true, 60, "Time left:"), 85, 370);
        addObject(new CreditsButton(), 300, 280);
        CreditsButton creditsbutton = new CreditsButton();
        addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck = new EnemyTruck();
        addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck2 = new EnemyTruck();
                addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck3 = new EnemyTruck();
                addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck4 = new EnemyTruck();
                addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck5 = new EnemyTruck();
                addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck6 = new EnemyTruck();
                addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck7 = new EnemyTruck();
                addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        EnemyTruck enemytruck8 = new EnemyTruck();
        addObject(new InstructionsButton(), 300, 210);
        InstructionsButton instructionsbutton = new InstructionsButton();
        //if (UserInfo.isStorageAvailable() && UserInfo.getMyInfo().getUserName().equals("bourne"))
        newGame();
        setBackground(pic);
        prepare();
        //List objectslookingfor = getObjects(EnemyTruck.class);  
        //if (objectslookingfor.size() == 0) 
        //{
          // addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        //}
    }
    public AmmoCounter getAmmoCounter()
    {
        return theAmmoCounter;
    }
    public ScoreCounter getScoreCounter()
    {
        return theScoreCounter;
    }
    public void started()
    {
        if (ranStartMenu) return;
        {
            ranStartMenu = true;
            Greenfoot.setWorld(new MenuWorld(this));
        }
    }
    private void prepare()
    {
    }

Start Menu World:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.World;
import greenfoot.GreenfootImage;
import greenfoot.Greenfoot;
import greenfoot.MouseInfo;
import greenfoot.UserInfo;
import java.util.List;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.Font;

/**
 * Write a description of class Menu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MenuWorld extends World
{
    private boolean Startgame = false;
    private boolean clicked = false;
    private GreenfootImage background;
    private World world;

    public MenuWorld(World w)
    {    
        super(600, 400, 1,false);
        addObject(new Play(), 230, 160);
        Play playbutton = new Play();
        addObject(new CreditsButton(), 300, 30);
        CreditsButton creditsbutton = new CreditsButton();
        addObject(new InstructionsButton(), 370, 30);
        InstructionsButton instructionsbutton = new InstructionsButton();
        world = w;
    }
    private void prepare()
    {
    }
    public void act() 
    {
        Greenfoot.start();
        //Greenfoot.stopWorld(new TerrainWorld());
        if(Greenfoot.mouseClicked(this))
        {
           Startgame = true;
           updateImage();
           Greenfoot.setWorld(new TerrainWorld());
           removeObjects(getObjects(Play.class));
           removeObjects(getObjects(CreditsButton.class));
           removeObjects(getObjects(InstructionsButton.class));
        }
        if(Greenfoot.mouseClicked(Play.class))
        {
           Greenfoot.setWorld(new TerrainWorld());   
        }
    }    
    public void stop()
    {
        Greenfoot.setWorld(new TerrainWorld());
    }
    public void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
    }
}

danpost danpost

2014/1/3

#
In the world class, change line 37 to:
private boolean ranStartMenu;
and remove lines 109 and 112.
You need to login to post a reply.