2013-07-31 89 views
3

我正處於Cocos2d - Android遊戲的最後階段,我遇到了一個我無法弄清的問題。Cocos2d Android鎖屏或離開遊戲部隊重啓

當我鎖定我的平板電腦(Nexus 7)或當我點擊home按鈕離開遊戲時,它會重新啓動。它重新回到初始屏幕,就像遊戲重新開放一樣。供您參考,這是我的主要活動和我的Android清單:

public class MainActivity extends Activity { 

//To get a static reference to context for shared preferences 
private static Context context; 

protected CCGLSurfaceView _glSurfaceView; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    //To get a static reference to context for shared preferences 
    MainActivity.context = getApplicationContext(); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    _glSurfaceView = new CCGLSurfaceView(this); 

    setContentView(_glSurfaceView); 
} 

//To get a static reference to context for shared preferences 
public static Context getAppContext(){ 
    return MainActivity.context; 
} 

@Override 
public void onStart() 
{ 
    super.onStart(); 

    CCDirector.sharedDirector().attachInView(_glSurfaceView); 

    CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait); 

    CCDirector.sharedDirector().setDisplayFPS(true); 

    CCDirector.sharedDirector().setAnimationInterval(1.0f/60.0f); 

    CCScene scene = Splash.scene(); 

    CCDirector.sharedDirector().runWithScene(scene); 
} 

@Override 
public void onPause() 
{ 
    super.onPause(); 

    CCDirector.sharedDirector().pause(); 
    SoundEngine.sharedEngine().pauseSound(); 
} 

@Override 
public void onResume() 
{ 
    super.onResume(); 

    CCDirector.sharedDirector().resume(); 
    SoundEngine.sharedEngine().resumeSound(); 
} 

@Override 
public void onStop() 
{ 
    super.onStop(); 

    SoundEngine.sharedEngine().pauseSound(); 

} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="bigfiresoftware.alphadefender" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="16" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/adicon" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name="bigfiresoftware.alphadefender.MainActivity" 
     android:screenOrientation="portrait" 
     android:label="@string/app_name" 
     android:hardwareAccelerated="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity>  
</application> 
android:configChanges="keyboard|keyboardHidden|orientation" 
</manifest> 

任何/所有的幫助是非常讚賞。謝謝。

+0

call CCDirector.sharedDirector()。resume();在onStop() –

+0

這沒有奏效。 (還是)感謝你的建議。 – Atamous

回答

0

在你的onStop加入這一行()方法

CCDirector.sharedDirector().end(); 

,你需要編寫一個的onDestroy()方法,當你關閉遊戲刪除所有的實例。

3

將您的清單文件中的以下行添加到您的活動中。

 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 

當你鎖定屏幕屏幕的方向changes.So你的活動,並摧毀它再次creates.If您的廣告這一行的方向不會改變。

+0

非常感謝,它解決了我的問題。 – Alanmars

1

上面的答案沒有解決它,但我終於找到了答案。這是一個艱難的。

如果任何人發現這個問題:

在AdnroidManifest.xml取出targetSdkVersion。或者改成其他人,我沒有嘗試過其他人,但刪除工作。

android:minSdkVersion="9" 
android:targetSdkVersion="17" /> //git rid of this guy