How to add background image in CocosSharp?

I have used this code to add background image in CCLayer but it's not working:

public class GameLayer : CCLayerColor
{
    private CCSprite background; 
    public GameLayer() : base(CCColor4B.Transparent)
    {       
        background = new CCSprite("cbg")
        {
            AnchorPoint = new CCPoint(0, 0),
            IsAntialiased = true,
            Position = new CCPoint(0, 0),

        };
        this.AddChild(background);
    }
}

My problem is image is displaying but not of screen size as background image, its just displaying at the bottom-left corner of the screen.

Best Answer

Answers

  • M_Shah04M_Shah04 USMember ✭✭✭
    edited August 2018

    @laetitia said:
    What if you specify "ContentSize" to be the size of your screen?

    I tried to use ContentSize = new CCSize(VisibleBoundsWorldspace.MaxX, VisibleBoundsWorldspace.MaxY) but not working!
    Can you please specify if there is some another way.
    Thanks.

  • LaetitiaLaetitia Member ✭✭

    In my project I use cocosSharp integrated with xamarin.forms, and I can access the screen size inside my scene, maybe it's more accurate than VisibleBoundsWorldspace?

    public class MyScene : CCScene
    {
    public MyScene (CCGameView gameView) : base(gameView)
    {

            var screenSize = gameView.DesignResolution;
          //Then use screen size for your background
       }
    

    }

  • M_Shah04M_Shah04 USMember ✭✭✭

    @laetitia said:
    In my project I use cocosSharp integrated with xamarin.forms, and I can access the screen size inside my scene, maybe it's more accurate than VisibleBoundsWorldspace?

    public class MyScene : CCScene
    {
    public MyScene (CCGameView gameView) : base(gameView)
    {

            var screenSize = gameView.DesignResolution;
          //Then use screen size for your background
       }
    

    }

    I have used this sample application: https://developer.xamarin.com/samples/mobile/BouncingGame/
    Please take a look at that and let me know the steps I can follow for the same, As I'm not able to implement your suggested steps.

  • M_Shah04M_Shah04 USMember ✭✭✭

    @laetitia said:
    What if you specify "ContentSize" to be the size of your screen?

    Thanks for the hint, after some try I got this working.

Sign In or Register to comment.