gpt4 book ai didi

Android 应用程序在第二次启动时卡住

转载 作者:行者123 更新时间:2023-11-29 22:09:05 26 4
gpt4 key购买 nike

我正在为 Android 开发游戏,我一直在尝试实现一个对话框来显示网络横幅广告的 WebView 。 (我正在使用 cocos2d 进行开发)。我使用静态方法 ads() 静态打开对话框。然而,自从这样做以来,我开始遇到启动问题。

首次启动时,它运行良好,但当应用关闭并重新打开时,“openscreen”场景开始但随后无法播放淡入淡出或继续进入菜单场景。

Activity (ads() 方法所在的位置)

package wingdev.defence;

import java.util.Timer;
import java.util.TimerTask;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;

public class DefenceActivity extends Activity{
protected CCGLSurfaceView _glSurfaceView;
public static int highscore = 0;
public static int mode= Activity.MODE_PRIVATE;
public static SharedPreferences mySharedPreferences ;
public static String CurrentLevel = "";
public static boolean firstlaunch=true;
public static int screenforchoice = 0;
public static boolean choicemade=false;
public static boolean choice=false;
public static DefenceActivity context;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
CCScene scene = OpenScreen.scene();
CCDirector.sharedDirector().runWithScene(scene);
}

@Override

public void onStart()

{
super.onStart();
CCDirector.sharedDirector().attachInView(_glSurfaceView);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);

CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
mySharedPreferences=getSharedPreferences("HighScore",mode);
highscore= mySharedPreferences.getInt("HighScore",0);
SharedPreferences.Editor editor= mySharedPreferences.edit();
editor.remove("HighScore");
editor.putInt("HighScore", highscore);
editor.commit();
CCScene scene = OpenScreen.scene();
CCDirector.sharedDirector().runWithScene(scene);
Config.context = this;
context = this;
}
public void startgame()
{
CCDirector.sharedDirector().attachInView(_glSurfaceView);

CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);;

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

CCScene scene = GameLayerSurvival.scene();
CCDirector.sharedDirector().runWithScene(scene);

}
@Override
public void onPause()
{
super.onPause();
CCDirector.sharedDirector().pause();
}

@Override
public void onResume()
{
super.onResume();
}
@Override
public void onStop()
{
super.onStop();
finish();

CCDirector.sharedDirector().end();
}
static public void newhighscore()
{if(GameLayerSurvival.score>highscore){
highscore=GameLayerSurvival.score;
}
SharedPreferences.Editor editor= mySharedPreferences.edit();
editor.remove("HighScore");
editor.putInt("HighScore", highscore);
editor.commit();
}

public static void ads(){
context.runOnUiThread(new Runnable() {
public void run() {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.main);
dialog.setTitle("Advert");
dialog.setCancelable(true);
WebView mWebView = (WebView) dialog.findViewById(R.id.webbanner);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.example.co.uk/");
Button bc_btn1 = (Button) dialog.findViewById(R.id.button1);
bc_btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
killDialog(dialog);
}
});
dialog.show();
}
});
}
public static void killDialog(Dialog dialog){
dialog.cancel();
}
}

Openscreen- 启动时使用的第一个场景

package wingdev.defence;

import org.cocos2d.actions.instant.CCCallFunc;
import org.cocos2d.actions.interval.CCDelayTime;
import org.cocos2d.actions.interval.CCFadeIn;
import org.cocos2d.actions.interval.CCFadeOut;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor4B;

public class OpenScreen extends CCColorLayer {

CCLabel _start;
CCLabel _easy;
CCLabel _normal;
CCLabel _highscore;
CCLabel _howto;
CCLabel _hard;
CCLabel _reset;
CCSprite wingdev = CCSprite.sprite("wingdevpresents.png");
CCSprite darkorange = CCSprite.sprite("PirateTea.png");
CCSprite plane = CCSprite.sprite("plane.png");
CCSprite back2 = CCSprite.sprite("black.png");
public static CCScene scene()
{
//
CCScene scene = CCScene.node();
CCColorLayer layer = new OpenScreen(ccColor4B.ccc4(255, 255, 255, 255));
scene.addChild(layer);

return scene;
}
protected OpenScreen(ccColor4B color) {
super(color);
CGSize winSize = CCDirector.sharedDirector().displaySize();
this.setIsTouchEnabled(true);
back2.setPosition(winSize.width / 2.0f, winSize.height / 2.0f);
back2.setScaleX(winSize.width / back2.getContentSize().width);
back2.setScaleY(winSize.height / back2.getContentSize().height);
addChild(back2);
wingdev.setPosition(winSize.width / 2.0f, winSize.height / 2.0f);
wingdev.setScaleX(winSize.width / wingdev.getContentSize().width);
wingdev.setScaleY(winSize.height / wingdev.getContentSize().height);
addChild(wingdev);
wingdev.runAction(CCSequence.actions(CCFadeIn.action(1.0f),CCDelayTime.action(2.0f),CCFadeOut.action(1.0f), CCCallFunc.action(this,"openscreen2")));
}
public void openscreen()
{
CCDirector.sharedDirector().replaceScene(Menu.scene());
}
public void openscreen2(){
CGSize winSize = CCDirector.sharedDirector().displaySize();
removeChild(wingdev,true);
darkorange.setPosition(winSize.width / 2.0f, winSize.height / 2.0f);
darkorange.setScaleX(winSize.width / darkorange.getContentSize().width);
darkorange.setScaleY(winSize.height / darkorange.getContentSize().height);
addChild(darkorange);
darkorange.runAction(CCSequence.actions(CCFadeIn.action(1.0f),CCDelayTime.action(2.0f),CCFadeOut.action(1.0f),CCCallFunc.action(this,"openscreen")));
}
}

如果能帮助我理解哪里出了问题,我们将不胜感激!

最佳答案

你暂停你的 CCDirector 在

 @Override
public void onPause()
{
super.onPause();
CCDirector.sharedDirector().pause();
}

我认为你需要在 onResume() 中重新启动它

 @Override
public void onResume()
{
super.onResume();
CCDirector.sharedDirector().resume();
}

如果应用程序未被终止,则不会调用 onStart()。

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

关于Android 应用程序在第二次启动时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10087941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com