2014-11-21 26 views
0

當我的蛇吃了食物時,如何進入下一個文本? (當蛇吃掉食物時,文本將從測試變爲成功。)我正在使用eclipse提供的蛇遊戲。這是迄今爲止我所做的代碼。我正在爲我的項目做這個,所以我很感激所有我能得到的幫助。Android 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]); 

    } 
} 
+0

不知道你的問題是什麼? – KRUKUSA 2014-11-21 10:24:10

+0

現在我正在顯示文本「測試」,我想它變成「成功」,當我的蛇吃了食物 – Millefiore 2014-11-21 10:37:35

回答

0

檢查出的源代碼(我在這裏:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.2_r1/com/example/android/snake/SnakeView.java)後,我不認爲這是可以在不改變源代碼添加此功能...

然而,如果u要編輯源,使其工作,我建議如下:

創建一個接口作爲偵聽器來使用

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

然後添加此INTE的變量rface到SnakeView類

private AppleEatenListener mAppleEatenListener; 

在SnakeView類創建一個setter設置mAppleEatenListener

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

之後,讓你一路下跌到updateSnake()方法在SnakeView類,並找到下面的代碼段(考慮使用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()); 
    } 
} 

,我們應該回到自己的蛇類,並添加在的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 
     } 
    }); 

注意,我假定這可能是知道蛇的大小,有用它吃了一個後蘋果,因爲我沒有看到請求蛇的大小的函數n中的API要麼...

我沒有測試此代碼,但我希望這有助於ü至少有一點

+0

Omg謝謝它的作品!你有任何聯繫你的方法嗎?例如Skype或其他。我想問你一些與此有關的問題。 – Millefiore 2014-11-21 12:23:05

+0

隨時添加我:您可以在此鏈接中找到我的Skype名稱(在60分鐘後到期):http://pastebin.com/tvWHjrmD – Subler 2014-11-21 12:36:38