Archived

This topic is now archived and is closed to further replies.

[java] trouble x 10

Recommended Posts

sushi-one    100
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

Share on other sites
garazdawi    145
Could u write down some psudocode for what you want to do? I''m not usre I get hwat you want to say....

Share on other sites
sushi-one    100

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

Share on other sites
garazdawi    145
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....

Share on other sites
sushi-one    100
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

Share on other sites
Guest Anonymous Poster
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).

Share on other sites
Guest Anonymous Poster
Not specifying visability defaults to package visability.

Share on other sites
garazdawi    145
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...?