locked
CCSprite in Android? RRS feed

  • Question

  • User209507 posted

    Hi,

    I'm just loading a background image, and it works fine in the wp8.1 emulator, but when I execute in the android emulators it does not appear, and I don't see any exception. I'm new using CocosSharp, so I guess I'm doing something wrong, but can't figure it out. Any help will be very welcome.

    Here is my code:

        public static void LoadGame(object sender, EventArgs e)
        {
            CCGameView gameView = sender as CCGameView;
    
            if (gameView != null)
            {
                var contentSearchPaths = new List<string>() { "Fonts", "Sounds", "Images" };
                CCSizeI viewSize = gameView.ViewSize;
    
                int width = 720;
                int height = (int)(720 * ((double)Math.Max(viewSize.Width, viewSize.Height) / (double)Math.Min(viewSize.Width, viewSize.Height)));
    
                // Set world dimensions
                Data.Common.ViewSize = new CCSizeI(width, height);
                gameView.DesignResolution = Data.Common.ViewSize;
    
                CCSprite.DefaultTexelToContentSizeRatio = 2.0f;
    
                gameView.ContentManager.SearchPaths = contentSearchPaths;
    
                gameView.RunWithScene(new Scenes.LanguageSelection(gameView));
            }
        }
    
    public class LanguageSelection : CCScene
    {
        CCLayer layerBackgorund;
    
        public LanguageSelection(CCGameView gameView) : base(gameView)
        {
            layerBackgorund = new Layers.Background();
            this.AddLayer(layerBackgorund);
        }
    }
    
    public class Background : CCLayer
    {
        private CCSprite background;
    
        public Background() : base()
        {
            background = new CCSprite("background.png")
            {
                AnchorPoint = new CCPoint(0, 0),
                IsAntialiased = true,
                Position = new CCPoint(0, 0),
                ContentSize = Common.ViewSize
            };
            this.AddChild(background);
        }
    }
    
    Thursday, April 21, 2016 6:51 PM

Answers

  • User209507 posted

    Solved: I needed to change the BuildAction from Content to AndroidAsset. Thanks Victor! I only noticed that after knowing the error :)

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 22, 2016 5:11 PM
  • User83292 posted

    It could be a capitalization issue, it could be that your assets are not using the right build configuration. It's hard to say - but I'd suggest working through the guide I linked above to identify what's different about your case vs. the guide. You should be able to isolate the difference if you can get a working example on Android.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, April 24, 2016 3:19 PM

All replies

  • User83292 posted

    Unfortunately CocosSharp does not (by default) throw exceptions when files are missing, so problems like these can be a pain to solve.

    You may want to turn on logging which will output errors to help you diagnose the problem:

    CCLog.Logger = Console.WriteLine;

    Full code sample can be found here:

    https://github.com/mono/CocosSharp/wiki/Logging

    Without knowing more about your project structure, I'd guess your assets/Content folder does not have the file, or it's in the wrong location, or it's using the wrong build operation. Do you have your file in the assets/Content/Images folder?

    You can follow along this walkthrough to see how to add assets to Android:

    https://developer.xamarin.com/guides/cross-platform/gamedevelopment/cocossharp/firstgame/part2/

    Friday, April 22, 2016 3:59 PM
  • User209507 posted

    Thanks Victor,

    You are right, it says "CocosSharpTests: Texture background.png was not found." Although, I'm using the default Images folder for wp8.1 and Android: WinPhone > Content/Images/background.png Droid > Assets/Content/Images/background.png Maybe I'm missing something in the initialization?

    Friday, April 22, 2016 4:58 PM
  • User209507 posted

    Solved: I needed to change the BuildAction from Content to AndroidAsset. Thanks Victor! I only noticed that after knowing the error :)

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 22, 2016 5:11 PM
  • User83292 posted

    It could be a capitalization issue, it could be that your assets are not using the right build configuration. It's hard to say - but I'd suggest working through the guide I linked above to identify what's different about your case vs. the guide. You should be able to isolate the difference if you can get a working example on Android.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, April 24, 2016 3:19 PM