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

2017/2/9

Text and variable

1
2
PenguinPower PenguinPower

2017/2/9

#
Hi guys, i need some help. public class Start extends Actor { public int language = 0; public Start() { GreenfootImage start = getImage(); start.setFont(new java.awt.Font("Helvetica", Font.PLAIN, 16)); start.drawString("Start", 11, 34); setImage(start); } public int getLanguage() { return language; } public void setLanguage(int value) { language = value; } Here i declared a variable language, set his value and return the value in an actor named Start. public void act() { if (Greenfoot.mouseClicked(this)) { MyWorld world =new MyWorld(); world.getObjects(Start.class).get(0).setLanguage(1); Greenfoot.setWorld(new Play()); } } Here i want to see if the actor Ro was clicked and set the language to the value 1. There will be another actor En which will set the value 2. public void act() { MyWorld world = new MyWorld(); getImage().scale(200,120); if(world.getObjects(Start.class).get(0).getLanguage() == 1) {GreenfootImage bej = getImage(); bej.setFont(new java.awt.Font("Helvetica", Font.PLAIN, 16)); bej.drawString("Bej", 11, 25); setImage(bej); if(Greenfoot.mouseClicked(this)) { } } else if(world.getObjects(Start.class).get(0).getLanguage() == 2) { if(Greenfoot.mouseClicked(this)) { } } } In the actor Bej i want to see if the value of language is 1, to add a text to the image (this doesn't work, there doesn't appear any text). What can i do? Did I checked in the correct way the value? Can you help me? And I hope you understood my problem. And i'm sorry for my english, i'm not very good :)
Super_Hippo Super_Hippo

2017/2/9

#
1
MyWorld world = new MyWorld();
This line creates a NEW world. I don't think that you want to create new worlds all the time and get the Start object in that new world rather than the Start object in the current world.
PenguinPower PenguinPower

2017/2/9

#
So, i have to delete the word "new"?
Super_Hippo Super_Hippo

2017/2/9

#
You could try to replace
1
new MyWorld()
with
1
(MyWorld) getWorld()
PenguinPower PenguinPower

2017/2/9

#
I get this error "java.lang.ClassCastException: Play cannot be cast to MyWorld"
Super_Hippo Super_Hippo

2017/2/9

#
In this case... why did you want to create a MyWorld at all? How are the worlds used?
PenguinPower PenguinPower

2017/2/9

#
I want to use the current MyWorld to access the value of language and see if it's equal with 1 or 2. Can I access the information in another way?
Super_Hippo Super_Hippo

2017/2/9

#
Ok, differently. You have a MyWorld world and a Play world. What is each world doing? Having a value stored in an Actor object in a different world is a bit complicated. But if you need to do that, you need a reference to the MyWorld world, for example passing it when creating the Play World (guessing that the world was changed from MyWorld to Play).
PenguinPower PenguinPower

2017/2/9

#
Ok, MyWorld is the menu of a game. Play is an other world where you practically play the game. The game is like this: after i press the start there will appear 2 buttons (Ro and En), to select the language of the game. If the player chose Ro, the language is 1, in the actor Turc, Bej (the colors) i want to see what language he chose to show the name of the color and i want to insert and audio file (in the language he selected) when he press the color. But the problem is i don't get how should I check it, should i use the Play world to check or it doesn't matter?
Super_Hippo Super_Hippo

2017/2/9

#
Since I am also programming a game with two languages right now which also has a menu and a play world, I would suggest you pass the menu to the play world (or just pass the language itself if it is the only thing which matters from the menu). You probably won't even need the Start class. Anyways, it could look like this:
1
Greenfoot.setWorld(new Play(language));
1
2
3
4
5
6
7
8
9
10
11
12
13
private int language;
 
public Play(int language)
{
    super(....);
    this.language = language;
    //create objects and everything
}
 
public int getLanguage()
{
    return language;
}
Then, whenever you want to get the language in an actor in the play world:
1
((Play)getWorld()).getLanguage()
PenguinPower PenguinPower

2017/2/9

#
The problem is Start is a button with lunch the game
Nosson1459 Nosson1459

2017/2/9

#
Have a variable in your world which can be accessed from your actors. When the Ro actor is clicked set the variable to 1 if the En actor is clicked then set the variable in your world to 2. In your other actors check what this variable is equal to then use it for the language of that actor.
danpost danpost

2017/2/9

#
I am not sure why you do not have the variable (with the related methods) for the language in the Play class. Then when a button is clicked, you can do this:
1
2
3
Play playWorld = new Play();
playWorld.setLanguage(1); // or 2 (in the other button)
Greenfoot.setWorld(playWorld);
This is very much like the last suggestion by Super_Hippo (which I did not see prior to posting this).
Super_Hippo Super_Hippo

2017/2/9

#
I thought about something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//fields
private Actor[] lang={new Actor() {}, new Actor() {}};
private Actor start=new Actor() {};
private int language = -1;
 
//when creating the world
addObject(lang[0], x, y);
lang[0].setImage("...");
addObject(lang[1], x, y);
lang[1].setImage("...");
addObject(start, x, y);
start.setImage("...");
 
//in act
for (int i=0; i<2; i++)
{
    if (Greenfoot.mousePressed(lang[i])
    {
        language = i;
    }
}
if (Greenfoot.mousePressed(start) && language!=-1)
{
    Greenfoot.setWorld(new Play(language));
}
Note that language is 0 or 1 now. If it has to be 1 and 2, you can simply use 'i+1' in line 19.
Nosson1459 Nosson1459

2017/2/10

#
danpost wrote...
This is very much like the last suggestion by Super_Hippo (which I did not see prior to posting this).
I didn't notice that post until just now from reading this.
There are more replies on the next page.
1
2