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

2020/2/22

I need desperate help with making levels

Happycrusher764 Happycrusher764

2020/2/22

#
So my level gets created when it is score= 5 for Level 1 (MyWorld). However the turtle cannot hit the strawberries in the new level. Why?
Greenfoot wrote...
java.lang.ClassCastException: Level2 cannot be cast to MyWorld at Strawberry.HitEnemy(Strawberry.java:25) at Strawberry.act(Strawberry.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
that is the error code. Here is my code for the whole game. If anyone wants to help please go to my scenarios. The second one "Yummy Strawberries v.2.0.1". Here is my code though for every world and actor:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    /**
     * Constructor for objects of class StartScreen.
     * 
     */
    public StartScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600 , 1); 
        prepare();
    }

    public void act()
    {
        if(Greenfoot.isKeyDown("enter"))
            Greenfoot.setWorld(new MyWorld());

    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        TitleScreen titleScreen = new TitleScreen();
        addObject(titleScreen,377,313);
    }
}
That was my title screen above.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (Sadeesh Senevirathne) 
 * @version (2.0.0)
 */ 
public class MyWorld extends World
{
    Counter counter = new Counter();
    //1.5.5 brings cleaner movement coding for the Turtle and a Score Counter that will function in next update.
    //This new update features radical changes to the whole code such as GameOver and new class.
    /**
     * 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();
    }
      
    public Counter getCounter()
    {
           return counter;
    }
    
    
    
    
    /**
     * 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(counter, 100, 40);
        Strawberry strawberry = new Strawberry();
        addObject(strawberry,263,204);
        Strawberry strawberry2 = new Strawberry();
        addObject(strawberry2,441,153);
        Strawberry strawberry3 = new Strawberry();
        addObject(strawberry3,451,321);
        Strawberry strawberry4 = new Strawberry();
        addObject(strawberry4,197,306);
        Strawberry strawberry5 = new Strawberry();
        addObject(strawberry5,87,139);
        Strawberry strawberry6 = new Strawberry();
        addObject(strawberry6,275,78);
        Bee bee = new Bee();
        addObject(bee,331,92);
        Bee bee2 = new Bee();
        addObject(bee2,273,311);
        Bee bee3 = new Bee();
        addObject(bee3,57,52);
        Turtle turtle = new Turtle();
        addObject(turtle,561,41);
        Strawberry strawberry7 = new Strawberry();
        addObject(strawberry7,451,470);
        Strawberry strawberry8 = new Strawberry();
        addObject(strawberry8,669,481);
        Strawberry strawberry9 = new Strawberry();
        addObject(strawberry9,667,302);
        Strawberry strawberry10 = new Strawberry();
        addObject(strawberry10,125,510);
        Strawberry strawberry11 = new Strawberry();
        addObject(strawberry11,304,508);
        Bee bee4 = new Bee();
        addObject(bee4,566,478);

        removeObject(bee);
        removeObject(bee2);
        removeObject(strawberry3);
        removeObject(strawberry7);
        removeObject(strawberry11);
        removeObject(strawberry10);
        removeObject(strawberry4);
        strawberry.setLocation(272,498);
        removeObject(strawberry9);
        strawberry8.setLocation(441,346);
        strawberry2.setLocation(732,182);
        strawberry6.setLocation(281,290);
        strawberry.setLocation(78,507);
        bee4.setLocation(293,492);
        strawberry8.setLocation(665,473);
    }
}
That is the MyWorld World. (level 1)
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()
    {
        Strawberry strawberry = new Strawberry();
        addObject(strawberry,511,373);
        Strawberry strawberry2 = new Strawberry();
        addObject(strawberry2,269,425);
        Strawberry strawberry3 = new Strawberry();
        addObject(strawberry3,335,244);
        Strawberry strawberry4 = new Strawberry();
        addObject(strawberry4,716,385);
        Strawberry strawberry5 = new Strawberry();
        addObject(strawberry5,715,20);
        Strawberry strawberry6 = new Strawberry();
        addObject(strawberry6,162,170);
        Strawberry strawberry7 = new Strawberry();
        addObject(strawberry7,98,487);
        Turtle turtle = new Turtle();
        addObject(turtle,476,467);
        Bee bee = new Bee();
        addObject(bee,361,335);
        Bee bee2 = new Bee();
        addObject(bee2,70,76);
        Bee bee3 = new Bee();
        addObject(bee3,690,170);
    }
}
that is level 2 world code.
public class Bee extends Actor
{
    /**
     * Act - do whatever the Bee wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
            
        move(Greenfoot.getRandomNumber(10));
        if (isAtEdge())
    {
        if (Greenfoot.getRandomNumber(100) <100)
    {
        turn(180);
    }
}
          if (Greenfoot.getRandomNumber(100) <20)
    {
        turn(Greenfoot.getRandomNumber(20));
    }
    }    
    }    

That is the bee actor code (the thing that eats the turtle).
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, 24, Color.GREEN, Color.BLACK));
      if (score == 5) Greenfoot.setWorld(new Level2());
  
    } 
    
    public void addScore()
    {
         score++; 
    }
}
That is my counter. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Strawberry here. * * @author (your name) * @version (a version number or a date) */ public class Strawberry extends Actor { /** * Act - do whatever the Strawberry wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { HitEnemy(); } public void HitEnemy() { if (isTouching (Turtle.class)) { World myWorld = getWorld(); MyWorld space = (MyWorld)myWorld; Counter counter = space.getCounter(); counter.addScore(); getWorld().removeObject (this); } } } That is my Strawberry actor. I suspect that the mistake is somewhere in the public void hit enemy section. But i dont know how to fix it.
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();
            }
    }
    
}
That is my strawberry code. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class YouLose here. * * @author (your name) * @version (a version number or a date) */ public class YouLose extends Actor { /** * Act - do whatever the YouLose wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public YouLose() { GreenfootImage image = getImage(); image.scale(300, 200); setImage(image); } } That is my GameOver actor code IN CONCLUSION I THINK THERE IS SOMETHING WRONG BETWEEN THE RELATION WITH THE STRAWBERRY, LEVEL 2 AND THE TURTLE. THE COUNTER ALSO DOES NOT WORK IN LEVEL 2. HOW CAN I FIX THIS?
Archerfish Archerfish

2020/2/22

#
Hi Happycrusher764! 1. Your suspicions were correct, the main issue is with the statements (in strawberry):
World myWorld = getWorld();
MyWorld space = (MyWorld)myWorld;
Counter counter = space.getCounter()
Although I do not fully understand yet, I believe a complication arises in "Level2" because "space" is a "MyWorld" variable. For "space" to work in "Level2", it needs to be a "Level2" variable. Here is a possible work around: NOTE: Because we are now directly calling the addScore() method in "Strawberry", the getCounter() method from "MyWorld" can be removed
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();
             getWorld().removeObject (this);
         }
    }
}
2. Another issue lies in the fact that there isn't a Counter object in Level2 (it hasn't been initialized or placed in the world) Hope this helps! Cheers, Archerfish
Happycrusher764 Happycrusher764

2020/2/22

#
Hey Archerfish. Im not sure what happened... When I get to level 2 the game just stops. An error message comes up and nothing happens. it just gets frozen... Also how do I initialize the counter to level2? The error message is:
Greenfoot.exe wrote...
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at Strawberry.HitEnemy(Strawberry.java:16) at Strawberry.act(Strawberry.java:9) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
Happycrusher764 Happycrusher764

2020/2/22

#
i really need help quickly bc my assessment is due on monday. Im hoping to finish the game today and do the paperwork tomorrow. So any help is omg super appreciated.
Archerfish Archerfish

2020/2/22

#
The error comes from the fact that the Counter is not in Level2 (Strawberry tries to retrieve a Counter in the World, but does not find one). But no worries, this should fix it (for Level2)!
import greenfoot.*;
 
public class Level2 extends World
{
    Counter counter = new Counter(); //Counter Initialized and declared
    /**
     * Constructor for objects of class Level2.
     * 
     */
    public Level2()
    {    
        super(800, 600, 1); 
        prepare();
    }

    private void prepare()
    {
        addObject(counter, 100, 40); //Counter added to world
        Strawberry strawberry = new Strawberry();
        addObject(strawberry,511,373);
        Strawberry strawberry2 = new Strawberry();
        addObject(strawberry2,269,425);
        Strawberry strawberry3 = new Strawberry();
        addObject(strawberry3,335,244);
        Strawberry strawberry4 = new Strawberry();
        addObject(strawberry4,716,385);
        Strawberry strawberry5 = new Strawberry();
        addObject(strawberry5,715,20);
        Strawberry strawberry6 = new Strawberry();
        addObject(strawberry6,162,170);
        Strawberry strawberry7 = new Strawberry();
        addObject(strawberry7,98,487);
        Turtle turtle = new Turtle();
        addObject(turtle,476,467);
        Bee bee = new Bee();
        //addObject(bee,361,335);
        Bee bee2 = new Bee();
        //addObject(bee2,70,76);
        Bee bee3 = new Bee();
        //addObject(bee3,690,170);
    }
}
Happycrusher764 Happycrusher764

2020/2/22

#
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()
    {
        Strawberry strawberry = new Strawberry();
        addObject(strawberry,511,373);
        Strawberry strawberry2 = new Strawberry();
        addObject(strawberry2,269,425);
        Strawberry strawberry3 = new Strawberry();
        addObject(strawberry3,335,244);
        Strawberry strawberry4 = new Strawberry();
        addObject(strawberry4,716,385);
        Strawberry strawberry5 = new Strawberry();
        addObject(strawberry5,715,20);
        Strawberry strawberry6 = new Strawberry();
        addObject(strawberry6,162,170);
        Strawberry strawberry7 = new Strawberry();
        addObject(strawberry7,98,487);
        Turtle turtle = new Turtle();
        addObject(turtle,476,467);
        Bee bee = new Bee();
        addObject(bee,361,335);
        Bee bee2 = new Bee();
        addObject(bee2,70,76);
        Bee bee3 = new Bee();
        addObject(bee3,690,170);
        Counter counter = new Counter();
        addObject(counter,29,36);
        counter.setLocation(84,18);
    }
}
this is my level2 code. as you can see i have added the counter. Archerfish. Thank you so much. It works now. However. I added the counter into the world and now it works. However the counter gets reset back to 0 when it goes to Level2. How do I fix this? I want it to stay on the score "5" when it goes to level 2.
Happycrusher764 Happycrusher764

2020/2/22

#
pls someone help me
danpost danpost

2020/2/22

#
Happycrusher764 wrote...
pls someone help me
You need to adjust the new Counter value when you create the new Level2 world. In Counter, you have the following at line 19:
if (score == 5) Greenfoot.setWorld(new Level2());
It needs to be broke up so you get a reference to the new world to change its Counter value:
if (score == 5)
{
    Level2 level2 = new Level2();
    // get reference to Counter object in level2 here
    // adjust value of Counter object in level2 here
    Greenfoot.setWorld(level2);
}
Happycrusher764 Happycrusher764

2020/2/23

#
How do i get reference to the counter object and how do i adjust the value of the counter value? sorry im brand new to coding
danpost danpost

2020/2/23

#
Happycrusher764 wrote...
How do i get reference to the counter object
Same as in Strawberry class above, except instead of getWorld() you would use level2.
how do i adjust the value of the counter value? sorry im brand new to coding
counter.score = score;
taking value of score in current counter and making score in counter of level2 equal it.
You need to login to post a reply.