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

2015/1/29

game levels

javaisanart javaisanart

2015/1/29

#
im making a game for my java class and ive asked for help multiple times the farthest ive gotten is the different level subclasses of the main world (in theory) i havent written any code yet i know i need to set up an internal counter and make it with the counter; or something but as of the if statement making it switch to the next world once the counter reaches 0
danpost danpost

2015/1/29

#
javaisanart wrote...
im making a game for my java class
obviously not, if you haven't written any code yet
and ive asked for help multiple times
not here you haven't
the farthest ive gotten is the different level subclasses of the main world (in theory)
maybe; but, there are many ways to create levels; depends on what is different between the levels
i know i need to set up an internal counter and make it with the counter; or something but as of the if statement making it switch to the next world once the counter reaches 0
I would not worry about the counter until you have created a working world (or worlds) to begin with.
javaisanart javaisanart

2015/1/29

#
danpost wrote...
javaisanart wrote...
im making a game for my java class
obviously not, if you haven't written any code yet
and ive asked for help multiple times
not here you haven't
the farthest ive gotten is the different level subclasses of the main world (in theory)
maybe; but, there are many ways to create levels; depends on what is different between the levels
i know i need to set up an internal counter and make it with the counter; or something but as of the if statement making it switch to the next world once the counter reaches 0
I would not worry about the counter until you have created a working world (or worlds) to begin with.
i meant i havent written any code for the level system yet.... but ive started. i have it now as to where once the number of enemies in the world reaches 0 it switched to a new world labled "Level2" with its own spawn of enemies something like
if (Enemy == 0)
{
        Greenfoot.setWorld(new Level2());
}
if that would even work
javaisanart javaisanart

2015/1/29

#
import greenfoot.*;

public class Level1 extends World
{
    static int totalEnemies = 10;
    public Level1()
    {    
        super(1100, 555, 1); 
        addObject ( new Enemy(), 150, 40);
        
        addObject ( new Enemy(), 150, 140);

        addObject ( new Enemy(), 150, 240);

        addObject ( new Enemy(), 150, 340);

        addObject ( new Enemy(), 150, 440);

        addObject ( new Enemy(), 50, 40);

        addObject ( new Enemy(), 50, 140);

        addObject ( new Enemy(), 50, 240);

        addObject ( new Enemy(), 50, 340);

        addObject ( new Enemy(), 50, 440);
       
        
        addObject ( new Shooter(), 950, 290);
        if (totalEnemies == 0)
        {
            Greenfoot.setWorld(new Level2());
        }
    }
}
my code so far but when it reaches 0 enemies nothing happens
danpost danpost

2015/1/30

#
javaisanart wrote...
< Code Omitted > my code so far but when it reaches 0 enemies nothing happens
The code block starting at line 6 and ending at line 35 is your world constructor. This code is executed when the world is created (during compiling or resetting of the scenario. It will never run after the world is created; so, checking the value of 'totalEnemies', which is '10' when the world is created, will never be zero at line 31. You do not need a field to track the number of enemies in the world as you can use this:
if (getObjects(Enemy.class).isEmpty())
to see if any Enemy actors are in the world. This check is something that needs to be done constantly, so the code (lines 31 through 34) must be in a block that is repeatedly called -- the 'act' method of the Level1 class.
javaisanart javaisanart

2015/1/30

#
so i tried putting in something like this
public void act()
{
        if (getObjects(Enemy.class).isEmpty())
        {
                  Greenfoot.setWorld(new Level2());
         }
}
but when i put in the if statement the script kinda glitches and makes it so no matter how many or where i put my brackets its an illegal start
javaisanart javaisanart

2015/1/30

#
nevermind got it
javaisanart javaisanart

2015/1/30

#
now i just need a way to make a HUD showing the level you are on etc.
danpost danpost

2015/1/30

#
You can either use the 'showText' method of the World class or create and add an actor into the world whose image relays the information to the user. An object reference field is usually created when using an actor. For example:
// fields
private Actor levelHud; // 'null' by default
public int level; // '0' by default
// in constructor
levelHud = new Actor(){}; // you can create an Actor subclass for this
levelUp();
// with the method
public void levelUp()
{
    level++;
    levelHud.setImage(new GreenfootImage("Level: "+level, 24, null, null));
}
javaisanart javaisanart

2015/1/30

#
danpost wrote...
You can either use the 'showText' method of the World class or create and add an actor into the world whose image relays the information to the user. An object reference field is usually created when using an actor. For example:
// fields
private Actor levelHud; // 'null' by default
public int level; // '0' by default
// in constructor
levelHud = new Actor(){}; // you can create an Actor subclass for this
levelUp();
// with the method
public void levelUp()
{
    level++;
    levelHud.setImage(new GreenfootImage("Level: "+level, 24, null, null));
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Hud extends Actor
{
    private Actor levelHud; // 'null' by default
    public int level; // '0' by default
    // in constructor
    levelHud = new Hud(){}; // you can create an Actor subclass for this
    levelUp();
    // with the method
    public void levelUp()
    {
        level++;
        levelHud.setImage(new GreenfootImage("Level: "+level, 24, null, null));
    }
    public void act() 
    {
        // Add your action code here.
    }    
}
like this? also says im missing an identifier
danpost danpost

2015/1/30

#
Please note what line 7 says about where lines 8 and 9 should go. If you do not understand, refer to this page of the java tutorials.
javaisanart javaisanart

2015/1/31

#
danpost wrote...
Please note what line 7 says about where lines 8 and 9 should go. If you do not understand, refer to this page of the java tutorials.
pfft oh duh lol sorry
javaisanart javaisanart

2015/2/1

#
i used the code but text isnt showing i can create my object and i entered those lines into the constructor but i cant make text appear to show the level
danpost danpost

2015/2/1

#
The code I gave was supposed to be in your world subclass -- not in your new Hud actor subclass. In fact, the code as given will work in your world subclass without any changes. If you create a basic Actor subclass for HUD objects, make it a very basic class, like this:
public class Hud extends greenfoot.Actor {}
and change my line 5 to:
levelHud = new Hud();
Yes. That first line of code in this post is the entire class code.
You need to login to post a reply.