Jump to content
  • Advertisement

Archived

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

sushi-one

[java] trouble x 10

This topic is 5416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
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...?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!