CCMenuItem crashes on Android after async operation

DougLampeDougLampe USMember

I'm having a problem with my app and the following test methods duplicate the issue. After an async call to HttpWebRequest, trying to add a menu item crashes. There is no exception, the app keeps running, but the debugger won't go past the line marked below. I'm guessing it has something to do with the async response continuing on a different thread. I can do normal Cocos stuff after the web request (mostly CCDrawNode) but CCMenuItem is causing issues.

//GameLayer class:
    protected override void AddedToScene()
    {
        base.AddedToScene();
        // create and initialize a Label
        label = new CCLabel("Hello CocosSharp", "sans-serif", 22, CCLabelFormat.SystemFont);
        // add the label as a child to this Layer

        var menuItem = new CCMenuItemLabel(label, async x =>
        {
            WebRequest request = HttpWebRequest.Create("http://www.google.com");

            var response = await request.GetResponseAsync();

            CCScene gameScene = new CCScene(GameView);

            GameLayer2 gameLayer = new GameLayer2();

            gameScene.AddLayer(gameLayer);

            Director.ReplaceScene(gameScene);

        });

        // Use the bounds to layout the positioning of our drawable assets
        var bounds = VisibleBoundsWorldspace;

        menu = new CCMenu(menuItem);

        AddChild(menu);

        // position the label on the center of the screen
        menu.Position = bounds.Center;

        // Register for touch events
        var touchListener = new CCEventListenerTouchAllAtOnce();
        touchListener.OnTouchesEnded = OnTouchesEnded;
        AddEventListener(touchListener, this);
    }

//GameLayer2 class:
    protected override void AddedToScene()
    {
        base.AddedToScene();
        // create and initialize a Label
        label = new CCLabel("Hello CocosSharp2", "sans-serif", 22, CCLabelFormat.SystemFont);
        // add the label as a child to this Layer
        AddChild(label);

        // THIS LINE FAILS!!! No error, just stops responding. Stepping over breakpoint detaches debugger.
        var menuItem = new CCMenuItemLabel(label, x =>
        {
        });

        // Use the bounds to layout the positioning of our drawable assets
        var bounds = VisibleBoundsWorldspace;

        menu = new CCMenu(menuItem);

        AddChild(menu);

        // position the label on the center of the screen
        menu.Position = bounds.Center;


    }

Posts

  • DougLampeDougLampe USMember

    Just to be clear, this works fine if you remove the Google web request.

  • DougLampeDougLampe USMember

    It looks like if I push both scenes in my delegate I can pop the second scene without any problems. I'll use that solution but no idea why I can't swap scenes after an async call.

Sign In or Register to comment.