This project is read-only.


Topics: Android
Feb 6, 2014 at 12:02 PM
how can one go from a normal activity to different androidgameactivities?
and how can you go from a scene to a normal activity?

this seems to be much more complicated then cocos2d-iphone?

i managed to go from a scene to an activity with events bubbling up to my androidgameactivity and from there on i start my startactivity(typeof(normalAcitivity));

but when i go back to this androidgameactivity i got a black screen.

I assume i open differents cocos2d games everytime i open an androidgameactivity?

need help in this...
Feb 6, 2014 at 6:52 PM
You can jump from one activity to another on android by using the activity launcher. The android activities are governed by your launch activity. These activities act like a big stack of applications that run in unison. When a cocos2dxna game, or even a MonoGame, activity is sent to the background, it is deactivated. When it is re-activated you will need to resurrect your claimed assets. Backgrounded activities will release their assets, like textures and such, so that other activities can use the GPU hardware.

The cocos2dxna framework attempts to resurrect as much as it can, but there are times when it can not resurrect all of your texture and label assets. You need to make sure that you create them when the activity regains the focus or at least check if the asset has been disposed and recreate it.
Feb 7, 2014 at 8:56 AM
Edited Feb 7, 2014 at 1:40 PM
and what is the preferred way to reload sprites with textures which are downloaded and stored in folder:
this is the reason i believe i got black screens and some exception:
AmbiguousMatchException: Ambiguous matching in method resolution

i need to do some reloading in??:

public override void ApplicationWillEnterForeground()
            //  CCSimpleAudioEngine.SharedEngine.PauseBackgroundMusic = false;
Guidance would be much appreciated since all my content is from these folder and subfolders. This seems to be happening in android and ios...

This topic describes the same:

edit: to go from a scene to an activity is done by bubbling up events to eventually the android game activity or is there another way to change activity when inside a layer?
Feb 7, 2014 at 4:41 PM
Regarding switching activities from a scene, I keep a static handle on the host activity and use that to do android specific things. This is how I implemented interstitial ads in one of our games. We don't do any platform-specific runtime things in the framework as that would be very messy. cocos2d-x does it because they have a support activity that is used to host the cocos2d-x game. We don't need that in our world, so we leave the custodial android work up to you, the developer. Any suggestions on how we could improve that integration is appreciated.

Where is this ambiguous method call occurring? We made some changes recently to the master on GitHub, so maybe this ambiguous method match is fixed. Are you running from the nuget package?

We probably need to push another revision of our nuget package.
Feb 7, 2014 at 4:42 PM
Feb 10, 2014 at 1:11 PM
Thank you for the reply and the explanations you've done so far.

I've downloaded a new revision of github and used the build dlls in my app.
i was actually running the nuget package in visual studio 2013.

the ambiguous call seems to be gone. this is ok.

however when i push my activity in background and restore this activity my sprites from external files:
data/data/myapp/files become all white.
even if i switch activities. the first time everything is ok, second time every external loaded image is white.

any suggestions on what might be wrong?
Feb 10, 2014 at 4:58 PM
The same white-image can happen on any of the platforms, including Microsoft's XNA (just let your computer's screen saver trigger). This happens because the texture is disposed but the framework doesn't know that it was disposed. There may be a bug deep in the CCTexture2D and CCContentManager code, not sure yet. Android can be very persnickety on resource challenged devices, especially older 2.3 devices.

Can you post a sample of the code that you are using to read the texture from a custom location? This will help me understand if you are using the file-based texture initialization or the memory buffer texture initialization.

Feb 11, 2014 at 7:41 AM
Included a link to a simple testproject: TEXT
And here is a sample of the code i`m referring to:
public override void OnEnter()

            // create and initialize a Label
            var label = new CCLabelTTF("Hello Cocos2D-XNA", "MarkerFelt", 22);

            // position the label on the center of the screen
            label.AnchorPoint = new CCPoint(0, 0);
            label.Position = new CCPoint(0, 0);

            // add the label as a child to this Layer

            string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "downloaded.png");

            var url = new Uri("");

            using (WebClient webClient = new WebClient())
                webClient.UseDefaultCredentials = true;
                webClient.DownloadFile(url, path);

            Texture2D texture;
            CCTexture2D ccTexture;
            using (FileStream fs = new FileStream(path, FileMode.Open))
                texture = Texture2D.FromStream(CCApplication.SharedApplication.GraphicsDevice, fs);
                ccTexture = new CCTexture2D();
            var sprite = new CCSprite(ccTexture);
            sprite.AnchorPoint = new CCPoint(0.5f, 0.5f);
            sprite.Position = CCDirector.SharedDirector.WinSize.Center;
            Color = new CCColor3B(Microsoft.Xna.Framework.Color.Blue);
            Opacity = 255;
if you open this the first time eveything will be ok. if you go to a different view and come back the picture is gone and removed with a white rectangle.

thank you so far for your help!
Feb 11, 2014 at 4:20 PM
Yeah, I thought so. This will never automatically reload itself. You are loading from a custom stream, so the texture cache has no way of knowing where to get this file. You will need to reload this texture on your own when the application resumes. The framework will never be able to do this for you automatically.
Feb 11, 2014 at 8:03 PM
So now the million dollar question, how should i do this?

i'm sorry but i don't seem to see the clue.
Feb 11, 2014 at 9:06 PM
CCApplication :: ApplicationWillEnterForeground()

Game :: LoadContent()

These methods are called when the game resumes from the background. That's when you create the content that was lost. You will need to keep a manifest of those resources you need to maintain though, otherwise you will need to re-download those assets, and that's not cool.
Feb 12, 2014 at 9:18 AM
Can you povide me a little sample project on what i should do?
everything i do in those methods is not working.
i understand what i should do but not how i should do this.
        public CCTexture2D ccTexture;
        string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "downloaded.png");
        public CCSprite sprite;

        /// <summary>
        /// The function be called when the application enter foreground  
        /// </summary>
        public override void ApplicationWillEnterForeground()
            Texture2D texture;
            if (ccTexture != null)
                ccTexture = null;
                using (FileStream fs = new FileStream(path, FileMode.Open))
                    texture = Texture2D.FromStream(CCApplication.SharedApplication.GraphicsDevice, fs);
                    ccTexture = new CCTexture2D();
                sprite = new CCSprite(ccTexture);

now i should be doing something in my layer too to reload this sprite? this sprite was given in the layer as addchild in onEnter method.

a little sample would clarify a lot i believe so we can stop this thread and i can put all the pieces together to make this thing working. :-D
Feb 12, 2014 at 11:48 PM
What if you just do this in lieu of your code to open the stream and create your own texture from the stream?

CCTexture2D ccTexture = CCTextureCache.SharedTextureCache().AddImage(path);
Texture2D texture = ccTexture.XNATexture; // if you need the XNA texture object

If you do that, then the texture cache knows about the path of the texture and it can recreate it when the texture is unloaded from memory.
Feb 13, 2014 at 10:44 AM
absolute paths aren't working. so path= "Data/data/appname/files/someting.png" in
CCTexture2D ccTexture = CCTextureCache.SharedTextureCache().AddImage(path); will not work.

anyway someone in my team changed this so you can use absolute paths and finally it's working.

is there a reason why you can't load external files? android, ios related?
in cocos2d-x you can load external images with absolute paths.
Feb 13, 2014 at 4:48 PM
cocos2d-x doesn't work on an xbox, we do. The security around more pervasive hardware like game consoles is much tighter and so you can't do arbitrary paths. You should download to the title container location and then re-load from that location. Otherwise, you risk leaving residual dirt on the device when your game is removed. Furthermore, any windows 8+ device will not allow you to use arbitrary locations. There, the security is even tighter and the user must authorize your writing to a non-standard location.

paths like "Data/data/appname/files/someting.png" are not absolute, they are relative. If the path is rooted, then it must fall within an allowable root file system, which is typically the title container location.

remember this is cocos2d-xna. The "XNA" has restrictions and tremendous benefits.