应用程序在android studio中启动新活动后没有响应

时间:2017-09-24 19:47:06

标签: java android

游戏从MainActivity类(扩展Activity)开始,然后调用扩展GamePanel的新SurfaceView,并且有许多类可以更新游戏,所以有一个类(GameplayScene)可以检查触摸并检查游戏发生了什么(如果用户赢或输),所以有一种更新方法可以检查玩家输赢情况以及玩家是否获胜或者输掉然后我想开始一个新的活动,它有一个重启游戏的按钮,

问题是:在开始新活动之后(通过使用不扩展任何内容的类中的Intent),应用程序不响应(但它会正确显示新活动)

从()开始新活动的类:

public class GameplayScene implements Scene {   

public GameplayScene() {
....

@Override
public void recieveTouch(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!youWin && !gameOver && player.getRectangle().contains((int) event.getX(), (int) event.getY()))
                movingPlayer = true;
                forBegin = false;
            if (gameOver && System.currentTimeMillis() - gameOverTime >= 2000) {
                reset();
                gameOver = false;
            }
            if (youWin && System.currentTimeMillis() - winTime >= 2000) {
                reset();
                youWin = false;

            }
            if(!youWin && !gameOver && !movingPlayer) {
                if(!forBegin && System.currentTimeMillis() - fireTime >= 200) {
                    touchFire = true;
                    count = 1;
                    fireTime = System.currentTimeMillis();
                }
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (!youWin && !gameOver && movingPlayer)
                playerPoint.set((int) event.getX(), (int) event.getY());
            break;
        case MotionEvent.ACTION_UP:
            movingPlayer = false;
            touchFire = false;
            break;
    }
}


//@Override
public void draw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    //Drawable d = getDrawable(getResources(), R.drawable.bgi);
    //d.setBounds(0, 0,canvas.getWidth(), canvas.getHeight());
    //d.draw(canvas);
    //canvas.drawBitmap(bgi, null, new Rect(0, 0, canvas.getWidth(), canvas.getHeight()),new Paint());

    player.draw(canvas);
    obstacleManager.draw(canvas);

    if(gameOver) {
        Context contX = Constants.CONTEXT;
        Intent intent = new Intent(contX , result.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("toWR",0);
        contX.startActivity(intent);


    }
    if(youWin) {

        Context contX = Constants.CONTEXT;
        Intent intent = new Intent(contX , result.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("toWR",1);
        contX.startActivity(intent);
    }
  }
}

Android清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".main_menu"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"
        android:screenOrientation="portrait" />
    <SurfaceView android:name=".GamePanel"
        android:screenOrientation="portrait" />
    <activity android:name=".result"
        android:screenOrientation="portrait" />
</application>

1 个答案:

答案 0 :(得分:0)

我假设您有某种DrawingThread(...)draw(...)委托给此Scene。如果是这种情况,那么你的目标可能是每秒60次更新......这意味着这个意图将尝试每秒发射60次。这可能是导致您的应用看起来没有响应的原因。

不是从此处启动意图,而是更改游戏状态(或重用gameOver状态)并中断游戏循环(绘图线程)。然后,在游戏循环之外,但在线程结束之前,一次开始活动。