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

2013/8/26

Help

Nocte Nocte

2013/8/26

#
So, I've been trying to create this game and I want to know 3 things: 1. how to make the game end after 2 minutes 2. how to add a "coin counter" Counts the number of times a specific actor class is removed 3. how to make an enemy spawn after something is collected Like you get a coin, and an one of the enemy classes I created spawns somewhere else Thanks
danpost danpost

2013/8/26

#
(1) and (2) can be accomplished by adding instance int fields for counting. One counts act methods the other counts collected coins. The game timer can count downward while the coin collection counter can increment upward. For a normal speed scenario, about 50 to 60 act cycles will be executed per second; so, something like 6000 (50x60x2) to 7200 (60x60x2) would be the range for the starting value of the game timer. The decrementing of the game timer should happen every act cycle; so, you should call a method that runs the timer from the act method (this can be either in the class of the main actor or in the world class). The incrementing of the coin collection counter should be in the class of the main actor who collects the coins and should be done in conjunction with removing of the coin being collected. (3) If it is a specific something being collected that spawns an enemy, it must either be the only actor object of its type OR it must be 'tagged' as being the one that spawns an enemy. To apply a tag, add a boolean field and have all objects of its type get a false value for this field except the one that causes the spawning. You will then just check to see if the field is true when removing the object to trigger the spawning. If it is by random chance that a spawn will occur, then check a random number for that chance. If you have multiple possibilities for what is spawned, create a separate random chance for determining which one is spawned (the creation of each and every type must be coded separately; meaning, there is no easy way to create a one-liner that can spawn a variety of types of objects). The location of the object being spawn can be specifically set or randomly determined. For further assistance, please respond with specific attempted code with a detailed description of what should happen and how the code does not do what you want (and how it is actually working at the moment). If you are getting any error messages, post the full error message you are getting and the code around which the error occurs.
Nocte Nocte

2013/8/27

#
Ok so I pretty much figured out some of it, changing it a little to make it easier for me, but this is a piece of code that got an error. What I am trying to make it do is after 10 points, another enemy spawns:
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
        if (Counter() == 10)
        {
        if (Greenfoot.getRandomNumber(200 <100)
            addObject(new Lobster(), 0, Greenfoot.getRandomNumber(600));
        }
        else
        {
            counter++;
        }
    }
Line 39 is the problem
danpost danpost

2013/8/27

#
Lines 39 through 47 should be done somewhere else (not in the Counter class). The spawning of a new lobster is not related at all to a counter and its code should not be in the Counter class. It should be in the class of the object that collects the object that causes the spawning. In line 39, however, you cannot refer to a Counter object by calling a constructor (or a method that does not exist; since you are not using the 'new' keyword here). Even then, you cannot compare an object with an int value. Line 41 is also flawed in that 'getRandomNumber' does not accept a boolean value for an argument. Usually, a game timer is created and its object is saved in an instance field in the world class where it is also controlled. The coin collection counter can be created and saved in the class of the object that collects the coins or created in the world class and the object passed to that class where it is incremented when necessary. These object can be added into the world, but do not have to be. To create and add the game timer, in your world class:
// add these instance fields
private Counter gameTimer = new Counter();
private int actCounter;
private boolean timeElapsed;
// you can (but do not have to) add the game timer into the world in your world constructor with
addObject(gameTimer, 100, 25);
// add in the world class act method
runGameTimer();
// and add in the world class code the following method
private void runGameTimer()
{
    if (timeElapsed) return;
    actCounter = (actCounter+1)%55; // change 55 to adjust timer speed
    if (actCounter == 0)
    {
        gameTimer.add(1);        
        if (gameTimer.getValue() == 120)
        {
            // end game here
            timeElapsed = true;
        }
    }
}
You need to login to post a reply.