我已经在这方面工作了一段时间并在网上查找但无法找到任何东西。
我试图解决为什么我创建的Log
Player.Update()
始终返回0,而登录我的Setter报告的是正确的值。
相关代码应在下面。
MainActivity
package com.Frenchie.AnimatedSprite;
import ...
public class MainActivity extends Activity implements SensorEventListener {
//Accelerometer
private SensorManager senSensorManager;
private Sensor senAccelerometer;
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;
GameView gameView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
player = new Player();
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){
player.setDirection(DIRECTION_UP);
//Log.d("onSensorChanged", ""+ player.getDirection());
}
else if (sensorEvent.values[0] > 1){
player.setDirection(DIRECTION_DOWN);
}
else if (sensorEvent.values[1] < -1){
player.setDirection(DIRECTION_LEFT);
}
else if (sensorEvent.values[1] > 1){
player.setDirection(DIRECTION_RIGHT);
}
else{
player.setDirection(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) {
}
}
Player.java
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;
public Player (Context context, GameView gameView){
this.gameView = gameView;
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 Player() {
}
public void Update() {
Log.d("Player | Update", "" + direction);
if (direction == DIRECTION_UP){
y -= speed;
}
else if (direction == DIRECTION_DOWN){
y += speed;
}
else if (direction == DIRECTION_RIGHT){
x += speed;
}
else if (direction == DIRECTION_LEFT){
x -= speed;
}
else if (direction == 0){
}
}
public void UpdateAnim(){
currentFrame = ++currentFrame % SPRITE_COLUMNS;
srcX = currentFrame * width;
if (direction == DIRECTION_RIGHT){
srcY = 2 * height;
}
else if (direction == DIRECTION_LEFT){
srcY = 1 * height;
}
if (direction == DIRECTION_DOWN){
srcY = 0 * height;
}
else if (direction == 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;
}
public void setDirection(int mainDirection) {
this.direction = mainDirection;
Log.d("Setter", "" + direction);
}
}
GameView
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();
}
}
}
如果您需要更多信息,请与我们联系。