为什么代码会破坏?
简介:尝试制作“选择你自己的冒险”游戏。
布局由以下内容组成: bPage =按钮说明用户所在的页面。点击手动更改页面(尚未实现) bOne:通过第一条道路 bTwo:通过第二条道路 tvPageTitle:页面标题,在顶部 mainText:主要文本正文(这里讲的故事)
还有一个启动画面,效果很好,但是当你点击“开始”时,它会加载这个挂起的布局(可能是无限循环)
有人可以告诉我为什么吗?试图尽可能多地提出意见,所以它应该是自我解释的
请根据需要提出尽可能多的问题:)
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
TextView mainText;
int page = 1;
MediaPlayer pageflip;
Boolean gameOver = false;
int currentPage = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
//TODO: customise story to insert player name. Remember to import method
//EditText userName = LoginActivity.userName;
// Link XML Views with Java
final TextView mainText = (TextView) findViewById(R.id.mainText);
final TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
final Button bOne = (Button) findViewById(R.id.bOne);
final Button bTwo = (Button) findViewById(R.id.bTwo);
final Button bPage = (Button) findViewById(R.id.bPage);
// Array creation
final String[] storyText = new String[22];
final String[] pageTitle = new String[22];
final CharSequence[] pathA = new CharSequence[22];
final CharSequence[] pathB = new CharSequence[22];
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
// Loop until Game Over
do {
// Populate fields and play page flip sound
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
???
}
});
*/
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
} while (gameOver == false);
//TODO: Game Over screen
}
}
EDIT2:根据变化采纳了新的建议。现在布局加载,但单击按钮会使应用程序崩溃
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
int page;
int currentPage;
MediaPlayer pageflip;
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
// Array creation
String[] storyText = new String[22];
String[] pageTitle = new String[22];
CharSequence[] pathA = new CharSequence[22];
CharSequence[] pathB = new CharSequence[22];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Link XML Views with Java
TextView mainText = (TextView) findViewById(R.id.mainText);
TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
Button bOne = (Button) findViewById(R.id.bOne);
Button bTwo = (Button) findViewById(R.id.bTwo);
Button bPage = (Button) findViewById(R.id.bPage);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
???
}
});
*/
// Populate fields according to first page
page = 1;
currentPage = page;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
setPage(page);
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
}
private void setPage(int curPage)
{
// Populate fields and play page flip sound
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:1)
你的部分问题肯定是无限的loop
。
} while (gameOver == false);
但你永远不会gameOver == true
。但我会重新考虑你的设计。而不是在loop
中使用此功能,看起来您可以使用一个函数来设置TextView
的文本并执行此操作并从onClick
调用该函数来传递函数需要选择正确页面的int
等...没有理由在OnClickListener
中不断设置loop
。它可以在onCreate()
中完成一次。
修改强>
private void setPage(int curPage)
{
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
然后在onClick()
调用函数
bOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
...
}
setPage(page);
在(onCreate()
)之前将变量声明为成员变量,但在onCreate()
中初始化它们
public class Remember extends Activity {
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
mainText = (TextView) findViewById(R.id.mainText);
tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
bOne = (Button) findViewById(R.id.bOne);
bTwo = (Button) findViewById(R.id.bTwo);
bPage = (Button) findViewById(R.id.bPage);
在使用View
layout
之前,您无法初始化setContentView()
你有
TextView mainText = (TextView) findViewById(R.id.mainText);
在onCreate()
内。将其更改为
mainText = (TextView) findViewById(R.id.mainText);
删除TextView
否则您正在创建一个局部变量,而您的成员变量没有值,因此您将获得NPE
。对您的所有View
执行相同的操作。
答案 1 :(得分:1)
如果我正确地遵循你的代码,你似乎从第1页开始,然后循环。 你似乎依赖休息;退出do-while,但你只是设置onclicklistener。你真的点击了什么吗? bOne或b两个我的意思。如果你不点击它们,无限循环。