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

2013/12/17

how to let my ship get a crate

14
15
16
17
18
19
20
danpost danpost

2014/1/8

#
Just add the line into the 'loadCrate' method. The sound file, 'load crate.wav', must be in the 'Sounds' folder of the project's folder.
virginia001 virginia001

2014/1/8

#
have it now
virginia001 virginia001

2014/1/9

#
can we proceed with spawning the shipss....
danpost danpost

2014/1/9

#
The ship would have to do the same as the truck to determine whether or not any more crates of its specific color remain. However, since the truck already does this, we can have the truck inform the ship that it is time to sail over the horizon and disappear. In the 'loadCrate' method of the truck class, change the 'if' statement to the following:
if (canMove())
{
    getWorld().removeObject(platform.getSchip());
    setPlatfom(null);
}
Then, in the Vracht world class:
// add these instance fields
private Actor[] ships = { new Schipgroen(), new Schipgeel(), new Schipzwart(), new Schiporanje(), new Schippaars(), new Schiprood() };
private int shipsRemaining = 6;
// and add this method
private void spawnRandomSchip()
{
    int shipNum = Greenfoot.getRandomNumber(shipsRemaining);
    addObject(ships[shipNum], 654, 96);
    ships[shipNum] = ships[shipsRemaining-1];
    shipsRemaining--;
}    
// and add this act method
public void act()
{
    if (getObjects(Schip.class).isEmpty())
    {
        if (shipsRemaining == 0) Greenfoot.stop();
        else spawnRandomSchip();
    }
}
This should work, provided you start with at least one of each color crate. Also, remove any other code you had in the world class that created and added a Schip object into the world.
virginia001 virginia001

2014/1/9

#
Working now thnx...
virginia001 virginia001

2014/1/9

#
I want to implement a timer into my game counting from lets say 60 seconds and when it's at zero you lost the game and the scenario stops i have this scenario as an example but I dont know how to adjust into my own scenario: http://www.greenfoot.org/scenarios/7243
danpost danpost

2014/1/9

#
(1) add an instance int field (call it 'gameTime') in the world class (2) add an instance int field (call it 'timeDelay') in the world class (3) add an instance Actor field (call it 'timeText'), and declare and set it as follows:
private Actor timeText = new Actor(){};
(4) add the following in the world constructor:
timeText.setImage(new GreenfootImage("Time: 0", 16, java.awt.Color.black, null));
addObject(timeText, 100, 50);
(5) add a call to a new method in the act method of the world (call it 'runTimer') (6) add the 'runTimer' method in the world class that (a) increments the 'timeDelay' field (b) checks the 'timeDelay' field for a specific value (around 50), and when at that value (I) reset the 'timeDelay' field to zero (ii) increment the 'gameTime' field (iii) update the image of the actor displaying the game time by using the following: 'timeText.setImage(new GreenfootImage("Time: "+gameTime, 16, java.awt.Color.black, null)); (iv) check the 'gameTime' field for the specified value of 60 (however, I do not think that will be enough time), and when at that value, do your end game routine.
virginia001 virginia001

2014/1/9

#
into my world class i got this now
private int gameTime;
    private int timeDelay;

    private Actor timeText = new Actor(){};

    timeText.setImage(new GreenfootImage("Time: 0", 16, java.awt.Color.black, null));  

    addObject(timeText, 100, 50);
The error says there is an identifier expected after timeText.setImage as for (6) i only still have implemented this in the code of the act method:
runTimer();
I don't now how to proceed with the other instructors and convert it into code though
danpost danpost

2014/1/9

#
Instruction (4) says 'in the world constructor'. That is the one that starts 'public Vracht()'. the 'super' call must remain the first line there. Move lines 6 and 8 (from your code post) into the constructor. For (6), you should begin with:
private void runTimer()
{
    // rest of code
}
Do the best you can with coding and then post back with any problems. It should be fairly straight-forward as to how to code it if you follow the instructions carefully.
virginia001 virginia001

2014/1/9

#
this is my result of your instructions but i get an error 'this is not an statement' before ==0 of timeDelay ==0;
private void runTimer()

    {  
        int timeDelay = +1;

        if (timeDelay >= 50)
        {
            timeDelay == 0;             
        }
        int gameTime;
        timeText.setImage(new GreenfootImage("Time: "+gameTime, 16, java.awt.Color.black, null));
        if (gameTime == 60)
        {
            Greenfoot.stop();
            Greenfoot.playSound("game over.wav");  
        }
    }
Super_Hippo Super_Hippo

2014/1/9

#
I am not sure if this works, if you just write 'int' there. I usually write for example 'private int timeDelay' in the beginning. In line 4, you always set timeDelay to 1. But you want to add one. To do this, you can use 'timeDelay = timeDelay+1;', 'timeDelay += 1;' or 'timeDelay++;'. Everything does the same, it adds '1' to timeDelay. If you use '==', then both values are compared to each user (used in if conditions). With '=' you set the variable on the left side to the value of the right side. So you need to remove one of the '='. Line 11 should be executed when timeDelay is 50, because it only changed then. Also increase the gameTime there which you set as an private int like said above.
virginia001 virginia001

2014/1/9

#
What i have now is this: but I get an error at (timeDelay = 50) and the = 50 are incompatible types
private void runTimer()

    {  
        int timeDelay = timeDelay++;

        if (timeDelay = 50)
        {
            timeDelay = 0;             
        }
        int gameTime = gameTime = 60;
        timeText.setImage(new GreenfootImage("Time: "+gameTime, 16, java.awt.Color.black, null));
        if (gameTime == 60)
        {
            Greenfoot.stop();
            Greenfoot.playSound("game over.wav");  
        }
    }
Super_Hippo Super_Hippo

2014/1/9

#
Reread my last post and try to figure out what I did:
private int timeDelay;
private int gameTime = 60;

private void runTimer()
    {  
        timeDelay++;

        if (timeDelay == 50)
        {
            timeDelay = 0;
            gameTime--;
            timeText.setImage(new GreenfootImage("Time: "+gameTime, 16, java.awt.Color.black, null));
        }
        
        if (gameTime == 0)
        {
            Greenfoot.playSound("game over.wav");  
            Greenfoot.stop();
        }
    }
virginia001 virginia001

2014/1/9

#
now the time is standing still on 60 even when I run the scenario this is the code what I have in the world constructor
timeText.setImage(new GreenfootImage("Time: 60", 16, java.awt.Color.black, null));  

        addObject(timeText, 30, 80);
Super_Hippo Super_Hippo

2014/1/9

#
Do you call the runTimer() method in the act() method? Where did you put the first two lines of my last post? Show us the world class code.
There are more replies on the next page.
14
15
16
17
18
19
20