[java] trouble x 10

Started by
7 comments, last by sushi-one 20 years, 3 months ago
Just write my question again in a new thread How can I (if possible) controll a variable in "SpaceInvader" class from my "player" class?? (I need to be able to control some of the game stated from the "Player" class?? I thought it only was to write "extends Player" in my "SpaceInvader" class, and use the variable as it was made in "SpaceInvader"..?? Hans-Petter
Hans-Petter Harveg
Advertisement
Could u write down some psudocode for what you want to do? I''m not usre I get hwat you want to say....
garazdawi - 'I put the laughter back in slaughter'

ok, this is preaty much like I have done it:

------------------------

class player {
final int intro=1;
final int playgame=2;
int state=intro;

void handleinput() {
if( keyDown ) {
// I want to change the state here
state = playgame;
}
}
}


class space extends player {

(and in the rungame method):
handleinput();

switch() {
case intro:
break;

case playgame:
break;
}

}

------------------------

The strange thing is, this is working if I change the state in the "player" constructor only, but if I change it in any other method, it do not work.. :/



Hans-Petter
Hans-Petter Harveg
try placing a super before referencing the variables in the space class. super.state=super.playgame or something lik ethat.... not sure if it will help at all, but maybe it''s wortha try....
garazdawi - 'I put the laughter back in slaughter'
no, i still can''t make it work.. hehe, the reason is probably just becaouse I''m very stupid, but is''t this like the heaven of OOP??

Hans-Petter
Hans-Petter Harveg
Did you forget something like private/protected/public ?

Variables in the base class must be public to allow access from outside, or protected if they shall be accessed from a derived class (like in your case).
Not specifying visability defaults to package visability.
quote:Original post by sushi-one
no, i still can''t make it work.. hehe, the reason is probably just becaouse I''m very stupid, but is''t this like the heaven of OOP??

Hans-Petter

The thing is that the way you describe it it should work.
public class test {        public static final int STATE_1=0;        public static final int STATE_2=1;        protected int state;        public test(){                state=STATE_1;        }                                                                                        public static void main(String[] arg){                test2 ts=new test2();        }}                                                                                class test2 extends test {        public test2(){                super();                System.out.println("Initial state is="+state);                System.out.println("Changing state...");                state=STATE_2;                System.out.println("Final state is="+state);        }}                                                                                

This code snippet works fine. Is this something like what you wanted or...?
garazdawi - 'I put the laughter back in slaughter'
jupp, thanx! It works fine now, really cool

Hans-Petter
Hans-Petter Harveg

This topic is closed to new replies.

Advertisement