簡體   English   中英

吸氣劑從活動類返回null

[英]Getters returning null from activity class

今天,我花了很多時間研究為什么我的吸氣劑返回null並且似乎找不到一個明確的答案。 我正在尋找使用類創建干凈的層次結構,並將信息從一個傳遞到另一個。 我不知道這是否是一種很好的做法,對於游戲開發和android studio來說還是新的。 我希望能夠從Game.java(這是我的活動類)中傳遞變量,而無需將其添加到其構造函數中的其他類中。 我希望能夠像其他類一樣訪問吸氣劑,但似乎無法解決。 完整的代碼將包含在下面: Game.java中的getXDirection和getyDirecion從玩家類返回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;
    }
}

播放器

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;
    }
}

邏輯庫

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");
        }
    }
}

感謝所有幫助!

這是您的問題,在您的GameView

userInput = new Game();

您指向的是活動的新實例,而不是顯示的實際活動。 在使用活動實例“ this”創建Player之后,需要設置此值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM