Android Eclipse改变蛇游戏上的文字

时间:2014-11-21 10:21:17

标签: android eclipse

当我的蛇吃食物时,我如何进入下一个文本? (当蛇吃食物时,文本将从测试变为成功。)我使用eclipse提供的蛇游戏。这是我到目前为止所做的代码。我正在为我的项目做这个,所以我感谢所有的帮助。

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Snake extends Activity {

/**
 * Constants for desired direction of moving the snake
 */
public static int MOVE_LEFT = 0;
public static int MOVE_UP = 1;
public static int MOVE_DOWN = 2;
public static int MOVE_RIGHT = 3;

private static String ICICLE_KEY = "snake-view";

private SnakeView mSnakeView;

/**
 * Called when Activity is first created. Turns off the title bar, sets up the content views,
 * and fires up the SnakeView.
 * 
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.snake_layout);
    init();

    mSnakeView = (SnakeView) findViewById(R.id.snake);
    mSnakeView.setDependentViews((TextView) findViewById(R.id.text),
            findViewById(R.id.arrowContainer), findViewById(R.id.background));

    if (savedInstanceState == null) {
        // We were just launched -- set up a new game
        mSnakeView.setMode(SnakeView.READY);
    } else {
        // We are being restored
        Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
        if (map != null) {
            mSnakeView.restoreState(map);
        }
    }

}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Store the game state
    outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
}

private int currentQuestion;
private String [] questions;
private TextView questionView;

public void init() {
    questions = new String[]{"testing","success"};
    currentQuestion = -1;
    questionView = (TextView) findViewById(R.id.QuestionTextView);
    showQuestion();
}
public void showQuestion() {
    currentQuestion++;
    if(currentQuestion == questions.length)
        currentQuestion =0;
    questionView.setText(questions[currentQuestion]);

    } 
}

1 个答案:

答案 0 :(得分:0)

在查看源代码后(我在这里做了http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.2_r1/com/example/android/snake/SnakeView.java),我不认为可以在不改变源代码的情况下添加此功能...

但是,如果您想编辑源代码以使其正常工作,我建议如下:

创建一个用作监听器的接口

public interface AppleEatenListener{
    public void appleEaten(int size);
}

然后将此接口的变量添加到SnakeView类

private AppleEatenListener mAppleEatenListener;

在SnakeView类中创建一个setter来设置mAppleEatenListener

public void setAppleEatenListener(AppleEatenListener listener){
    this.mAppleEatenListener = listener;
}

之后,请转到SnakeView类中的updateSnake()方法并找到以下代码段(考虑使用ctrl + f(搜索功能)):

此代码段来自当前源代码:

// except if we want the snake to grow
if (!growSnake) {
    mSnakeTrail.remove(mSnakeTrail.size() - 1);
}

在这里我们要补充一点,如果蛇应该增长,我们称之为AppleEatenListener函数appleEaten(),如下所示:

 // except if we want the snake to grow
if (!growSnake) {
    mSnakeTrail.remove(mSnakeTrail.size() - 1);
}
else{
    if(mAppleEatenListener != null){
        mAppleEatenListener.appleEaten(mSnakeTrail.size());
    }
}

现在,我们应该返回你自己的Snake类并在onCreate()中添加以下内容:

mSnakeView.setOnAppleEatenListener(new AppleEatenListener() {
        @Override
        public void appleEaten(int size) {
            showQuestion();
            //size is the current size of the snake after the apple was eaten
        }
    });

请注意,我已经添加了监听器请求" size"参数,我认为在吃掉苹果之后知道蛇的大小是有用的,因为我没有看到在API中请求蛇的大小的功能......

我还没有测试过这段代码,但我希望这对你至少有一点帮助