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

2014/11/21

Character Selection Game

1
2
3
4
danpost danpost

2014/12/1

#
If the only condition to start the game is that a selection is made, then you can put the code to start the game in the 'setCharselect' method.
joanne_wm joanne_wm

2014/12/3

#
then what would the code be?
danpost danpost

2014/12/3

#
Have you looked over the class documentations that greenfoot provides? They show all the methods provided by greenfoot, describing each one. Try to see if you can find what is needed before asking for help here.
joanne_wm joanne_wm

2014/12/5

#
where can i find the documentation?
danpost danpost

2014/12/5

#
There is a 'Documentation' link along the bottom of the green header of the page. The top-right section, 'References' (I believe it is called), has an 'online' link to the Greenfoot API documentation where you can see what fields, methods, and constructors are available in each class provided.
joanne_wm joanne_wm

2014/12/5

#
When I want to level up the game, I used this code:
    {if (Greenfoot.isKeyDown("whatever key this is")) 
   Greenfoot.setWorld(new LB_HeavenWorld()); 
}
but while running the game, and when I press the provided key, it doesn't move on to the next level. Another problem is that when I try running the game on the selection World, 1. the objects don't appear 2. when I press the objects so they should go to the next level, this is what happens in the terminal
java.lang.ClassCastException: SelectionWorld cannot be cast to L4_SpaceWorld
	at ActorName.selected(Seulgi.java:20)
	at ActorName.act(Seulgi.java:53)
danpost danpost

2014/12/5

#
joanne_wm wrote...
When I want to level up the game, I used this code: < Code Omitted > but while running the game, and when I press the provided key, it doesn't move on to the next level.
The scenario still runs, but on the same level? or do you get any type of error message?
Another problem is that when I try running the game on the selection World, 1. the objects don't appear 2. when I press the objects so they should go to the next level, this is what happens in the terminal < Terminal Output Omitted >
What is line 20 in your ActorName class (in the 'selected' method)? And if they do not appear, how can you press one? That does not make much sense. Maybe you are not being very clear as to what is happening.
danpost danpost

2014/12/5

#
It is very confusing to help with so little knowledge of your code. Tiny snippets do not give context as to what you are doing and make it very difficult to help properly. Even an overview as far as class structure (what class extends what and what each is for), the selection world and space world codes; and maybe even the player class.
joanne_wm joanne_wm

2014/12/6

#
so basically here's my full code for the actor
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Irene here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Irene extends Animal 
{
    public Irene()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 100, image.getHeight() -100);
        setImage(image);
    }  
 

    /**
     * Act - do whatever the Irene wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    if (canSee(SuperCandy.class))
        {
             eat(SuperCandy.class);
        }
             
        if (Greenfoot.isKeyDown("up"))
        {
            move(10);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
    
  
{  
    if (Greenfoot.mouseClicked(this)) ((L4_SpaceWorld)getWorld()).setCharselect(1);  
} 
}

}
danpost danpost

2014/12/6

#
The thing that is confusing me is you ask how to go from the selection world to the space world after the selection is done -- line 49 above leads me to believe you are already in the space world -- unless L4_SpaceWorld is your selection world; or is not the 'setCharselect' method part of the selection process? Please, clarify this for me.
danpost danpost

2014/12/6

#
Anyway, if you can compile your project, do so; and then right-click on your SelectionWorld class icon and select 'new SelectionWorld()' from the pop-up menu. That will make that world be the initial active world of your project. When a selection is made you should be setting a new L4_SpaceWorld as the active world using 'setWorld'. The selection process should be done in the SelectionWorld class; so, the methods needed for that process should be in that class. We should square that away first; then work on implementing that selection in your L4_SpaceWorld class.
joanne_wm joanne_wm

2014/12/8

#
I'm not even sure if the character is already in space world. Heres what is in the SelectionWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

private int Charselect;  
  
public int setCharselect(int x)      
{      
    Charselect = x;      
    return Charselect;      
}   
  
public int getCharselect()      
{      
    return Charselect;      
}  


    /**
     * Constructor for objects of class SelectionWorld.
     * 
     */
    public SelectionWorld()
    {    
        //Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(560, 560, 1); 
        
    }
    
     /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {       
        setPaintOrder(SelectionWorld.class, L4_SpaceWorld.class, LB_HeavenWorld.class);
        addObject(new Irene(), 1, 1);  
        addObject(new Seulgi(), 1, 1);  
        addObject(new Wendy(), 1, 1);  
        addObject(new Joy(), 1, 1);  
        addObject(new Taeyeon(), 1, 1);   
    }
    


}
So this is the code in the SelectionWorld, also the problem with the objects not appearing. Also the terminal window would say: java.lang.ClassCastException: SelectionWorld cannot be cast to L4_SpaceWorld at Irene.act(Irene.java:49)
the code for the space world is then:
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class L4_SpaceWorld extends World
{
     private CountdownClock timer;
     private GreenfootSound backgroundMusic = new GreenfootSound("행복 Happiness.mp3");
private int Charselect;  
  
public int setCharselect(int x)      
{      
    Charselect = x;      
    return Charselect;      
}   
  
public int getCharselect()      
{      
    return Charselect;      
}  

    /**
     * Create the space world (the universe). Our world has a size 
     * of 900x600 cells, where every cell is just 1 pixel.
     */
       public L4_SpaceWorld() 
    {
        super(560, 560, 1);
        //etc. backgroundMusic.playLoop();
        
        prepare();
        
        setPaintOrder(CountdownClock.class, Irene.class, Seulgi.class, Wendy.class, Joy.class, Taeyeon.class,
        SuperCandy.class);
    }
    
     /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {       
        timer = new CountdownClock(60);
        
        addObject(timer, 60, 45);
        
        
        addRandomSuperCandy(100);
    }
    
   
    private void addRandomSuperCandy(int howMany)
   {
       int i;
       for (i=0; i < howMany; i++)
       {
           addObject(new SuperCandy(), 200 + Greenfoot.getRandomNumber(getWidth() - 200),
                       100 + Greenfoot.getRandomNumber(getHeight()) - 200);
        }
   }
   
 public void act()
    {
        if (timer.getCounter() <= 0)
        {
       Greenfoot.stop();  
    }
    
    {if (Greenfoot.isKeyDown("A")) 
   Greenfoot.setWorld(new LB_HeavenWorld()); 
}

if (timer.getCounter() <= 0)
        {
            gameOver("Oh no! You ran out of time!");
        }
        
         java.util.List SuperCandys = getObjects(SuperCandy.class);
        if (SuperCandys.isEmpty())
        {
            gameOver("You win\nAll candies eaten. Press 'A' to go to the next level.");
     
       }
    }
  
    public void gameOver(String msg)
    {
        PopUpMessage popup = new PopUpMessage(msg);
        addObject(popup, getWidth() / 2, getHeight() / 2);
        Greenfoot.stop();
    }
}
danpost danpost

2014/12/8

#
Ok. Let us start with your SelectionWorld class. Current problems at the moment: (1) you have a world constructor that calls 'super' to create the world; but that is all the constructor does and there is no act method in the class. In other words, you create a dead world; (2) you have a prepare method that would set the paint order and add 5 actors into the world if it was ever called; problem is that you have World classes in the paint order; not actor classes. No two worlds will ever be active at the same time; but the different types of actors in a world is what need attention as far as paint order. The documentation says the method will 'Set the paint order of objects in the world.' The paint order would be specific to world object the method is executed on. Other worlds cannot be objects in that world. A second problem with the prepare method is that all 5 actors will be placed at exactly the same location if the method was ever executed; Now, you also appear to have a problem when your actors are placed in the SelectionWorld world. We will address that at the same time as we make it easier to make a simple selection. We will start by making a subclass of actor called 'Selector'. I do not know if only the images are different for the 5 possible selections or not, so I will add a reference field to hold the actor itself instead of just using the image of the Selector objects created:
import greenfoot.*;

public class Selector extends Actor
{
    Actor selection;

    public Selector(Actor actor)
    {
        selection = actor;
        GreenfootImage img = new GreenfootImage(actor.getImage());
        GreenfootImage text = new GreenfootImage(actor.getClass().getName(), 24, null, null)
        GreenfootImage image = new GreenfootImage(80, 100);
        img.scale(80, 80);
        image.drawImage(img, 0, 0);
        image.drawImage(text, 40-text.getWidth()/2, 76);
        setImage(image);
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            L4_SpaceWorld spaceWorld = new L4_SpaceWorld();
            spaceWorld.setSelection(selection);
            Greenfoot.setWorld(spaceWorld);
        }
    }
}
If any name is too big and part of it is cut off of the actor image, decrease the '24' on line 11 (go down by twos). Ok. this class will hold the actor for which it will be the selector for, show its image with its name and wait for a click, upon which the new space world will be created, informed of the selection and set active. Now, in your SelectionWorld, add a call to 'prepare' in the constructor:
public SelectionWorld()
{
    super(560, 560, 1);
    prepare();
}
and change your prepare method to the following:
private void prepare()
{
    addObject(new Selector(new Irene()), 60, 300);
    addObject(new Selector(new Seulgi()), 170, 300);
    addObject(new Selector(new Wendy()), 280, 300);
    addObject(new Selector(new Joy()), 390, 300);
    addObject(new Selector(new Taeyeon()), 500, 300);
}
I do not know your image sizes or even if they are anywhere near square in shape, but hopefully this will give you something to work with. You just need to change your 'setCharSelect(int)' method to 'setSelection(Actor)' in the L4_SpaceWorld class. It does not have to return anything (a void return type is fine). You can then have it place the actor in the world directly.
joanne_wm joanne_wm

2014/12/9

#
thank you very much. Sorry for the inconvenience because I am trying something confusing, and our classes did not cover this area.
joanne_wm joanne_wm

2014/12/9

#
alright. problems are bouncing up again. Since I wanted to make the characters eat "SuperCandy.class", I entered the code into every single character
 if (canSee(SuperCandy.class))
        {
             eat(SuperCandy.class);
        }
             
Yep, and some of the characters say that they can't find the symbol. So regarding the code you gave me above
private void prepare()
{
addObject(new blablabla)
}
so i know where the problem is, and it says that the size given does not match with the size in the actor. I tried solving it by changing the numbers in the code above, but if I want to resize it, which row below is the one to change?
 GreenfootImage image = new GreenfootImage(80, 100);  
        img.scale(80, 80);  
        image.drawImage(img, 0, 0); 
one more thing since I have to change the "setCharSelect(int x)" to "setSelection(Actor)", how do I do it? I'm not too sure with this one
There are more replies on the next page.
1
2
3
4