使用另一个类中的方法处理活动中的变量

时间:2013-02-16 02:51:17

标签: android variables methods dynamic-variables

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

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

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

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

}

2 个答案:

答案 0 :(得分:0)

如果我理解你正在做什么,你就试图在由引力函数修改的GameView类中使用charY。问题是float是一个原语,因此它基于值,只修改本地副本。如果您传递了一个对象,则该对象将被修改。

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

另一个解决方案是将GameView传递给MovementUtils并让MovementUtils修改它。

换句话说,你会像这样new MovementUtils(this);

传递GameView

引力函数会在GameView中调用void setCharY(int y);

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

答案 1 :(得分: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 1}}类

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

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

在你的GameView课程中:

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

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