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

2017/8/14

can i access a variable from another world class...

Albani,Absar Albani,Absar

2017/8/14

#
i am developing a high score system but i am separating it into another class,, world class. the problem is i cant access the variable from another world class help me... how can i access the variable from another world class in another
Albani,Absar Albani,Absar

2017/8/14

#
please help me... asap
danpost danpost

2017/8/14

#
Cannot help without seeing your code. Show the class where the variable is stored and the class where you are trying to access it. Also, indicate where you are trying to access it and what you are trying to do at that point in your code.
Albani,Absar Albani,Absar

2017/8/14

#
this is my cone in my gameover world class
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
26
27
28
play aw = new play();
private int score;
public gameOver()
{   
    // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
    super(500, 400, 1);
    addObject(aw, 300,280);
    Greenfoot.start();
 
    showScore();
 
}
 
public void act()
{
    if (Greenfoot.mouseClicked(aw)) Greenfoot.setWorld(new startup());
}
 
public void adScore(int points)
{
    score = score + points;
    showScore();
}
 
private void showScore()
{
    showText("Your Score:" + score, 80, 25);
}
in my game world class "animation start i have this code"..
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//--------------------------------------------------------------------
 
boolean isTurning;
 
private int time, timeshower, Stoper;
private int score;
public static int randomer = Greenfoot.getRandomNumber (30);
 
/**
 * Constructor for objects of class MyWorld.
 *
 */
public animationStart()
{   
    // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
    super(1000, 500, 1);
    setPaintOrder(floor.class);
    setPaintOrder(Border.class);
 
    start();
    score = 0;
    showScore();
    time = 0;
    Stoper = 0;
 
    isTurning = false;
 
}
 
private void showScore()
{
    showText("Score: " + score, 80, 25);
}
 
public void adScorer(int points)
{
    score = score + points;
    showScore();
}
a actor class 'man' which in the 'animationstart' world class which the player play as. has this code
1
2
3
4
5
6
else if (isTouching(sooe.class))
{
    removeTouching(sooe.class);
    animationStart newsc = (animationStart)getWorld();           
    newsc.adScorer(10);
}
now what i want is this actor man will also pass the score to the game over world class so thAT the game over world class will check if he has a bigger score than the previous player.
Albani,Absar Albani,Absar

2017/8/14

#
when i try to add this to the actor 'man'
1
2
3
4
5
6
7
8
else if (isTouching(sooe.class))
{
    removeTouching(sooe.class);
    animationStart newsc = (animationStart)getWorld();           
    newsc.adScorer(10);
    gameOver scor = ( gameOver)getWorld();           
    scor.adScorer(10);
}
it just keep saying that "animationStart cannot cast on gameOver"
danpost danpost

2017/8/14

#
The getWorld method returns the world that the man is in, if any, and you said yourself that the man in in an animationStart world (not a gameOver world). In fact, I do not see where you are even creating a gameOver type world. Even if you are starting in a gameOver type world (which goes to a startUp world, then to an animationStart world), any information stored in the gameOver world is lost as there currently is no static content in the gameOver class and all references to any gameOver world are lost when going to the startUp world. There does not seem to be any need to constantly update the score in gameOver until the animationStart world is done running. In fact, if it is to maintain a high score value in the gameOver class, it would make sense to leave the value alone until an animationStart game is finished; then, compare the current game score to the high score held by gameOver to see if a new high score has been achieved -- then update the high score, if needed. You can maintain a current session high score that is reset only when the project is recompiled by changing line 2 in the gameOver class code above to this:
1
public static int score;
Then in any class of your project, you can access it directly using 'gameOver.score'. For example, from your animationStart class, you could have this:
1
if (score > gameOver.score) gameOver.score = score;
which could also be written as this:
1
gameOver.score = Math.max(score, gameOver.score);
You need to login to post a reply.