This project is read-only.

Creating CCParticleSmoke. (Invalid cross-thread access.)

Topics: Windows Phone 8
Mar 9, 2014 at 8:03 PM
I'm working on learning Cocos2d-XNA and using it cross platform. Using the default template I replaced the label with CCParticleSmoke. The layer constructor looks like this:
        public IntroLayer()
        {

            // create and initialize a Label
            var label = new CCParticleSmoke();

            // position the label on the center of the screen
            label.Position = CCDirector.SharedDirector.WinSize.Center;

            // add the label as a child to this Layer
            AddChild(label);

            // setup our color for the background
            Color = new CCColor3B(Microsoft.Xna.Framework.Color.Blue);
            Opacity = 255;

        }
When I go to run the app in I get a exception. System.UnauthorizedAccessException: Invalid cross-thread access.
I don't get this problem with Win32 or Windows 8. Is there something specific I should be doing to avoid this?

Thanks,
Johnny
Mar 11, 2014 at 1:01 AM
Are you using the source tree or the NuGet package?
Mar 11, 2014 at 1:09 AM
This error was with the nuget package. I got the source and compiled it. For some reason DecodeJpeg is returning a null bitmap. I'm going to reinstall Visual Studio and the SDK. The test projects seem to work but I can't get them to work in my own for some reason.
Mar 11, 2014 at 3:07 AM
I think the threads are deadlock. The UI thread is waiting for the Scene to initialize and the Scene tries to load the Particle Texture on the UI thread and hit the max wait and crashed with a null value. Adding a 2 seconds sleep to the Scene init fixed the issue. I guess it wouldn't be as much of a problem with a proper menu. The smoke texture still has white boxes on it for some reason but at least I can continue.
Mar 11, 2014 at 11:03 AM
A better fix was to change
        public GamePage()
        {
            InitializeComponent();

            _game = XamlGame<Game1>.Create("", this);

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
to
        public GamePage()
        {
            InitializeComponent();
            Loaded += GamePage_Loaded;

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        void GamePage_Loaded(object sender, RoutedEventArgs e)
        {
            _game = XamlGame<Game1>.Create("", this);
        }
Mar 11, 2014 at 6:49 PM
Interesting. And that seems to work consistently then?

That's awesome! This deadlock-on-start is really frustrating.

Did you try your change with a pause and resume?
Mar 11, 2014 at 6:56 PM
Yeah I've had no problem with it loading anymore. I haven't tried pause/resume.

There is something CCParticleSmoke to have white boxes around the smoke. It doesn't seem to happen to any other Particle effects.
Mar 11, 2014 at 7:19 PM
The white boxes are from scaling. The particle image for the smoke effect is very small, so I think the content scale factor is set too high and it become a big box. I have not had a chance to spend time analyzing it.
Mar 11, 2014 at 7:23 PM
I've taken your fix and integrated it into the wp8 template. I will share up with the MonoGame people too.
May 2, 2014 at 11:10 PM
eclips wrote:
A better fix was to change
        public GamePage()
        {
            InitializeComponent();

            _game = XamlGame<Game1>.Create("", this);

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
to
        public GamePage()
        {
            InitializeComponent();
            Loaded += GamePage_Loaded;

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        void GamePage_Loaded(object sender, RoutedEventArgs e)
        {
            _game = XamlGame<Game1>.Create("", this);
        }
Hi eclips ,

May I know how'd you use xaml with cocos2D-Xna . Can I know which Version of Cocos2D you are using to build on Windows Phone.
It would be really helpful.

Regards,
May 2, 2014 at 11:10 PM
eclips wrote:
A better fix was to change
        public GamePage()
        {
            InitializeComponent();

            _game = XamlGame<Game1>.Create("", this);

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
to
        public GamePage()
        {
            InitializeComponent();
            Loaded += GamePage_Loaded;

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        void GamePage_Loaded(object sender, RoutedEventArgs e)
        {
            _game = XamlGame<Game1>.Create("", this);
        }
Hi eclips ,

May I know how'd you use xaml with cocos2D-Xna . Can I know which Version of Cocos2D you are using to build on Windows Phone.
It would be really helpful.

Regards,