简体   繁体   English

如果我退出活动后打开cocos 2d项目,则android屏幕会变黑

[英]android screen goes black if I open cocos 2d project after getting out of an activity

The complete code is here my question is: 完整的代码在这里,我的问题是:

  1. I run the code it runs as expected 我运行它按预期运行的代码
  2. I press the back button 我按下后退按钮
  3. I reopen the app from mobile 我从手机重新打开应用程序
  4. The screen goes black 屏幕变黑

I have tested this code on Android v2.3 (Gingerbread) OS 我已经在Android v2.3(Gingerbread)操作系统上测试了此代码

Is this problem because of cocos 2d library please let me know thanks in advance... 是因为cocos 2d库导致此问题,请提前告知我...

    import org.cocos2d.layers.CCColorLayer;
    import org.cocos2d.layers.CCLayer;
    import org.cocos2d.layers.CCScene;
    import org.cocos2d.nodes.CCDirector;
    import org.cocos2d.nodes.CCSprite;
    import org.cocos2d.opengl.CCGLSurfaceView;
    import org.cocos2d.types.CGPoint;
    import org.cocos2d.types.CGSize;
    import org.cocos2d.types.ccColor4B;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Window;
    import android.view.WindowManager;

    public class GameActivity extends Activity {
        private CCGLSurfaceView mGLSurfaceView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

    //        Log.d("hii", "m here on create");

            // set the window status, no tile, full screen and don't sleep
            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);

            mGLSurfaceView = new CCGLSurfaceView(this);

            setContentView(mGLSurfaceView);

            // attach the OpenGL view to a window
            CCDirector.sharedDirector().attachInView(mGLSurfaceView);

            // no effect here because device orientation is controlled by manifest
            CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);

            // show FPS
            // set false to disable FPS display, but don't delete fps_images.png!!
            CCDirector.sharedDirector().setDisplayFPS(false);

            // frames per second
            CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);
            CCScene scene = TemplateLayer.scene();

            // Make the Scene active
            CCDirector.sharedDirector().runWithScene(scene);
        }

        @Override
        public void onStart() {
            super.onStart();     
            Log.d("hii", "m here on start");
        }

        @Override
        public void onPause() {
            super.onPause();
            Log.d("hii", "m here on pause");
            CCDirector.sharedDirector().pause();
        }

        @Override
        public void onResume() {
            super.onResume();
            Log.d("hii", "m here on resumae");
            CCDirector.sharedDirector().resume();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d("hii", "m here on destroy");
            CCDirector.sharedDirector().end();
        }

        public static class TemplateLayer extends CCColorLayer
        {


            CGSize winSize = CCDirector.sharedDirector().displaySize();

            //killing sprite
            CCSprite killerchild,boat,pirates;

            public static CCScene scene() 
            {

                CCScene scene = CCScene.node();
                CCLayer layer = new TemplateLayer(ccColor4B.ccc4(0, 225, 145, 146));

                scene.addChild(layer);

                return scene;
            }


               @Override
                public void onEnter() {

                    // then iterate over all the children
                    super.onEnter();

                }

               @Override
                public void onExit() {

                    super.onExit();

                }



            protected TemplateLayer(ccColor4B color) 
            {

                super(color);

                this.setIsTouchEnabled(true);


                killerchild = CCSprite.sprite("logo.png");
                killerchild.setAnchorPoint(CGPoint.ccp(0, 0));

                killerchild.setPosition(CGPoint.ccp(winSize.width/2-killerchild.getContentSize().width/2, winSize.height-killerchild.getContentSize().height));
                addChild(killerchild);

                boat = CCSprite.sprite("boat.png");
                boat.setAnchorPoint(CGPoint.ccp(0, 0));

                boat.setPosition(CGPoint.ccp(winSize.width/2-boat.getContentSize().width/2,-boat.getContentSize().height/4));
                addChild(boat);


                pirates = CCSprite.sprite("Pirates.png");
                pirates.setAnchorPoint(CGPoint.ccp(0, 0));

                pirates.setPosition(CGPoint.ccp(winSize.width/2-pirates.getContentSize().width/2,winSize.height/2-pirates.getContentSize().height/2));
                addChild(pirates,-1);

            }

        }

    }

extends CCColorLayer in TemplateLayer class : 在TemplateLayer类中扩展CCColorLayer:

public static CCScene scene() {
    CCScene scene = CCScene.node();
    CCLayer layer = new TemplateLayer(ccColor4B.ccc4(0, 225, 145, 146));
    scene.addChild(layer);
    return scene;
}

And Change the TemplateLayer() consturctor into : 并将TemplateLayer()构造函数更改为:

TemplateLayer(ccColor4B color){
super(color);
}

Run The project 运行项目

I think this is an issue because of the jar file you are using for cocos 2D. 我认为这是一个问题,因为您正在使用cocos 2D的jar文件。 I have used my jar and I am not getting this issue. 我用完了我的罐子,但没有收到这个问题。 Please download from below link and check. 请从下面的链接下载并检查。

https://www.wetransfer.com/downloads/8d55297ac0f605408682fea530e0419120130920102338/e48fbd145e26dc5968402f3a8915678620130920102338/8f429f https://www.wetransfer.com/downloads/8d55297ac0f605408682fea530e0419120130920102338/e48fbd145e26dc5968402f3a8915678620130920102338/8f429f

Please check with this library and let me know if issue still persist. 请检查此库,让我知道问题是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM