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

2020/3/8

Counter

1
2
Happycrusher764 Happycrusher764

2020/3/8

#
Hello. My counter works in my world but when it changes worlds from MyWorld to Level2 it resets the score back to 0. how can I fix this? even if it is manually setting the score to 5 when it is set to Level2. Please help with this. Ik i have asked many questions today but I'm trying to improve my game as much as possible for the due date tomorrow. The code i think may be relevant is below:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level2 extends World
{
    
    /**
     * Constructor for objects of class Level2.
     * 
     */
    public Level2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
         
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(new Strawberry(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        addObject(new Strawberry(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        addObject(new Strawberry(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        addObject(new Strawberry(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        addObject(new Strawberry(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        addObject(new Turtle(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        addObject(new Bee(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        addObject(new Bee(), Greenfoot.getRandomNumber(775), Greenfoot.getRandomNumber(575));
        Counter counter = new Counter();
        addObject(counter,146,37);
        Counter counter2 = new Counter();
        addObject(counter2,56,38);
        counter2.setLocation(1128,394);
        removeObject(counter2);
    }
    
    public void act()
    {
        
        

    }
}
That above is my Level2 Code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Counter counter = new Counter();
        addObject(counter,49,42);
        Strawberry strawberry = new Strawberry();
        addObject(strawberry,142,362);
        Strawberry strawberry2 = new Strawberry();
        addObject(strawberry2,226,135);
        Strawberry strawberry3 = new Strawberry();
        addObject(strawberry3,745,376);
        Strawberry strawberry4 = new Strawberry();
        addObject(strawberry4,427,493);
        Strawberry strawberry5 = new Strawberry();
        addObject(strawberry5,522,259);
        Bee bee = new Bee();
        addObject(bee,621,118);
        Bee bee2 = new Bee();
        addObject(bee2,130,493);
        Turtle turtle = new Turtle();
        addObject(turtle,697,266);
        turtle.setLocation(680,353);
        counter.setLocation(103,39);
    }
}
That above is my MyWorld (Level1) world.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;  
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 50, Color.GREEN, Color.BLACK));
      if (score == 5) Greenfoot.setWorld(new Level2());
      if (score == 5) Greenfoot.playSound ("trans.mp3");
      
    } 
    
    public void addScore()
    {
         score++; 
    }
    
    
}
That is my Counter actor.
import greenfoot.*;
import java.util.*;
 
public class Strawberry extends Actor
{
    ArrayList<Counter> counter;
    public void act() 
    {
     HitEnemy();
    }   
    public void HitEnemy()
    {
         if (isTouching (Turtle.class))
         {
             counter = (ArrayList<Counter>)(getWorld().getObjects(Counter.class));
             (counter.get(0)).addScore();
             Greenfoot.playSound("point.mp3");
             getWorld().removeObject (this);
             
         }
    }
}
Strawberries. the thing that increases the score if collected by turtle in the game.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Turtle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Turtle extends Actor
{
     public Turtle()
    {    
        GreenfootImage image = getImage();  
        image.scale(50, 50);
        setImage(image);
    }
    /**
     * Act - do whatever the Turtle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    HitEnemy();
            if (Greenfoot.isKeyDown ("a"))
    {
        turn (-3);
    }
     if (Greenfoot.isKeyDown ("d"))
    {
        turn (3);
    }
     if (Greenfoot.isKeyDown ("s"))
    {
        move (-3);
    }  
     if (Greenfoot.isKeyDown ("w"))
    {
        move (3);
    }
    
    }  
    
   
    public void HitEnemy()
    {
         if (isTouching (Bee.class))
         {
             getWorld().addObject (new YouLose(), 400, 300);
             getWorld().removeObject (this);
             Greenfoot.stop();
            }
    }
    
}


//turtle. no explanation needed...
That above is my turtle.
Happycrusher764 Happycrusher764

2020/3/8

#
The text is describes the code above not below. In conclusion I would like help making it so that the score remains as 5 when gone to Level2
Happycrusher764 Happycrusher764

2020/3/8

#
Can someone please help me?
danpost danpost

2020/3/8

#
Your Counter class, for as little as there is, is difficult to work with. It updates its image every act step and will start a new Level2 world if in any world with the score equal to 5. Use a constructor to initialize its image and update it only when the score is added to. Place a second condition on changing to the Level2 world.
Happycrusher764 Happycrusher764

2020/3/8

#
Im not sure how to do this. How can I go about doing this?
Happycrusher764 Happycrusher764

2020/3/8

#
pls help im trying to finish this quickly as it is late now.
danpost danpost

2020/3/8

#
Happycrusher764 wrote...
Im not sure how to do this. How can I go about doing this?
import greenfoot.*;

public class Counter extends Actor
{
    int score = 0;
    
    public Counter()
    {
        updateImage();
    }
    
    public void updateImage()
    {
        setImage(new GreenfootImage("Score: "+score, 50, Color.GREEN, Color.BLACK));
    }
    
    public void addScore()
    {
        score++;
        updateImage();
        if ( (getWorld() instanceof MyWorld) && score == 5 )
        {
            GreenfootSound trans = new GreenfootSound("trans.mp3");
            trans.play();
            Greenfoot.delay(1); // allows updated score to show
            World level2 = new Level2();
            level2.addObject(this, 146, 37);
            while (trans.isPlaying()) { } // waits till sound stops
        }
    }
}
Happycrusher764 Happycrusher764

2020/3/9

#
This does not work. once i reach the last strawberry the game freezes for like 5 seconds, then the strawberry disappears. But it doesnt go to the next level. A loading symbol also appears on the bottom right.
footpickle footpickle

2020/3/9

#
YEET ha ha ha
danpost danpost

2020/3/9

#
Happycrusher764 wrote...
This does not work. once i reach the last strawberry the game freezes for like 5 seconds, then the strawberry disappears. But it doesnt go to the next level. A loading symbol also appears on the bottom right.
Try commenting out lines 23, 24 and 28. Then, if still freezing, in addition, line 25 (though, I doubt that would be causing the freezing).
footpickle footpickle

2020/3/9

#
UH OH STINKY HA HA
Happycrusher764 Happycrusher764

2020/3/10

#
Ok @danpost i will try this.
danpost danpost

2020/3/10

#
Happycrusher764 wrote...
Ok @danpost i will try this.
Oh, crap -- I just forgot to put a line in there. Instead of commenting out those lines, add the following after the while statement:
Greenfoot.setWorld(level2);
(??? or add the line and comment out ??? -- the loading symbol seems strange)
Happycrusher764 Happycrusher764

2020/3/10

#
import greenfoot.*;
 
public class Counter extends Actor
{
    int score = 0;
     
    public Counter()
    {
        updateImage();
    }
     
    public void updateImage()
    {
        setImage(new GreenfootImage("Score: "+score, 50, Color.GREEN, Color.BLACK));
    }
     
    public void addScore()
    {
        score++;
        updateImage();
        if ( (getWorld() instanceof MyWorld) && score == 5 )
        {
            GreenfootSound trans = new GreenfootSound("trans.mp3");
            trans.play();
            Greenfoot.delay(1); // allows updated score to show
            World level2 = new Level2();
            level2.addObject(this, 146, 37);
            while (trans.isPlaying()) { } // waits till sound stops
            Greenfoot.setWorld(level2);
        }
    }
}
Does this look correct?
Happycrusher764 Happycrusher764

2020/3/10

#
OMG i fixed the counter yes. It now stays across level2! I removed the while statement (that was the reason it was freeezing the game...).
There are more replies on the next page.
1
2