2017-12-19 25 views
1

今日、私のゲッターがヌルを返してきて、きれいな答えを見つけることができないように、私は多くの時間を費やしました。私はクラスを使用してクリーンな階層を作成し、一方から他方に情報を渡すことを検討しています。私はこれがゲームの開発とアンドロイドスタジオではまだまだ新しいことであるかどうかは分かりません。私は、自分のコンストラクタ内の他のクラスに追加することなく、Game.java(私のアクティビティクラス)の変数を渡すことができるようにしたいと思います。別のクラスのようにゲッターにアクセスできるようにしたいが、どうやってどうやったらうまくいくか分からない。完全なコードは、以下に含まれます:Game.javaでgetXDirectionとgetyDirecionは、彼らがアクティビティクラスからnullを返すゲッター

を初期化されていないimplysプレイヤークラスから0に戻ってきているGame.java

package com.Frenchie.SpaceshipSammy; 

import ... 

public class Game extends Activity implements SensorEventListener{ 

    private SensorManager senSensorManager; 
    private Sensor senAccelerometer; 

    //Directional constants 
    private static final int DIRECTION_STATIONARY = 0; 
    private static final int DIRECTION_LEFT = 1; 
    private static final int DIRECTION_RIGHT= 2; 
    private static final int DIRECTION_UP = 3; 
    private static final int DIRECTION_DOWN= 4; 

    //Direction variables 
    private int yDirection, xDirection; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Set view to GameView 
     setContentView(new GameView(this)); 

     //Sensor stuff 
     senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_GAME); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent motionEvent) { 
     switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
       //Finger down - Move up 
       yDirection = DIRECTION_UP; 
       break; 
      case MotionEvent.ACTION_UP: 
       //Finger lifted - Move down 
       yDirection = DIRECTION_DOWN; 
       break; 
     } 
     return true; 
    } 

    //Overriding Accelerometer to read data 
    @Override 
    public void onSensorChanged(SensorEvent sensorEvent) { 
     Sensor mySensor = sensorEvent.sensor; 

     if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
      float x = sensorEvent.values[1]; 

      if (x > -1 && x < 1) { 
       //Stationary 
       xDirection = DIRECTION_STATIONARY; 
      } else if (x >= 1) { 
       //Move right 
       xDirection = DIRECTION_RIGHT; 
      } else if (x <= -1) { 
       //Move left 
       xDirection = DIRECTION_LEFT; 
      } else { 
       Log.d("onSensorChanged", "Escaped"); 
      } 
     } 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int i) { 

    } 

    //Getters for Player class to use 
    public int getyDirection() { 
     return yDirection; 
    } 

    public int getxDirection() { 
     return xDirection; 
    } 
} 

プレーヤー.java

package com.Frenchie.SpaceshipSammy; 

import ... 

public class Player { 

    //Directional constants 
    private static final int DIRECTION_STATIONARY = 0; 
    private static final int DIRECTION_LEFT = 1; 
    private static final int DIRECTION_RIGHT = 2; 
    private static final int DIRECTION_UP = 3; 
    private static final int DIRECTION_DOWN = 4; 

    //Location variables 
    private int x, y, speed; 

    //Sprite 
    private Bitmap sprite; 

    private Game userInput; 

    public Player(Context context){ 

     speed = 10; 

     userInput = new Game(); 

     sprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.player); 
    } 

    //Called from Logic to move players location 
    public void PositionUpdate(){ 
     xMove(); 
     yMove(); 
    } 

    private void xMove(){ 
     if (userInput.getxDirection() == DIRECTION_STATIONARY){ 
      //Stationary 
     } 
     else if (userInput.getxDirection() == DIRECTION_RIGHT){ 
      //Move right 
      x += speed; 
      Log.d("xMove","Right"); 
     } 
     else if (userInput.getxDirection() == DIRECTION_LEFT){ 
      //Move left 
      x -= speed; 
     } 
     else { 
      Log.d("xMove", "xDirection unrecognised"); 
     } 
    } 

    private void yMove(){ 
     if (userInput.getyDirection() == DIRECTION_UP){ 
      //Move up 
      y -= speed; 
     } 
     else if (userInput.getyDirection() == DIRECTION_DOWN){ 
      //Move down 
      y += speed; 
     } 
     else{ 
      //Log.d("yMove", "yDirection unrecognised"); 
     } 
    } 

    //Get x and y for Logic 
    public int getX() { 
     return x; 
    } 

    public int getY() { 
     return y; 
    } 

    public Bitmap getSprite() { 
     return sprite; 
    } 
} 

Logic.java

package com.Frenchie.SpaceshipSammy; 

import ... 

public class Logic implements Runnable { 

    //Bring in required classes 
    private Player player; 

    //Player variables 
    private int yPlayer, xPlayer; 
    private Bitmap playerSprite; 

    public Logic(Context context){ 

     player = new Player(context); 

     //Sprite currently wont change so this doesn't need to be updated with the location 
     playerSprite = player.getSprite(); 

     //Creating and running thread 
     Thread thread = new Thread(this); 
     thread.start(); 
    } 

    //Thread to tell the players position update method to run and update the players values in this class 
    @Override 
    public void run() { 
     while(true) { 
      player.PositionUpdate(); 
      PlayerLocation(); 
     } 
    } 

    //Updates the players location which can be passed onto GameView 
    public void PlayerLocation(){ 

     xPlayer = player.getX(); 
     yPlayer = player.getY(); 
    } 

    //Getters for GameView to use 
    public int getyPlayer() { 
     return yPlayer; 
    } 

    public int getxPlayer() { 
     return xPlayer; 
    } 

    public Bitmap getPlayerSprite() { 
     return playerSprite; 
    } 
} 

GameView.java

package com.Frenchie.SpaceshipSammy; 

import ... 

public class GameView extends SurfaceView implements Runnable{ 

    private SurfaceHolder surfaceHolder = getHolder(); 
    private Canvas canvas; 

    //Link Logic class 
    private Logic logic; 

    public GameView(Context context) { 
     super(context); 

     //Creates logic as a new object 
     logic = new Logic(context); 

     //Creates and starts the thread 
     Thread thread = new Thread(this); 
     thread.start(); 

    } 

    //Override thread method. This is called when the thread is started 
    @Override 
    public void run() { 
     while (true){ 
      DrawFrame(); 
     } 
    } 


    private void DrawFrame(){ 
     canvas = surfaceHolder.lockCanvas(); 
     if (surfaceHolder.getSurface().isValid()){ 
      canvas.drawColor(Color.MAGENTA); 
      canvas.drawBitmap(logic.getPlayerSprite(), logic.getxPlayer(), logic.getyPlayer(), null); 
      surfaceHolder.unlockCanvasAndPost(canvas); 
     } else { 
      Log.d("DrawFrame", "Surface Invalid"); 
     } 
    } 
} 

すべてのヘルプは歓迎です!

+1

getterはnullを返しますか? – elmorabea

+0

Game.javaのgetyDirectionおよびgetxDirection。ありがとうございます。これは決してわかりません@elmorabea – Frenchie

答えて

0

これはあなたのGameView

userInput = new Game(); 

にあなたがあなたの活動の新しいインスタンスではなく、表示されている実際の活動を指している、あなたの問題です。アクティビティインスタンス 'this'を使用してPlayerを作成した後、この値を設定する必要があります。

+0

ありがとう、私はあなたが変数として 'Game userInput'を宣言し、 'userInput = new Game(this);'ゲームの_Game()が適用できないと言う 'this'の下に赤い線が表示されます。どうして私はあなたを完全に恋に落としているのですか? – Frenchie

関連する問題