我的 Android 游戏崩溃了(不幸的是停止了)
My Android Game is Crashing (Unfortunately stopped)
我的游戏有问题。单击新游戏按钮 (btnnewgame2) 后它停止了。单击它后,它应该会进入我的主要游戏 LunetaPark1。如果我的代码太长(实际上是整个代码),我很抱歉。请看一看。我不知道是什么问题。一旦我找到了这个问题的答案,我的另一个 activity 将 运行 smoothly.Please 帮助我。
NewGame1
public class NewGame1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_new_game1);
}
public void action(View v){
int id = v.getId();
if (id==R.id.btnnewgame2){
Intent intent= new Intent(this,LunetaPark1.class);
startActivity(intent);
}
if (id==R.id.btnhighscore1){
Intent intent= new Intent(this,HighScore.class);
startActivity(intent);
}
if (id==R.id.btnexit3){
Intent intent= new Intent(this,MainActivity.class);
startActivity(intent);
}
if (id==R.id.btnback7){
Intent intent= new Intent(this,LvlForm1.class);
startActivity(intent);
}
}
}
LunetaPark1
public class LunetaPark1 extends Activity {
static MediaPlayer mp1;
MediaPlayer jump;
MediaPlayer takecoin;
GameLoopThread3 gameLoopThread;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// for no title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameView(this));
}
public class GameView extends SurfaceView {
Bitmap bmp;
Bitmap background, taya, note1;
Bitmap run1;
Bitmap run6;
Bitmap jump2;
Bitmap coin;
Bitmap exit;
// MediaPlayer mp1,jump,takecoin;
private SurfaceHolder holder;
// private gameloop gameLoopThread;
private int x = 0, y = 0, z = 0, delay = 0, getx, gety, sound = 1;
int show = 0, sx, sy = 0;
int cspeed = 0, kspeed = 0, gameover = 0;
int score = 0, health = 120, reset = 0;
private int min = 1, sec = 0;
private Handler mHandler = new Handler();
private Runnable mRunnable;
private boolean isStop = false;
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public GameView(Context context) {
super(context);
mHandler.postDelayed(mRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(sec <= 0){
if(min > 0){
sec = 59;
min--;
isStop = false;
}
else{
//stop timer here
Log.e("TIMER", "timer stop!");
mHandler.removeCallbacks(this);
isStop = true;
}
}
else{
sec--;
isStop = false;
}
Log.i("TIMER", "min: " + min + " sec: " + sec);
if(!isStop){
mHandler.postDelayed(this, 1000);
}
}
}, 1000);
gameLoopThread = new GameLoopThread3(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@SuppressWarnings("deprecation")
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// for stoping the game
gameLoopThread.setRunning(false);
gameLoopThread.getThreadGroup().interrupt();
}
@SuppressLint("WrongCall")
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
// getting the screen size
Display display = getWindowManager().getDefaultDisplay();
sx = display.getWidth();
sy = display.getHeight();
;
cspeed = x / 4;
kspeed = x / 4;
background = BitmapFactory.decodeResource(getResources(),
R.drawable.imgluneta);
run1 = BitmapFactory
.decodeResource(getResources(), R.drawable.run1);
run6 = BitmapFactory
.decodeResource(getResources(), R.drawable.run6);
jump2 = BitmapFactory.decodeResource(getResources(),
R.drawable.run11);
coin = BitmapFactory
.decodeResource(getResources(), R.drawable.coin);
exit = BitmapFactory
.decodeResource(getResources(), R.drawable.exit);
taya = BitmapFactory
.decodeResource(getResources(), R.drawable.taya);
note1 = BitmapFactory.decodeResource(getResources(),
R.drawable.note1);
exit = Bitmap.createScaledBitmap(exit, 25, 25, true);
background = Bitmap
.createScaledBitmap(background, 2 * sx, sy, true);
// health dec
note1 = Bitmap.createScaledBitmap(note1, sx, sy, true);
mp1 = MediaPlayer.create(LunetaPark1.this, R.raw.game);
jump = MediaPlayer.create(LunetaPark1.this, R.raw.jump);
takecoin = MediaPlayer.create(LunetaPark1.this, R.raw.cointake);
}
// on touch method
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
show = 1;
getx = (int) event.getX();
gety = (int) event.getY();
// exit
if (getx < 25 && gety < 25) {
System.exit(0);
}
// sound off
if (getx > 25 && getx < 60) {
if (gety < 25) {
sound = 0;
mp1.stop();
}
}
// sound on
if (getx > 61 && getx < 90) {
if (gety < 25) {
sound = 1;
}
}
// restart game
if (getx > 91 && gety < 25) {
if (health <= 0) {
gameLoopThread.setPause(0);
health = 100;
score = 0;
}
}
}
return true;
}
@SuppressLint("WrongCall")
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
// background moving
z = z - 10;
if (z == -sx) {
z = 0;
canvas.drawBitmap(background, z, 0, null);
} else {
canvas.drawBitmap(background, z, 0, null);
}
// running player
x += 5;
if (x == 20) {
x = 5;
}
if (show == 0) {
if (x % 2 == 0) {
int height = run1.getHeight();
canvas.drawBitmap(run1, sx / 16, (15 * sy / 18) - height / 2, null);
// kinfe hit
if (kspeed == 20) {
kspeed = sx;
health -= 25;
canvas.drawBitmap(note1, 0, 0, null);
}
} else {
int height = run6.getHeight();
canvas.drawBitmap(run6, sx / 16, (15 * sy / 18) - height / 2, null);
// kinfe hit
if (kspeed == 20) {
kspeed = sx / 2;
health -= 25;
canvas.drawBitmap(note1, 0, 0, null);
}
}
}
// for jump
if (show == 1) {
if (sound == 1) {
jump.start();
}
int height=jump2.getHeight();
canvas.drawBitmap(jump2, sx / 16, (3 * sy / 4)-height, null);
// score
if (cspeed <= (sx / 8)-height/2 && cspeed >= (sx / 16)-height/2) {
if (sound == 1) {
takecoin.start();
}
cspeed = sx / 2;
score += 10;
}
// jump-hold
delay += 1;
if (delay == 3) {
show = 0;
delay = 0;
}
}
// for coins
cspeed = cspeed - 5;
if (cspeed == -sx / 2) {
cspeed = sx / 2;
canvas.drawBitmap(coin, cspeed, 3 * sy / 4, null);
} else {
canvas.drawBitmap(coin, cspeed, 3 * sy / 4, null);
}
// kinfe
kspeed = kspeed - 20;
int height = taya.getHeight();
canvas.drawBitmap(taya, kspeed, (15 * sy / 18) - height / 2, null);
if (kspeed < 0) {
kspeed = sx;
health -= 25;
}
// score
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setTextSize(15);
paint.setTextAlign(Align.LEFT);
canvas.drawText("Score :" + score, 3 * sx / 4, 20, paint);
// exit
canvas.drawBitmap(exit, 0, 0, null);
if (sound == 1) {
mp1.start();
mp1.setLooping(true);
}
// health
Paint myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStrokeWidth(10);
canvas.drawText("Health :" + health, 0, (sy / 8) - 5, myPaint);
canvas.drawRect(0, sy / 8, health, sy / 8 + 10, myPaint);
// game over
if (health <= 0) {
gameover = 1;
mp1.stop();
canvas.drawText("GAMEOVER OVER", sx / 2, sy / 2, myPaint);
canvas.drawText("YOUR SCORE : " + score, sx / 2, sy / 4,
myPaint);
canvas.drawText("Restart", 91, 25, myPaint);
gameLoopThread.setPause(1);
}
// restart
if (reset == 1) {
gameLoopThread.setPause(0);
health = 100;
score = 0;
}
// timer
Paint mypaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStrokeWidth(10);
mypaint.setTextSize(50);
canvas.drawText("Timer :" + min + sec,min, sec, mypaint);
}
}
@Override
public void onResume() {
super.onResume();
LunetaPark1.mp1.isPlaying();
}
@Override
public void onPause() {
super.onPause();
LunetaPark1.mp1.pause();
}
}
GameLoopThread3
public class GameLoopThread3 extends Thread
{
private GameView view;
static final long FPS = 10;
private boolean running = false;
boolean isPaused;
public GameLoopThread3(GameView view)
{
this.view = view;
}
public void setRunning(boolean run)
{
running = run;
}
public void setPause(int i)
{
synchronized (view.getHolder())
{
if(i==0)
{
isPaused=false;
}
if(i==1)
{
isPaused = true;
}
}
}
@SuppressLint("WrongCall")
@Override
public void run()
{
long ticksPS = 100;
long startTime = 0;
long sleepTime;
while (running)
{
//pause and resume
if (isPaused)
{
try
{
this.sleep(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
Canvas c = null;
startTime = System.currentTimeMillis();
try
{
c = view.getHolder().lockCanvas();
synchronized (view.getHolder())
{
view.onDraw(c);
}
}
finally
{
if (c != null)
{
view.getHolder().unlockCanvasAndPost(c);
}
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try
{
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
}
catch (Exception e) {}
}
}
}
Logcat
01-24 20:20:14.408: E/AndroidRuntime(1242): FATAL EXCEPTION: main
01-24 20:20:14.408: E/AndroidRuntime(1242): java.lang.IllegalStateException: Could not execute method of the activity
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View.onClick(View.java:3599)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View.performClick(View.java:4204)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View$PerformClick.run(View.java:17355)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.os.Handler.handleCallback(Handler.java:725)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.os.Looper.loop(Looper.java:137)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 20:20:14.408: E/AndroidRuntime(1242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-24 20:20:14.408: E/AndroidRuntime(1242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-24 20:20:14.408: E/AndroidRuntime(1242): at dalvik.system.NativeStart.main(Native Method)
01-24 20:20:14.408: E/AndroidRuntime(1242): Caused by: java.lang.reflect.InvocationTargetException
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View.onClick(View.java:3594)
01-24 20:20:14.408: E/AndroidRuntime(1242): ... 11 more
01-24 20:20:14.408: E/AndroidRuntime(1242): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.luksongbakagame/com.example.luksongbakagame.LunetaPark1}; have you declared this activity in your AndroidManifest.xml?
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivityForResult(Activity.java:3370)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivityForResult(Activity.java:3331)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivity(Activity.java:3566)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivity(Activity.java:3534)
01-24 20:20:14.408: E/AndroidRuntime(1242): at com.example.luksongbakagame.NewGame1.action(NewGame1.java:30)
01-24 20:20:14.408: E/AndroidRuntime(1242): ... 14 more
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luksongbakagame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.luksongbakagame.Splash"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.luksongbakagame.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.luksongbakagame.StartForm"
android:label="@string/title_activity_start_form" >
</activity>
<activity
android:name="com.example.luksongbakagame.OptionForm"
android:label="@string/title_activity_option_form" >
</activity>
<activity
android:name="com.example.luksongbakagame.HelpForm"
android:label="@string/title_activity_help_form" >
</activity>
<activity
android:name="com.example.luksongbakagame.About"
android:label="@string/title_activity_about" >
</activity>
<activity
android:name="com.example.luksongbakagame.PracticeMode"
android:label="@string/title_activity_practice_mode" >
</activity>
<activity
android:name="com.example.luksongbakagame.NormalMode"
android:label="@string/title_activity_normal_mode" >
</activity>
<activity
android:name="com.example.luksongbakagame.ChallengeMode"
android:label="@string/title_activity_challenge_mode" >
</activity>
<activity
android:name="com.example.luksongbakagame.LvlForm1"
android:label="@string/title_activity_lvl_form1" >
</activity>
<activity
android:name="com.example.luksongbakagame.LvlForm2"
android:label="@string/title_activity_lvl_form2" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame1"
android:label="@string/title_activity_new_game1" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame2"
android:label="@string/title_activity_new_game2" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame3"
android:label="@string/title_activity_new_game3" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame4"
android:label="@string/title_activity_new_game4" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame5"
android:label="@string/title_activity_new_game5" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame6"
android:label="@string/title_activity_new_game6" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame7"
android:label="@string/title_activity_new_game7" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame8"
android:label="@string/title_activity_new_game8" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame9"
android:label="@string/title_activity_new_game9" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame10"
android:label="@string/title_activity_new_game10" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame"
android:label="@string/title_activity_new_game"
android:screenOrientation="landscape" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame11"
android:label="@string/title_activity_new_game11" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame12"
android:label="@string/title_activity_new_game12" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame13"
android:label="@string/title_activity_new_game13" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame14"
android:label="@string/title_activity_new_game14" >
</activity>
<activity
android:name="com.example.luksongbakagame.HighScore"
android:label="@string/title_activity_high_score" >
</activity>
</application>
</manifest>
启动器 activity 在你的 manifest.xml 中这样声明。 (启动器活动是应用程序的启动 activity)
<activity
android:name="packagename.LauncherActivityName"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
其他类这样声明
<activity
android:name="packagename.OtherActivityName"
android:label="@string/app_name" >
</activity>
字符串声明
<string name="urstringname">Your String</string>
例如
<string name="myname">Ann Lee</string>
在这里您可以通过字符串名称 myname 访问字符串 "Ann Lee"。
我的游戏有问题。单击新游戏按钮 (btnnewgame2) 后它停止了。单击它后,它应该会进入我的主要游戏 LunetaPark1。如果我的代码太长(实际上是整个代码),我很抱歉。请看一看。我不知道是什么问题。一旦我找到了这个问题的答案,我的另一个 activity 将 运行 smoothly.Please 帮助我。
NewGame1
public class NewGame1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_new_game1);
}
public void action(View v){
int id = v.getId();
if (id==R.id.btnnewgame2){
Intent intent= new Intent(this,LunetaPark1.class);
startActivity(intent);
}
if (id==R.id.btnhighscore1){
Intent intent= new Intent(this,HighScore.class);
startActivity(intent);
}
if (id==R.id.btnexit3){
Intent intent= new Intent(this,MainActivity.class);
startActivity(intent);
}
if (id==R.id.btnback7){
Intent intent= new Intent(this,LvlForm1.class);
startActivity(intent);
}
}
}
LunetaPark1
public class LunetaPark1 extends Activity {
static MediaPlayer mp1;
MediaPlayer jump;
MediaPlayer takecoin;
GameLoopThread3 gameLoopThread;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// for no title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameView(this));
}
public class GameView extends SurfaceView {
Bitmap bmp;
Bitmap background, taya, note1;
Bitmap run1;
Bitmap run6;
Bitmap jump2;
Bitmap coin;
Bitmap exit;
// MediaPlayer mp1,jump,takecoin;
private SurfaceHolder holder;
// private gameloop gameLoopThread;
private int x = 0, y = 0, z = 0, delay = 0, getx, gety, sound = 1;
int show = 0, sx, sy = 0;
int cspeed = 0, kspeed = 0, gameover = 0;
int score = 0, health = 120, reset = 0;
private int min = 1, sec = 0;
private Handler mHandler = new Handler();
private Runnable mRunnable;
private boolean isStop = false;
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public GameView(Context context) {
super(context);
mHandler.postDelayed(mRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(sec <= 0){
if(min > 0){
sec = 59;
min--;
isStop = false;
}
else{
//stop timer here
Log.e("TIMER", "timer stop!");
mHandler.removeCallbacks(this);
isStop = true;
}
}
else{
sec--;
isStop = false;
}
Log.i("TIMER", "min: " + min + " sec: " + sec);
if(!isStop){
mHandler.postDelayed(this, 1000);
}
}
}, 1000);
gameLoopThread = new GameLoopThread3(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@SuppressWarnings("deprecation")
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// for stoping the game
gameLoopThread.setRunning(false);
gameLoopThread.getThreadGroup().interrupt();
}
@SuppressLint("WrongCall")
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
// getting the screen size
Display display = getWindowManager().getDefaultDisplay();
sx = display.getWidth();
sy = display.getHeight();
;
cspeed = x / 4;
kspeed = x / 4;
background = BitmapFactory.decodeResource(getResources(),
R.drawable.imgluneta);
run1 = BitmapFactory
.decodeResource(getResources(), R.drawable.run1);
run6 = BitmapFactory
.decodeResource(getResources(), R.drawable.run6);
jump2 = BitmapFactory.decodeResource(getResources(),
R.drawable.run11);
coin = BitmapFactory
.decodeResource(getResources(), R.drawable.coin);
exit = BitmapFactory
.decodeResource(getResources(), R.drawable.exit);
taya = BitmapFactory
.decodeResource(getResources(), R.drawable.taya);
note1 = BitmapFactory.decodeResource(getResources(),
R.drawable.note1);
exit = Bitmap.createScaledBitmap(exit, 25, 25, true);
background = Bitmap
.createScaledBitmap(background, 2 * sx, sy, true);
// health dec
note1 = Bitmap.createScaledBitmap(note1, sx, sy, true);
mp1 = MediaPlayer.create(LunetaPark1.this, R.raw.game);
jump = MediaPlayer.create(LunetaPark1.this, R.raw.jump);
takecoin = MediaPlayer.create(LunetaPark1.this, R.raw.cointake);
}
// on touch method
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
show = 1;
getx = (int) event.getX();
gety = (int) event.getY();
// exit
if (getx < 25 && gety < 25) {
System.exit(0);
}
// sound off
if (getx > 25 && getx < 60) {
if (gety < 25) {
sound = 0;
mp1.stop();
}
}
// sound on
if (getx > 61 && getx < 90) {
if (gety < 25) {
sound = 1;
}
}
// restart game
if (getx > 91 && gety < 25) {
if (health <= 0) {
gameLoopThread.setPause(0);
health = 100;
score = 0;
}
}
}
return true;
}
@SuppressLint("WrongCall")
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
// background moving
z = z - 10;
if (z == -sx) {
z = 0;
canvas.drawBitmap(background, z, 0, null);
} else {
canvas.drawBitmap(background, z, 0, null);
}
// running player
x += 5;
if (x == 20) {
x = 5;
}
if (show == 0) {
if (x % 2 == 0) {
int height = run1.getHeight();
canvas.drawBitmap(run1, sx / 16, (15 * sy / 18) - height / 2, null);
// kinfe hit
if (kspeed == 20) {
kspeed = sx;
health -= 25;
canvas.drawBitmap(note1, 0, 0, null);
}
} else {
int height = run6.getHeight();
canvas.drawBitmap(run6, sx / 16, (15 * sy / 18) - height / 2, null);
// kinfe hit
if (kspeed == 20) {
kspeed = sx / 2;
health -= 25;
canvas.drawBitmap(note1, 0, 0, null);
}
}
}
// for jump
if (show == 1) {
if (sound == 1) {
jump.start();
}
int height=jump2.getHeight();
canvas.drawBitmap(jump2, sx / 16, (3 * sy / 4)-height, null);
// score
if (cspeed <= (sx / 8)-height/2 && cspeed >= (sx / 16)-height/2) {
if (sound == 1) {
takecoin.start();
}
cspeed = sx / 2;
score += 10;
}
// jump-hold
delay += 1;
if (delay == 3) {
show = 0;
delay = 0;
}
}
// for coins
cspeed = cspeed - 5;
if (cspeed == -sx / 2) {
cspeed = sx / 2;
canvas.drawBitmap(coin, cspeed, 3 * sy / 4, null);
} else {
canvas.drawBitmap(coin, cspeed, 3 * sy / 4, null);
}
// kinfe
kspeed = kspeed - 20;
int height = taya.getHeight();
canvas.drawBitmap(taya, kspeed, (15 * sy / 18) - height / 2, null);
if (kspeed < 0) {
kspeed = sx;
health -= 25;
}
// score
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setTextSize(15);
paint.setTextAlign(Align.LEFT);
canvas.drawText("Score :" + score, 3 * sx / 4, 20, paint);
// exit
canvas.drawBitmap(exit, 0, 0, null);
if (sound == 1) {
mp1.start();
mp1.setLooping(true);
}
// health
Paint myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStrokeWidth(10);
canvas.drawText("Health :" + health, 0, (sy / 8) - 5, myPaint);
canvas.drawRect(0, sy / 8, health, sy / 8 + 10, myPaint);
// game over
if (health <= 0) {
gameover = 1;
mp1.stop();
canvas.drawText("GAMEOVER OVER", sx / 2, sy / 2, myPaint);
canvas.drawText("YOUR SCORE : " + score, sx / 2, sy / 4,
myPaint);
canvas.drawText("Restart", 91, 25, myPaint);
gameLoopThread.setPause(1);
}
// restart
if (reset == 1) {
gameLoopThread.setPause(0);
health = 100;
score = 0;
}
// timer
Paint mypaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStrokeWidth(10);
mypaint.setTextSize(50);
canvas.drawText("Timer :" + min + sec,min, sec, mypaint);
}
}
@Override
public void onResume() {
super.onResume();
LunetaPark1.mp1.isPlaying();
}
@Override
public void onPause() {
super.onPause();
LunetaPark1.mp1.pause();
}
}
GameLoopThread3
public class GameLoopThread3 extends Thread
{
private GameView view;
static final long FPS = 10;
private boolean running = false;
boolean isPaused;
public GameLoopThread3(GameView view)
{
this.view = view;
}
public void setRunning(boolean run)
{
running = run;
}
public void setPause(int i)
{
synchronized (view.getHolder())
{
if(i==0)
{
isPaused=false;
}
if(i==1)
{
isPaused = true;
}
}
}
@SuppressLint("WrongCall")
@Override
public void run()
{
long ticksPS = 100;
long startTime = 0;
long sleepTime;
while (running)
{
//pause and resume
if (isPaused)
{
try
{
this.sleep(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
Canvas c = null;
startTime = System.currentTimeMillis();
try
{
c = view.getHolder().lockCanvas();
synchronized (view.getHolder())
{
view.onDraw(c);
}
}
finally
{
if (c != null)
{
view.getHolder().unlockCanvasAndPost(c);
}
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try
{
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
}
catch (Exception e) {}
}
}
}
Logcat
01-24 20:20:14.408: E/AndroidRuntime(1242): FATAL EXCEPTION: main
01-24 20:20:14.408: E/AndroidRuntime(1242): java.lang.IllegalStateException: Could not execute method of the activity
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View.onClick(View.java:3599)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View.performClick(View.java:4204)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View$PerformClick.run(View.java:17355)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.os.Handler.handleCallback(Handler.java:725)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.os.Looper.loop(Looper.java:137)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 20:20:14.408: E/AndroidRuntime(1242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-24 20:20:14.408: E/AndroidRuntime(1242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-24 20:20:14.408: E/AndroidRuntime(1242): at dalvik.system.NativeStart.main(Native Method)
01-24 20:20:14.408: E/AndroidRuntime(1242): Caused by: java.lang.reflect.InvocationTargetException
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 20:20:14.408: E/AndroidRuntime(1242): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.view.View.onClick(View.java:3594)
01-24 20:20:14.408: E/AndroidRuntime(1242): ... 11 more
01-24 20:20:14.408: E/AndroidRuntime(1242): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.luksongbakagame/com.example.luksongbakagame.LunetaPark1}; have you declared this activity in your AndroidManifest.xml?
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivityForResult(Activity.java:3370)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivityForResult(Activity.java:3331)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivity(Activity.java:3566)
01-24 20:20:14.408: E/AndroidRuntime(1242): at android.app.Activity.startActivity(Activity.java:3534)
01-24 20:20:14.408: E/AndroidRuntime(1242): at com.example.luksongbakagame.NewGame1.action(NewGame1.java:30)
01-24 20:20:14.408: E/AndroidRuntime(1242): ... 14 more
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luksongbakagame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.luksongbakagame.Splash"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.luksongbakagame.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.luksongbakagame.StartForm"
android:label="@string/title_activity_start_form" >
</activity>
<activity
android:name="com.example.luksongbakagame.OptionForm"
android:label="@string/title_activity_option_form" >
</activity>
<activity
android:name="com.example.luksongbakagame.HelpForm"
android:label="@string/title_activity_help_form" >
</activity>
<activity
android:name="com.example.luksongbakagame.About"
android:label="@string/title_activity_about" >
</activity>
<activity
android:name="com.example.luksongbakagame.PracticeMode"
android:label="@string/title_activity_practice_mode" >
</activity>
<activity
android:name="com.example.luksongbakagame.NormalMode"
android:label="@string/title_activity_normal_mode" >
</activity>
<activity
android:name="com.example.luksongbakagame.ChallengeMode"
android:label="@string/title_activity_challenge_mode" >
</activity>
<activity
android:name="com.example.luksongbakagame.LvlForm1"
android:label="@string/title_activity_lvl_form1" >
</activity>
<activity
android:name="com.example.luksongbakagame.LvlForm2"
android:label="@string/title_activity_lvl_form2" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame1"
android:label="@string/title_activity_new_game1" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame2"
android:label="@string/title_activity_new_game2" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame3"
android:label="@string/title_activity_new_game3" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame4"
android:label="@string/title_activity_new_game4" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame5"
android:label="@string/title_activity_new_game5" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame6"
android:label="@string/title_activity_new_game6" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame7"
android:label="@string/title_activity_new_game7" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame8"
android:label="@string/title_activity_new_game8" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame9"
android:label="@string/title_activity_new_game9" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame10"
android:label="@string/title_activity_new_game10" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame"
android:label="@string/title_activity_new_game"
android:screenOrientation="landscape" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame11"
android:label="@string/title_activity_new_game11" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame12"
android:label="@string/title_activity_new_game12" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame13"
android:label="@string/title_activity_new_game13" >
</activity>
<activity
android:name="com.example.luksongbakagame.NewGame14"
android:label="@string/title_activity_new_game14" >
</activity>
<activity
android:name="com.example.luksongbakagame.HighScore"
android:label="@string/title_activity_high_score" >
</activity>
</application>
</manifest>
启动器 activity 在你的 manifest.xml 中这样声明。 (启动器活动是应用程序的启动 activity)
<activity
android:name="packagename.LauncherActivityName"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
其他类这样声明
<activity
android:name="packagename.OtherActivityName"
android:label="@string/app_name" >
</activity>
字符串声明
<string name="urstringname">Your String</string>
例如
<string name="myname">Ann Lee</string>
在这里您可以通过字符串名称 myname 访问字符串 "Ann Lee"。