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

15
16
17
18
19
20
21
virginia001 virginia001

2014/1/10

#
No i didn't because they keep giving me an error because you cant declare an method in a method right heres the code though..
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Vracht extends World
{
    private int gameTime = 60;
    private int timeDelay;

    private Actor timeText = new Actor(){};

    GreenfootSound backgroundMusic = new GreenfootSound("Sea sound.wav");
    public void started()
    {
        backgroundMusic.playLoop(); 
    }

    public void stopped()
    {
        backgroundMusic.stop();
    }

    private Actor[] ships = { new Schipgroen(), new Schipgeel(), new Schipzwart(), new Schiporanje(), new Schippaars(), new Schiprood() };  
    private int shipsRemaining = 6;  

    private void spawnRandomSchip()  
    {  
        int shipNum = Greenfoot.getRandomNumber(shipsRemaining);  
        addObject(ships[shipNum], 654, 96);  
        ships[shipNum] = ships[shipsRemaining-1];  
        shipsRemaining--;  
    }      

    public void act()  
    {  
        if (getObjects(Schip.class).isEmpty())  
        {  
            if (shipsRemaining == 0) Greenfoot.stop();  
            else spawnRandomSchip();  
        } 
    }

    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();  
        }  
    }  

    public Vracht()
    {    
        super(760,760, 1);

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

        addObject(timeText, 30, 80);

        prepare();

    }

    private void prepare()
    {
        Platform platform = new Platform();    
        addObject(platform, 380, 652);    
        vrachtwagenplek vrachtwagenplek = new vrachtwagenplek();  
        addObject(vrachtwagenplek, 500, 126); 
        platform.setLocation(390, 651);
        vrachtwagenplek.setLocation(381, 53);

        VrachtwagenZwart vrachtwagenzwart = new VrachtwagenZwart();
        addObject(vrachtwagenzwart, 269, 122);
        VrachtwagenGroen vrachtwagengroen = new VrachtwagenGroen();
        addObject(vrachtwagengroen, 342, 125);
        VrachtwagenOranje vrachtwagenoranje = new VrachtwagenOranje();
        addObject(vrachtwagenoranje, 415, 123);
        VrachtwagenRood vrachtwagenrood = new VrachtwagenRood();
        addObject(vrachtwagenrood, 513, 127);
        VrachtwagenGeel vrachtwagengeel = new VrachtwagenGeel();
        addObject(vrachtwagengeel, 467, 119);

        vrachtwagengroen.setLocation(319, 121);
        vrachtwagenoranje.setLocation(388, 121);
        vrachtwagengeel.setLocation(454, 120);
        vrachtwagenrood.setLocation(514, 119);
        vrachtwagenzwart.setLocation(256, 122);
        groen groen = new groen();
        addObject(groen, 296, 596);
        groen groen2 = new groen();
        addObject(groen2, 502, 734);
        groen.setLocation(287, 590);
        groen2.setLocation(496, 727);
        oranje oranje = new oranje();
        addObject(oranje, 353, 596);
        oranje.setLocation(351, 590);
        oranje oranje2 = new oranje();
        addObject(oranje2, 291, 729);
        oranje2.setLocation(288, 726);
        paars paars = new paars();
        addObject(paars, 411, 667);
        paars.setLocation(412, 589);
        rood rood = new rood();
        addObject(rood, 502, 594);
        rood rood2 = new rood();
        addObject(rood2, 294, 664);
        rood2.setLocation(288, 661);
        paars.setLocation(426, 589);
        oranje.setLocation(356, 590);
        oranje.setLocation(358, 589);
        rood.setLocation(495, 589);
        zwart zwart = new zwart();
        addObject(zwart, 366, 665);
        zwart.setLocation(360, 660);
        zwart zwart2 = new zwart();
        addObject(zwart2, 436, 734);
        zwart2.setLocation(427, 728);
        geel geel = new geel();
        addObject(geel, 496, 660);
        geel geel2 = new geel();
        addObject(geel2, 361, 728);
        geel2.setLocation(359, 727);
        zwart.setLocation(359, 661);
        oranje2.setLocation(427, 663);
        paars paars2 = new paars();
        addObject(paars2, 294, 729);
        paars2.setLocation(288, 727);
        vrachtwagenzwart.setLocation(248, 122);
        vrachtwagengroen.setLocation(297, 121);
        vrachtwagenoranje.setLocation(358, 121);
        vrachtwagengeel.setLocation(421, 120);
        vrachtwagenrood.setLocation(476, 120);
        VrachtwagenPaars vrachtwagenpaars = new VrachtwagenPaars();
        addObject(vrachtwagenpaars, 525, 125);
        vrachtwagenpaars.setLocation(522, 120);
        vrachtwagenoranje.setLocation(350, 121);
        vrachtwagengeel.setLocation(407, 121);
        vrachtwagenrood.setLocation(468, 120);
        vrachtwagenrood.setLocation(467, 120);
        vrachtwagenrood.setLocation(461, 120);
        vrachtwagenpaars.setLocation(515, 120);

        vrachtwagenzwart.setPlatform(vrachtwagenplek);
        vrachtwagengroen.setPlatform(vrachtwagenplek);
        vrachtwagenoranje.setPlatform(vrachtwagenplek);
        vrachtwagengeel.setPlatform(vrachtwagenplek);
        vrachtwagenrood.setPlatform(vrachtwagenplek);
        vrachtwagenpaars.setPlatform(vrachtwagenplek);

        platform.setLocation(390, 651);
        vrachtwagenplek.setLocation(381, 53);
        rood.setPlatform(platform);
        groen.setPlatform(platform);
        oranje.setPlatform(platform);
        paars.setPlatform(platform);
        rood2.setPlatform(platform);
        zwart.setPlatform(platform);
        oranje2.setPlatform(platform);
        geel.setPlatform(platform);
        paars2.setPlatform(platform);
        geel2.setPlatform(platform);
        zwart2.setPlatform(platform);
        groen2.setPlatform(platform);

        Lives lives = new Lives();
        addObject(lives, 34, 36);
        Lives lives2 = new Lives();
        addObject(lives2, 84, 43);
        Lives lives3 = new Lives();
        addObject(lives3, 136, 33);

        lives.setLocation(27, 25);
        lives2.setLocation(75, 24);
        lives3.setLocation(124, 24);

        oranje2.setLocation(427, 661);
    }
}
danpost danpost

2014/1/10

#
danpost wrote...
(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.
You should have ended up with this:
// imports
public class Vracht extends World
{
    // fields you already had
    private int gameTime; // (1)
    private int timeDelay; // (2)
    private Actor timeText = new Actor(){}; // (3)

    public Vracht()
    {
        super(760, 760, 1);
        prepare();
        // begin (4)
        timeText.setImage(new GreenfootImage("Time: 0", 16, java.awt.Color.black, null));
        addObject(timeText, 100, 20);
        // end (4)
    }

    public void act()
    {
        // code already in act method
        runTimer(); // (5)
    }

    // runTimer method (6)
    private void runTimer()
    {
        timeDelay++; // (a)
        if (timeDelay == 50) // (b)
        {
            timeDelay = 0; // (i)
            gameTime++; // (ii)
            timeText.setImage(new GreenfootImage("Time: "+gameTime, 16, java.awt.Color.black, null)); // (iii)
            if (gameTime == 60) // (iv)
            {
                Greenfoot.stop(); // game over routine
            }
        }
    }

    // method(s) you already had  ('prepare')
}
Like I said, it was pretty straight-forward.
virginia001 virginia001

2014/1/10

#
okay I see but adjusted it a little because I became a little confused by the other guy saying something different... as for the timer it still stays on 60 seconds thoughh and I've implemented everything into my code..
danpost danpost

2014/1/10

#
What does your line for step (1) look like?
virginia001 virginia001

2014/1/10

#
private int gameTime;
danpost danpost

2014/1/10

#
Update the scenario on the site.
Super_Hippo Super_Hippo

2014/1/10

#
I actually did not say something different. At least, I do not know where.
virginia001 virginia001

2014/1/10

#
I've fixed the timer now now and it's counting only problem is I want it to count from lets say 60 seconds to one but atm the timer is going from 60 to -1,-2 and so on...
danpost danpost

2014/1/10

#
Alright, well it is counting downward, so change the field declaration statement to
private int gameTime = 60;
and in the constructor of the world ( 'public Vracht()' ), change the initial displaying text to 'Time: 60' (looks like this is done, already). Finally, in the 'runTimer' method, the inner 'if' statement should check for 'if (gameTime == 0)'.
Super_Hippo Super_Hippo

2014/1/10

#
Oh true, that was what I did different...
virginia001 virginia001

2014/1/10

#
The timer is exactly how i want want to now....Now i have created a new class called shark as more of a challenge in the game but i have created my own image and saved it into the image folder of the scenario but when i want to import it a terminal window pops up and says it can't find the image
Super_Hippo Super_Hippo

2014/1/10

#
Do you use an image format which Greenfoot supports?
virginia001 virginia001

2014/1/10

#
yes png
danpost danpost

2014/1/10

#
Again, upload the scenario. We can hopefully figure out what is going on then.
virginia001 virginia001

2014/1/10

#
published it
There are more replies on the next page.
15
16
17
18
19
20
21