1

I have these classes which is the 3rd and 4th child of a base class in my game:

/*
 * List of player states:

* * normal * explode */

public class LocalPlayer extends MovingObjects{

//minus 1 laps to start to accommodate going over the start line for the first time
public int lapsCompleted = -1;      

public LocalPlayer(int localPlayerNumber) {

    /*
     * This line below and the variable localPlayerNumber will not be needed or will be different
     * if later on decide to do custom rocket designs - not needed for this stage but bear in mind if the decision is
     * made to go down that road this and the game object will have to be reconsidered as early as possible.
     */
    super(16, "assets/images/player" + localPlayerNumber , ".jpg", new Vector(373, 450 + (55 * localPlayerNumber))); 


}

//basic constructor just initialises MovingObjects Variables to zero values
public LocalPlayer(){
    super();
}

///// Uploading Position Methods

public void update(){
    if(objectState == "explode"){

    }
    if(objectState == "normal"){

        super.update();

        //look for edge of map and stop player leaving and reduce speed to 0
        if(position.x > rightEdge - icon.getIconWidth()){
            position.x = rightEdge - icon.getIconWidth();
            speed = 0;
        }else{
            if(position.x < leftEdge){
                position.x = leftEdge;
                speed = 0;
            }
        }

        if(position.y > downEdge - icon.getIconHeight()){
            position.y = downEdge - icon.getIconHeight();
            speed = 0;
        }else{
            if(position.y < upEdge){
                position.y = upEdge;
                speed = 0;
            }
        }
    }       
}

///// Movement Methods

//Increases speed
public void up(){
    if(speed == 0){
        speed = 2;// 2 is minimum speed to achieve all driving angles
    }else
    if(speed < 11){
        speed++;
    }
}

//Decreases speed
public void down(){
    if(speed > 2){
        speed--;
    }else{
        speed = 0;
    }
}

//Turns image and angle 22.5 degrees to the right
public void right(){
    if(angle == 337.5)
    {
        angle = 0;
        imageNumber = 0;
    }else{          
        angle = angle + 22.5;
        imageNumber = imageNumber + 1;
    }
}

//Turns image and angle 22.5 degrees to the left
public void left(){
    if(angle == 0)
    {
        angle = 337.5;
        imageNumber = 15;
    }else{          
        angle = angle - 22.5;
        imageNumber = imageNumber - 1;
    }
}   

// works out start grid currently only vertical lines
    //add more levels either start all the same way or needs updating
    public Vector getStartPos(int serverNumber, CheckPoints line)
    {
        int row ;
        Vector vReturn;
        if (serverNumber % 2 == 0) {
            // even
            row = serverNumber/2;

            //this needs some explaining:
            //vectorX  = row * width of image * gap to next player
            //vectorY = getline y and inset slightly(will see how goes)
            vReturn  =  new Vector((line.pos1.x + 10 ) - row * (50 + 10), line.pos1.y + 5);             
        } else {
            // odd
            row = (serverNumber + 1)/2;     
            vReturn  =  new Vector((line.pos2.x  +10) - row * (50 + 10), line.pos2.y - 55);             
        }           
            return vReturn;
    }
}

and:

 import java.awt.image.BufferedImage;


 public class NetworkedLocalPlayer extends LocalPlayer{

RocketSpecificServer server = new RocketSpecificServer();   

public NetworkedLocalPlayer(String ipAddress,int numOfImages,String filePre, String fileE, CheckPoints finishLine) {

    //sign player into server
    server.setUpNetwork(ipAddress); 

    LoadContent.gameSettings = (GameSettings) server.signIn();

    //get server number (needed for initialising)
    serverNumber = Integer.parseInt(((StringReturnSerial)server.getServerNumber()).s);

    //this is temp and should be changed later!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //filePre = filePre + serverNumber;
    filePre = filePre + 1;

    //Initialise image variables
    imageVariables(numOfImages,filePre,fileE);

    //work out pos
    position = getStartPos(serverNumber,finishLine);

    //get images
    initImageArray();

}

public void update(){
    //updates players info table checks the state of this object 
    //hasn't been changed by another player
    LoadContent.serverPlayerInfo =(PlayerPositionsSerial) server.getPos();
    objectState = LoadContent.serverPlayerInfo.playersArray[serverNumber][4];

    //usual update 
    super.update();

    //updates server        
    LoadContent.serverPlayerInfo = (PlayerPositionsSerial) server.updatePos(angle, position.x, position.y,objectState);
}
}

The problem is the update method. In the NetworkedLocalPLayer player class it asks the server for a serialised class which holds all the players positions in the game (and someother bits) that is assigned to the static version in LoadContent class which implements my gameloop. the update method then checks its own position in the playertable array to update its own objectState variable which I can see when using breakpoints comes back as its supposed to as "normal". It then call the parents update method which starts executing and will stop on line in the LocalPlayer class :

if(objectState == "normal"){

the next line is :

super.update();

which should call the super update method of the MovingObjects class which i can also provide if you think it will help but bassically the I have a break opint inside the MovingObjects class and on the super.update() call in the LocalPlayer class which never get hit.

When i change a variable in the game loop to turn multiplayer off which has nothing to do with this but directly used the localPlayer class as it is this method fire perfectly fine and has done unchanged for ages. So is there a reason I cant use super like this? im now getting any errors or exceptions

Thank you

for any help John harris

john
  • 157
  • 1
  • 12
  • It probably is a duplicate of that but I didnt realize there was anything wrong with comaring strings like this – john Jul 01 '13 at 13:10

2 Answers2

3

Never compare Strings with ==. Do it like this (assuming objectState cannot be null - otherwise you could write "explode".equals(objectState) but I find it harder to read):

public void update(){
    if(objectState.equals("explode")){

    }
    if(objectState.equals("normal")){
        super.update();
        // ...
    }
}

If using Java 7, you can also switch on Strings (have a look at the changes introduced with Java 7).

Community
  • 1
  • 1
Axel
  • 13,939
  • 5
  • 50
  • 79
1

You problem is that objectState == "normal" is always false.

Use "normal".equals(objectState) instead

Tala
  • 8,888
  • 5
  • 34
  • 38
  • Thank you so much i have come accross this before quick check and ill makr as answer thank you – john Jul 01 '13 at 13:07