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

2017/1/20

game

1
2
jacquelinereilly jacquelinereilly

2017/1/20

#
okay my final question. How do i do this, when all the objects are collected i need a window that comes saying try again or exit. it happens when the counter = 2 . there are 3 levels, so i would need to do it 3 times. do i need a new actor or world ?
Nosson1459 Nosson1459

2017/1/20

#
You can use whatever you want but for restarting you can just use a new subclass of World instead of placing actors on top of your old ones (unless you want to make a class which extends JFrame but that is a whole separate part of Java). I don't understand what you mean by "exit", where are you exiting to?
jacquelinereilly jacquelinereilly

2017/1/20

#
oh yeah that wasn't explained very well. when they collect all the objects,, so when counter =2. i just need a window that says "Continue Playing" or Exit" which then the main menu of the game will be shown again
jacquelinereilly jacquelinereilly

2017/1/20

#
i tried something, with a conditional statement, but it didn't work, lots of errors. anyone have ideas?
Nosson1459 Nosson1459

2017/1/20

#
Make a new subclass of World and in that constructor put in two button Actors for those options.
jacquelinereilly jacquelinereilly

2017/1/20

#
okay, how would I get it to appear when they level is done? the level is done when the objects are collected, so when counter is equal to 2
Nosson1459 Nosson1459

2017/1/20

#
Do you have coding that checks for the end of the game? If you do then insert this into the if:
Greenfoot.setWorld(new EndWorld());
If you don't, then you can check if counter is two OR in you World you can do
if (getObjects(Objects.class).isEmpty() || counter.value == 2) // the counter.value won't necessarily work for you, it's just an example
{
    Greenfoot.setWorld(new EndWorld());
}
jacquelinereilly jacquelinereilly

2017/1/20

#
i don't have anything that checks for the end of the level. I did your second option, and when the level is done nothing happens. The counter doesn't work for the second option . it says incompatible types: int and Counter
danpost danpost

2017/1/20

#
When doing the second option, you need to replace 'Object' with the class you are checking to not be in the world (either FormulaBottle or Plankton). You probably want to actually do both, ORing them (if one or the other) using the '||' operator.
jacquelinereilly jacquelinereilly

2017/1/20

#
yes i did this
if (getObjects (FormulaBottle.class).isEmpty() )
    {
        Greenfoot.setWorld (new EndOfLevel() );
    }
danpost danpost

2017/1/20

#
jacquelinereilly wrote...
yes i did this < Code Omitted >
Show where you placed the code (the class).
jacquelinereilly jacquelinereilly

2017/1/20

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

/**
 * This class Level2 here displays the second level for the user to play.
 * 
 * Jacqueline Reilly
 * January 18, 2017
 */
public class Level2 extends World
{
    //Instance for counters
    Counter counter = new Counter ();
    PlanktonCounter PCounter = new PlanktonCounter ();
    /**
     * Constructor for objects of class Level2.
     * 
     */
    public Level2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super (950, 700, 1); 
        
        //Adding counter
        addObject(counter, 91, 137);
        addObject (PCounter, 97, 186);
        
        //Making background fit the screen
        GreenfootImage Bg = new GreenfootImage ("background.jpg");
        Bg.scale (getWidth (), getHeight ());
        setBackground (Bg);
        
        //Adding the platforms 
        Platform platform = new Platform ();
        addObject (platform,234, 618);
        Platform platform1 = new Platform ();
        addObject (platform1, 655, 518);
        
        //Adding characters
        addObject (new MrKrabs(), 210, 500);
        addObject (new Plankton(), 78, 476);
        
        //Adding object for Mr. Krabs to collect
        KrabbyPatty krabbyPatty = new KrabbyPatty ();
        addObject (krabbyPatty, 393, 481);
        KrabbyPatty krabbyPatty1 = new KrabbyPatty ();
        addObject (krabbyPatty1, 601, 357);
        
        //Adding object for Plankton to collect
        FormulaBottle bottle = new FormulaBottle ();
        addObject (bottle, 293, 330);
        FormulaBottle bottle1 = new FormulaBottle ();
        addObject (bottle1, 825, 351);
        
        //

    
    
    if (getObjects(FormulaBottle.class).isEmpty() )
    {
        Greenfoot.setWorld (new EndOfLevel() );
    }
}
    
    public Counter getCounter(){
        return counter; 
    }
    
    public PlanktonCounter getPlankton() {
     return PCounter;
    }
}
danpost danpost

2017/1/20

#
It does not do any good to place it in the constructor of the class, in code which is executed before the world becomes active. The code needs to be in an 'act' method:
public void act()
{
    // code here
}
jacquelinereilly jacquelinereilly

2017/1/20

#
so it shouldn't be in the world ? it has to be in an actor?
danpost danpost

2017/1/20

#
jacquelinereilly wrote...
so it shouldn't be in the world ? it has to be in an actor?
No. It should be in the world. The World class has an act method, also. It is just not in the template that is given when you create a new subclass of World.
There are more replies on the next page.
1
2