0

我正在开发一款游戏,但遇到了一些问题。我的架构是这样的:

类 GameView 用于在我的表面上绘制位图 类 GameLoopThread 用于实现我的游戏循环(如果它不明显......) 类 MovementUtils 用于保存与移动对象相关的所有实用程序

我想在 MovementUtils 中包含重力和运动控制等方法,但我在实际更新 GameView 中的值时遇到了麻烦。我尝试使用意图,但无济于事。我会发布我的代码,也许有人可以告诉我我应该做什么。另外,忽略加速度计线,这完全是另一个问题......

游戏视图.java

package com.example.connorgame;


import java.util.EventObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;


public class GameView extends SurfaceView  {
private Bitmap platform;
private Bitmap character;
private Bitmap background;
private SurfaceHolder holder;

private GameLoopThread gameLoopThread;
private MainActivity mainactivity;
private MovementUtils moveutil;

public float charX = 0;
public float charY = 0;
private boolean isFalling = true;
private boolean isJumping = false;
private float initialJumpY = 0;

private int initialFrame;
private int currentFrame;
private int frameDifference;

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

    gameLoopThread = new GameLoopThread(this);
    mainactivity = (MainActivity) context;
    moveutil = new MovementUtils();

    holder = getHolder();
    holder.addCallback(new Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub

        }
    });



    platform = BitmapFactory.decodeResource(getResources(), R.drawable.platform);
    character = BitmapFactory.decodeResource(getResources(), R.drawable.character);
    background = BitmapFactory.decodeResource(getResources(), R.drawable.background);

}



    @Override 
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(background, 0, 0, null);
        canvas.drawBitmap(platform, 30, 700, null);
        canvas.drawBitmap(character, charX, charY, null);

        moveutil.gravity(charY, isFalling);

        if (charY > getHeight() - character.getHeight()) {
            initialFrame = gameLoopThread.numFrames;
            initialJumpY = charY;
            isFalling = false;
            isJumping = true;
        }

        if (isJumping == true && isFalling == false) {
            currentFrame = gameLoopThread.numFrames;
            frameDifference = (currentFrame - initialFrame);
            charY = charY - 5;

            if (charY == initialJumpY - 100) {
                isJumping = false;
                isFalling = true;
            }

        }

    }

}

MovementUtils.java

package com.example.connorgame;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class MovementUtils {


public void gravity (float charY, boolean isFalling) {
    if(isFalling == true){
        charY = charY + 5;
    }
}

}

4

2 回答 2

0

如果我理解您的操作正确,那么您正试图在GameView通过重力函数修改的类中使用 charY。问题是这float是一个原语,所以它是基于值的,只有本地副本会被修改。如果您改为传递一个对象,则该对象将被修改。

一种解决方案可能是只返回新的 Y 位置,而不是尝试gravity()更改 Y 位置。

另一种解决方案是传递GameViewtoMovementUtils并让MovementUtils修改它。

换句话说,你会像这样传递 GameViewnew MovementUtils(this);

而重力函数会void setCharY(int y);在 GameView 中调用 a。

public class MovementUtils {
    private GameView gameView;

    public MovementUtils(GameView view) {
        this.gameView = view;
    }

    public void gravity (float charY, boolean isFalling) {
        if(isFalling == true){
            charY = charY + 5;
            gameView.setCharY(charY);
        }
    }
}
于 2013-02-16T02:56:04.997 回答
0

Java 只支持passing by value

所以当你打电话moveutil.gravity(charY, isFalling);

并在函数内更改它,例如

public void gravity (float charY, boolean isFalling) {
if(isFalling == true){
    charY = charY + 5;
}

此类更改发生在变量的本地副本中,charY不会影响您的类charY中定义的实例变量GameView

为了解决这个问题,您可以将变量定义为对象以及函数的参数,例如

public void gravity (Float charY, boolean isFalling) {}

在你的 GameView 类中:

private Float charX = 0; // why you define them public !?
private Float charY = 0;

或者,您可以传递您的GameView类的实例或创建一个ValueObject类(类似于Contextandroid 中的对象)并使用它来传递参数

于 2013-02-16T02:56:12.543 回答