Java Can';t似乎将int值从主类返回给玩家类

Java Can';t似乎将int值从主类返回给玩家类,java,android,class,variables,return,Java,Android,Class,Variables,Return,我一直在尝试将MainActivity中的int值返回给我的玩家类,以确定他应该移动的方向以及应该使用我的精灵表中的哪组图像。在我的球员级别上,我似乎只得到了0分。我知道我的加速计代码正在工作,因为我已经单独测试过了,但是int没有从主活动传递到玩家。感谢您的帮助!多谢各位 MainActivity.java package com.Frenchie.AnimatedSprite; import ... public class MainActivity extends Activity i

我一直在尝试将MainActivity中的int值返回给我的玩家类,以确定他应该移动的方向以及应该使用我的精灵表中的哪组图像。在我的球员级别上,我似乎只得到了0分。我知道我的加速计代码正在工作,因为我已经单独测试过了,但是int没有从主活动传递到玩家。感谢您的帮助!多谢各位

MainActivity.java

package com.Frenchie.AnimatedSprite;

import ...

public class MainActivity extends Activity implements SensorEventListener {

    //Accelerometer
    private SensorManager senSensorManager;
    private Sensor senAccelerometer;

    private volatile int direction;

    private static int DIRECTION_STATIONARY = 0;
    private static int DIRECTION_DOWN = 1;
    private static int DIRECTION_LEFT = 2;
    private static int DIRECTION_RIGHT = 3;
    private static int DIRECTION_UP = 4;

    Player player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GameView gameView = new GameView(this);
        setContentView(gameView);

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

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor mySensor = sensorEvent.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            if (sensorEvent.values[0] < -1){
                direction = DIRECTION_UP;
            }
            else if (sensorEvent.values[0] > 1){
                direction = DIRECTION_DOWN;
            }
            else if (sensorEvent.values[1] < -1){
                direction = DIRECTION_LEFT;
            }
            else if (sensorEvent.values[1] > 1){
                direction = DIRECTION_RIGHT;
            }
            else{
                direction = DIRECTION_STATIONARY;
            }
            //Log.d("Player Update", "X:" +sensorEvent.values[0]+ " Y:" +sensorEvent.values[1]+ " Z:" +sensorEvent.values[2] + " Direction: " + direction);
        }
    }

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

    }

    public int getDirection() {
        return direction;
    }
}
package com.Frenchie.AnimatedSprite;

import...

public class GameView extends SurfaceView implements Runnable {

    private Canvas canvas;
    private SurfaceHolder surfaceHolder;
    private Thread thread;

    Player player;


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

        player = new Player(context, this);

        surfaceHolder = getHolder();

        thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        while (true) {
            Update();
            DrawCanvas();
        }
    }

    private void Update() {
        player.Update();
        player.UpdateAnim();
        Control();
    }

    private void DrawCanvas() {

        canvas = surfaceHolder.lockCanvas();
        if (surfaceHolder.getSurface().isValid()) {
            canvas.drawColor(Color.MAGENTA);
            canvas.drawBitmap(player.getSprite(), player.getSrc(), player.getDst(), null);
            surfaceHolder.unlockCanvasAndPost(canvas);
        } else {
            Log.d("Run", "Surface Invalid");
        }
    }

    private void Control() {
        try {
            thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.Frenchie.AnimatedSprite;

import ...

public class Player {

    private Bitmap sprite;
    private int x, y, speed;

    //Animation Variables
    private static final int SPRITE_ROWS = 4;
    private static final int SPRITE_COLUMNS = 4;
    private int currentFrame, width, height, srcY, srcX, direction;
    Rect src, dst;

    //Static Directions
    private static int DIRECTION_STATIONARY = 0;
    private static int DIRECTION_DOWN = 1;
    private static int DIRECTION_LEFT = 2;
    private static int DIRECTION_RIGHT = 3;
    private static int DIRECTION_UP = 4;

    GameView gameView;
    MainActivity mainActivity;

    public Player (Context context, GameView gameView){
        this.gameView = gameView;

        mainActivity = new MainActivity();

        x = 100;
        y = 500;

        speed = 30;

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

        width = sprite.getWidth() / SPRITE_COLUMNS;
        height = sprite.getHeight() / SPRITE_ROWS;
    }

    public void Update() {

        Log.d("Player | Update", "" + direction);
        if (mainActivity.getDirection() == DIRECTION_UP){
            y -= speed;
            direction = DIRECTION_UP;
        }

        else if (mainActivity.getDirection() == DIRECTION_DOWN){
            y += speed;
            direction = DIRECTION_DOWN;
        }
        else if (mainActivity.getDirection() == DIRECTION_RIGHT){
            x += speed;
            direction = DIRECTION_RIGHT;
        }
        else if (mainActivity.getDirection() == DIRECTION_LEFT){
            x -= speed;
            direction = DIRECTION_LEFT;
        }
    }

    public void UpdateAnim(){

        currentFrame = ++currentFrame % SPRITE_COLUMNS;
        srcX = currentFrame * width;

        if (mainActivity.getDirection() == DIRECTION_RIGHT){
            srcY = 2 * height;
        }
        else if (mainActivity.getDirection() == DIRECTION_LEFT){
            srcY = 1 * height;
        }
        if (mainActivity.getDirection() == DIRECTION_DOWN){

            srcY = 0 * height;
        }
        else if (mainActivity.getDirection() == DIRECTION_UP){
            srcY = 3 * height;
        }

        src = new Rect(srcX, srcY, srcX + width, srcY + height);
        dst = new Rect(x, y, x + width, y + height);
    }

    public Rect getSrc(){
        return src;
    }

    public Rect getDst(){
        return dst;
    }

    public Bitmap getSprite() {
        return sprite;
    }
}
Player.java

package com.Frenchie.AnimatedSprite;

import ...

public class MainActivity extends Activity implements SensorEventListener {

    //Accelerometer
    private SensorManager senSensorManager;
    private Sensor senAccelerometer;

    private volatile int direction;

    private static int DIRECTION_STATIONARY = 0;
    private static int DIRECTION_DOWN = 1;
    private static int DIRECTION_LEFT = 2;
    private static int DIRECTION_RIGHT = 3;
    private static int DIRECTION_UP = 4;

    Player player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GameView gameView = new GameView(this);
        setContentView(gameView);

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

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor mySensor = sensorEvent.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            if (sensorEvent.values[0] < -1){
                direction = DIRECTION_UP;
            }
            else if (sensorEvent.values[0] > 1){
                direction = DIRECTION_DOWN;
            }
            else if (sensorEvent.values[1] < -1){
                direction = DIRECTION_LEFT;
            }
            else if (sensorEvent.values[1] > 1){
                direction = DIRECTION_RIGHT;
            }
            else{
                direction = DIRECTION_STATIONARY;
            }
            //Log.d("Player Update", "X:" +sensorEvent.values[0]+ " Y:" +sensorEvent.values[1]+ " Z:" +sensorEvent.values[2] + " Direction: " + direction);
        }
    }

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

    }

    public int getDirection() {
        return direction;
    }
}
package com.Frenchie.AnimatedSprite;

import...

public class GameView extends SurfaceView implements Runnable {

    private Canvas canvas;
    private SurfaceHolder surfaceHolder;
    private Thread thread;

    Player player;


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

        player = new Player(context, this);

        surfaceHolder = getHolder();

        thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        while (true) {
            Update();
            DrawCanvas();
        }
    }

    private void Update() {
        player.Update();
        player.UpdateAnim();
        Control();
    }

    private void DrawCanvas() {

        canvas = surfaceHolder.lockCanvas();
        if (surfaceHolder.getSurface().isValid()) {
            canvas.drawColor(Color.MAGENTA);
            canvas.drawBitmap(player.getSprite(), player.getSrc(), player.getDst(), null);
            surfaceHolder.unlockCanvasAndPost(canvas);
        } else {
            Log.d("Run", "Surface Invalid");
        }
    }

    private void Control() {
        try {
            thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.Frenchie.AnimatedSprite;

import ...

public class Player {

    private Bitmap sprite;
    private int x, y, speed;

    //Animation Variables
    private static final int SPRITE_ROWS = 4;
    private static final int SPRITE_COLUMNS = 4;
    private int currentFrame, width, height, srcY, srcX, direction;
    Rect src, dst;

    //Static Directions
    private static int DIRECTION_STATIONARY = 0;
    private static int DIRECTION_DOWN = 1;
    private static int DIRECTION_LEFT = 2;
    private static int DIRECTION_RIGHT = 3;
    private static int DIRECTION_UP = 4;

    GameView gameView;
    MainActivity mainActivity;

    public Player (Context context, GameView gameView){
        this.gameView = gameView;

        mainActivity = new MainActivity();

        x = 100;
        y = 500;

        speed = 30;

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

        width = sprite.getWidth() / SPRITE_COLUMNS;
        height = sprite.getHeight() / SPRITE_ROWS;
    }

    public void Update() {

        Log.d("Player | Update", "" + direction);
        if (mainActivity.getDirection() == DIRECTION_UP){
            y -= speed;
            direction = DIRECTION_UP;
        }

        else if (mainActivity.getDirection() == DIRECTION_DOWN){
            y += speed;
            direction = DIRECTION_DOWN;
        }
        else if (mainActivity.getDirection() == DIRECTION_RIGHT){
            x += speed;
            direction = DIRECTION_RIGHT;
        }
        else if (mainActivity.getDirection() == DIRECTION_LEFT){
            x -= speed;
            direction = DIRECTION_LEFT;
        }
    }

    public void UpdateAnim(){

        currentFrame = ++currentFrame % SPRITE_COLUMNS;
        srcX = currentFrame * width;

        if (mainActivity.getDirection() == DIRECTION_RIGHT){
            srcY = 2 * height;
        }
        else if (mainActivity.getDirection() == DIRECTION_LEFT){
            srcY = 1 * height;
        }
        if (mainActivity.getDirection() == DIRECTION_DOWN){

            srcY = 0 * height;
        }
        else if (mainActivity.getDirection() == DIRECTION_UP){
            srcY = 3 * height;
        }

        src = new Rect(srcX, srcY, srcX + width, srcY + height);
        dst = new Rect(x, y, x + width, y + height);
    }

    public Rect getSrc(){
        return src;
    }

    public Rect getDst(){
        return dst;
    }

    public Bitmap getSprite() {
        return sprite;
    }
}
mainActivity=新的mainActivity()

在这里,您正在创建MainActivity的新实例。 此实例没有所需的方向信息

这不是在Android中传递活动信息的方式。您可能要做的是在MainActivity中创建一个播放器对象,并在MainActivity中设置播放器的方向

Player player = new Player();
在主活动中的
onSensorChanged(SensorEvent SensorEvent)
方法中-

player.setDirection(direction);
player.update();

您能否将代码缩短为a,并向我们指出设置
int
值的位置以及您尝试传递和接收该值的位置?在不知道
sensorEvent.values[0]
sensorEvent.values[1]
中的值的情况下,很难判断。您能告诉我们SensorChanged(SensorEvent SensorEvent)作为参数接收的是什么吗?@Pieter SensorEvent。值[0-2]由加速度计生成,工作正常,[0]是x,[1]是y,[2]是z。这就是我要找的。谢谢!唯一的问题是我不能像你说的那样添加玩家类,就像我在onCreate中添加它一样,我会出错,因为它不包含gameView和context,但是我似乎不能向onCreate方法添加context?你知道我如何将玩家类添加到主活动中吗?我不知道你所说的“似乎无法将上下文添加到onCreate方法”是什么意思。您可以使用getApplicationContext在活动中的任何函数中获取上下文。将此行添加到onCreate方法-
player=newplayer(getApplicationContext(),gameView)