我正在创建一个使用名为lvl1p1的图像数组的游戏。这些图像被加载并在ImageView上使用。现在,我的问题是当播放器正确回答时,ImageView不会加载下一个图像阵列lvl1p2。
这是我的代码:
package com.bd.mpg;
import java.util.Arrays;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class GameWindow extends Activity {
//declarations
String ans;
boolean answer;
TypedArray imgs;
public int imgarray;
public int counter;
public int wincount;
final Context con = this;
boolean check;
public int totalstar;
String debugtext;
String debugtexta;
String[] lvl1p1ANS;
ImageView img;
ImageView imgbat;
Button submit;
EditText answerfield;
TextView debug;
//main code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_game_window);
lvl1p1ANS = getResources().getStringArray(R.array.lvl1p1ANS);
img = (ImageView)findViewById(R.id.imagegame);
imgbat = (ImageView)findViewById(R.id.imageView3);
submit = (Button)findViewById(R.id.submitbtn);
answerfield = (EditText)findViewById(R.id.answertb);
debug = (TextView)findViewById(R.id.leveltitle);
//level setter
if (wincount == 0){
imgarray = R.array.lvl1p1;
} else if (wincount == 1) {
imgarray = R.array.lvl1p2;
} else {
Toast.makeText(GameWindow.this,
"Beta!!!", Toast.LENGTH_SHORT).show();
}
//item_load_up
imgs = getResources().obtainTypedArray(imgarray);
img.setImageResource(imgs.getResourceId(9, 0));
//image_click
img.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
counter++; //energy counter
debugtext = String.valueOf(counter);
imgs = getResources().obtainTypedArray(imgarray);
if (counter == 8) {
imgbat.setImageResource(R.drawable.b1r);
img.setImageResource(imgs.getResourceId(1, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else if (counter == 7) {
imgbat.setImageResource(R.drawable.b2r);
img.setImageResource(imgs.getResourceId(2, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else if (counter == 6) {
imgbat.setImageResource(R.drawable.b3r);
img.setImageResource(imgs.getResourceId(3, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else if (counter == 5) {
imgbat.setImageResource(R.drawable.b4y);
img.setImageResource(imgs.getResourceId(4, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else if (counter == 4) {
imgbat.setImageResource(R.drawable.b5y);
img.setImageResource(imgs.getResourceId(5, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else if (counter == 3) {
imgbat.setImageResource(R.drawable.b6y);
img.setImageResource(imgs.getResourceId(6, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else if (counter == 2) {
imgbat.setImageResource(R.drawable.b7g);
img.setImageResource(imgs.getResourceId(7, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else if (counter == 1){
imgbat.setImageResource(R.drawable.b8g);
img.setImageResource(imgs.getResourceId(8, 0));
Toast.makeText(GameWindow.this,
debugtext, Toast.LENGTH_SHORT).show();
} else { //game_over_code
Toast.makeText(GameWindow.this,
"Game Over!", Toast.LENGTH_SHORT).show();
Intent go = new Intent(getApplicationContext(), GameOver.class);
startActivity(go);
}
}
});
submit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
ans = answerfield.getText().toString();
debugtexta = String.valueOf(imgarray);
if (ans.equals("FLASHLIGHT")){
} else {
Toast.makeText(GameWindow.this,
"bugok!", Toast.LENGTH_SHORT).show();
}
}
});
debug.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(GameWindow.this,
debugtexta, Toast.LENGTH_SHORT).show();
}
});
}
}
提前致谢。 :)
答案 0 :(得分:0)
您的onCreate
功能中有太多代码。
将所有设置值的代码移出onCreate
函数并输入新函数。
现在,您可以在onCreate
中调用该函数一次,稍后再想要重新启动值时。