I have a problem .When i try to store a global public static value which i try to acces from another class.
In class Move i have the function from button event.
import static monopoly.Interface.zar1;
import static monopoly.Interface.zar2;
public class Move {
public int rolezar1;
public int rolezar2;
public int location=0;
public int overstart=0; //after start
public int untilstart=0; //until start
public int locbefore=0; //save location before passing over start
public void End(java.awt.event.ActionEvent evt) {
}
public void RollAction(java.awt.event.ActionEvent evt) {
Dice dice = new Dice();
rolezar1 = dice.RollDice();
rolezar2 = dice.RollDice();
zar1.setText(Integer.toString(rolezar1));
zar2.setText(Integer.toString(rolezar2));
Integer pfinal = rolezar2 + rolezar1;
int[] posx = new int[]{95, 145, 195, 245, 295, 345, 395, 445, 495, 495, 495, 495, 495, 495, 495, 495, 495, 495, 445, 395, 345, 295, 245, 195, 145, 95, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45};
int[] posy = new int[]{60, 60, 60, 60, 60, 60, 60, 60, 60, 110, 160, 210, 260, 310, 360, 410, 460, 510, 510, 510, 510, 510, 510, 510, 510, 510, 510, 460, 410, 360,310, 260, 210, 160, 110, 60};
int i = 0;
int loc = rolezar1 + rolezar2;
while(i <loc){
int j=0;
if (i+location==posx.length){
untilstart=posx.length-location;
overstart=loc - untilstart;
locbefore=location;
location=0;
}
else{
overstart=0;
untilstart=0;
}
while(j<untilstart){
Interface.Player1.setBounds(posx[locbefore+j], posy[locbefore+j], 20, 20);
j++;
i++;
}
j=0;
while(j<overstart){
Interface.Player1.setBounds(posx[location+j], posy[location+j], 20, 20);
j++;
i++;
}
if(overstart==0&&untilstart==0){
Interface.Player1.setBounds(posx[location+i], posy[location+i], 20, 20);
i++;
}
}
location+=overstart;
}
public void move(){
}
}
when i call the function in Main class the variable does not retain it's latest value
private void RollActionPerformed(java.awt.event.ActionEvent evt) {
Move apel = new Move();
apel.RollAction(evt);
}
Now the question: How can I use the variable from class Move in Main class correctly ?. When i put this code in Main class it's work.
I don't find any static members in your Move class . only class attribute variable exists . For your program to run as expected
Make Dice as Move member, Not method local i,e
cut Dice dice = new Dice();
and past it along with other class member like this and try
public int rolezar1;
public int rolezar2;
public int location=0;
public int overstart=0; //after start
public int untilstart=0; //until start
public int locbefore=0
Dice dice = new Dice() // Now this may work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.