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
joanne_wm joanne_wm

2014/11/21

#
Hi there. I want to know if anyone can help me with this? I want to have this page where I can select characters but I don't know how. This is really urgent so PLZ help me out! THX!
danpost danpost

2014/11/21

#
The way the selection is handled can be done in multiple ways. However, I do suggest that you have a separate world for the selection process and when the 'exit to game' action is encountered, either create the world and set it up before setting the world active or pass the info to the game world in its constructor and have that world set itself up according to what was passed to it.
joanne_wm joanne_wm

2014/11/24

#
can you help me with the code? It does seems confusing. Can I kind of have the simplest way for doing the selection? Sorry for not replying immediately I didn't have my phone
danpost danpost

2014/11/24

#
joanne_wm wrote...
can you help me with the code? It does seems confusing. Can I kind of have the simplest way for doing the selection?
What have you got so far? and what have you tried?
joanne_wm joanne_wm

2014/11/25

#
I added this into the World
1
2
3
4
5
6
7
8
9
public int getCharselect() 
   
       return Charselect; 
   
   public int setCharselect(int x) 
   
       Charselect=x; 
       return Charselect; 
   }
and then I added this into the character
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void selected() 
    
        L4_SpaceWorld wereld = (L4_SpaceWorld)getWorld(); 
        int Charselect=wereld.getCharselect(); 
    
 
public voic act()
{
        selected(); 
         if (Greenfoot.mouseClicked(this)) { 
            L4_SpaceWorld wereld = (L4_SpaceWorld)getWorld(); 
            int Charselect=wereld.setCharselect(1); 
         
    }
joanne_wm joanne_wm

2014/11/25

#
i meant void act for line 7 of course i added other things into it this is just a snippet of it i was actually basing it off another post but it doesn't seem to work
danpost danpost

2014/11/25

#
In both the 'selected' method and the 'act' method, you are, as a last line, declaring and setting a variable to a value (in both cases, 'int Charselect'). As soon as a 'return' from the method is executed (or when the current execution of the method is ended), the value set to 'Charselect' is lost. The 'selected' method, as is, does absolutely nothing. The 'act' method, as is, does set 'Charselect' in the World to '1' when the character is clicked on, but that is it. I think you wanted the 'selected' method to retrieve the value of 'Charselect' from the world, in which case the method needs a return type of the type that 'Charselect' in the world is: public int selected() Then, the 'selected' method will need a 'return int;' statement to return the value. The method would then look like this:
1
2
3
4
5
public int selected()
{
    L4_SpaceWorld wereld = (L4_SpaceWorld)getWorld();
    return wereld.getCharselect();
}
The act method with this 'selected' method would then be:
1
2
3
4
5
6
public void act()
{
    int charSelect = selected();
    if (Greenfoot.mouseClicked(this))
    { /* omitted code block */}
}
This method is quite short and can be reduced even further to a one-liner. It almost makes the method not worth having as you could then put that one line directly in the calling code instead of the call to the method. In other words, without the 'selected' method, your act method could then be:
1
2
3
4
5
public void act()
{
    int charSelect = ((L4_SpaceWorld)getWorld()).getCharselect();
    if (Greenfoot.mouseClicked(this)) ((L4_SpaceWorld)getWorld()).setCharselect(1);
}
Looking at this, we can see that the 'charSelect' variable is not used after being declared on line 3; and, therefore, it can be removed. So, we end up simply with the following 'act' method without a 'selected' method:
1
2
3
4
public void act()
{
    if (Greenfoot.mouseClicked(this)) ((L4_SpaceWorld)getWorld()).setCharselect(1);
}
joanne_wm joanne_wm

2014/11/28

#
thank you! so basically i put all the characters in a whole new world for the Selection Screen
joanne_wm joanne_wm

2014/11/28

#
but now it says that: cannot find symbol - method getCharselect() it highlighted line 2: last item
1
2
3
4
5
public void act() 
    int charSelect = ((L4_SpaceWorld)getWorld()).getCharselect(); 
    if (Greenfoot.mouseClicked(this)) ((L4_SpaceWorld)getWorld()).setCharselect(1); 
}
danpost danpost

2014/11/28

#
I cannot say why you are getting that message as you previously have shown that method and stated in was in your world -- unless it is not your 'L4_SpaceWorld' world that the method is in.
joanne_wm joanne_wm

2014/12/1

#
I entered this into the L4_Space World
1
2
3
4
5
6
7
8
9
10
  public int setCharselect(int x)   
   {   
       Charselect=x;   
       return Charselect;   
   }
    
public int getCharselect()   
   {   
       return Charselect;   
   }
And it says it cannot find variable Charselect
joanne_wm joanne_wm

2014/12/1

#
so basically i'm missing to define the Charselect variable but how and what do i do?
danpost danpost

2014/12/1

#
The first line shown below defines the field. The field will remain active for as long as the world object exists.
1
2
3
4
5
6
7
8
9
10
11
12
private int Charselect;
 
public int setCharselect(int x)   
{   
    Charselect = x;   
    return Charselect;   
}
 
public int getCharselect()   
{   
    return Charselect;   
}
joanne_wm joanne_wm

2014/12/1

#
thank you very much!
joanne_wm joanne_wm

2014/12/1

#
so now i want to do the selection in "SelectionWorld" (like the codes working) how do i make the game start in the "SelectionWorld", after the Selection is done, goes to "SpaceWorld"?
There are more replies on the next page.
1
2
3
4