Forum Libraries, Components, and Plugins

Async/Await crashes iOS

I have been trying unsuccessfully to implement some animation sequences using RunActionAsync as per forums.xamarin.com/discussion/34667/cocossharp-v1-3-2-0-release. When I add the await the app crashes. I have created an 'empty cocossharp' project, added the NuGet package Microsoft.Bcl.Async as per the link above, tried with and without the using system.threading & system.threading.task. This is the sample code I am running.

`

    async void Move()
    {
        CCMoveTo movevar;
        if (Forward)
        {
            movevar = new CCMoveTo (2.0f, firstPoint);
            label.Text = "First";
        }
        else
        {
            movevar = new CCMoveTo (2.0f, secondPoint);
            label.Text = "Second";
        }

        Forward = !Forward;

        await mySprite.RunActionAsync (movevar);

        label.Text = "Finished";


    }

`

In case you want to follow this Rey @ReynaldoGalindo.

Posts

  • ReynaldoGalindoReynaldoGalindo USMember

    Thanks, following

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    Hey guys

    I just tried this myself.

    • Create new iOS game.
    • Added PCL version to packages
    • Added image clouds.png content
    • Modified GameLayer to the following:

      using System;
      using System.Collections.Generic;
      using CocosSharp;

      namespace TestRunAsync
      {
      public class GameLayer : CCLayer
      {
      CCSprite mySprite;
      CCLabel label;
      CCPoint firstPoint, secondPoint;

          public GameLayer()
          {
              // Load and instantate your assets here
      
              mySprite = new CCSprite("cloud.png");
      
              label = new CCLabel(string.Empty, "Arial", 50, CCLabelFormat.SystemFont);
      
              // Make any renderable node objects (e.g. sprites) children of this layer
              AddChild(mySprite);
              AddChild(label);
          }
      
          protected override void AddedToScene()
          {
              base.AddedToScene();
      
              // Use the bounds to layout the positioning of our drawable assets
              CCRect bounds = VisibleBoundsWorldspace;
      
              firstPoint = bounds.Center / 2;
              secondPoint = bounds.Center + firstPoint;
      
              mySprite.Position = firstPoint;
              label.Position = bounds.Center;
      
              // Register for touch events
              var touchListener = new CCEventListenerTouchAllAtOnce();
              touchListener.OnTouchesEnded = OnTouchesEnded;
              AddEventListener(touchListener, this);
          }
      
          void OnTouchesEnded(List<CCTouch> touches, CCEvent touchEvent)
          {
              if (touches.Count > 0)
              {
                  // Perform touch handling here
                  Move();
              }
          }
      
          bool Forward;
          async void Move()
          {
              CCMoveTo movevar;
              if (Forward)
              {
                  movevar = new CCMoveTo (2.0f, firstPoint);
                  label.Text = "First";
              }
              else
              {
                  movevar = new CCMoveTo (2.0f, secondPoint);
                  label.Text = "Second";
              }
      
              Forward = !Forward;
      
              await mySprite.RunActionAsync (movevar);
      
              label.Text = "Finished";
      
      
          }
      }
      

      }

    You may have to create a sample project for us to take a look at. There is also the Game Sir Lance and Hop that is absolutely full of async actions.

    Attached is the project I just created. If you do not want to create a sample please use the one attached to make the changes that will make the project not work.

  • WilliamSheltonWilliamShelton USMember ✭✭

    Sometimes I think Xamarin Studio is just out to get me, LOL...Your GameLayer is virtually identical to mine. When I loaded my project up today it showed a NuGet update for the microsoft.bcl.async package(s). I let NuGet perform that update & now it works. What doesn't make sense to me is I had just created that project the day I posted this & those packages haven't been updated for quite a while. I don't know how it would have gotten the older version of the packages when I first added them to the project, but some how it (or I) did. Anyway, it is working now. Thanks for looking at it.

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    No problem. Glad it is working.

Sign In or Register to comment.