CocosSharp v1.7.0.0-pre1: Change Scene Sample Help Required

Over the last week I have been learning about games and started with v1.7.0.0-pre1.
Can someone show / give an example of how to move through different scenes?
I understood and followed the example on GoneBananas but that was using V 1.6 and not v1.7.0.0-pre1

I assume we change scenes using the gameView object (CCGameView) but not sure how to reference when in a scene.
Or is there still a CCDirector and if so a sample would be great.

Being new to games I found this presentation helpful. Once in a scene I understand everything OK (so far anyway......probably more questions to follow :))

An updated hierarchy model would greatly assist like on GitHub Wiki

Thank you

Tagged:

Best Answers

Answers

  • PaulKellyPaulKelly USMember ✭✭

    Hi Victor and Rami,
    Thanks for your replies.
    I did as you suggested and worked it all out. The biggest difference is that I am starting off with V1.7 with the new CCGameView. I took some time to work out, as I was originally creating my own CCDirector object but could not work out how to "connect" it to the new CCGameView. Turns out the a CCDirector, name Director is created automatically when you create the CCGameView and is avalaible in all scenes that are created using the GameView object in the scene's base class.

    public class StartScene : CCScene
    {
        public StartScene (CCGameView gameview):base(game view)
    

    Following is quick sample I how I did it. Please advise comment if this is incorrect as still very new to game development.
    I would like to see Scene Template files added to project templates.

    I hope this helps others.

    public partial class ViewController : UIViewController
    {
        public ViewController (IntPtr handle) : base (handle)
        {
        }
    
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
    
            if (GameView != null) {
                // Set loading event to be called once game view is fully initialised
                GameView.ViewCreated += LoadGame;
            }
        }
    
    void LoadGame (object sender, EventArgs e)
        {
            CCGameView gameView = sender as CCGameView;
    
            if (gameView != null) {
    
                // other stuff from new templates etc
    
                gameView.Stats.Enabled = true;  //  <- Enable stats view !!!!!
    
                CCScene sceneStart = new StartScene (gameView);     //  <----- Create new scene here with gameView
                gameView.RunWithScene (sceneStart);
            }
        }
    

    Then for a scene template I have

    public class StartScene : CCScene
    {
        public StartScene (CCGameView gameview):base(gameview)
        {
            this.AddLayer (new startLayer());
        //  Add other layers like background etc   (even though I used CCLayerColor in this example)
        }
    }
    

    A Layer templete

    public class startLayer : CCLayerColor
    {
        CCSprite sp1;
    
        public startLayer () : base (CCColor4B.Yellow)
        {
            // Load and instantate your assets here
    
            sp1 = new CCSprite ("shark");
            sp1.PositionX = 300;
            sp1.PositionY = 300;
        }
    
        protected override void AddedToScene ()
        {
            base.AddedToScene ();
    
               // other stuff from new templates etc
    
            AddChild (sp1);
        }
    
        void OnTouchesEnded (List<CCTouch> touches, CCEvent touchEvent)
        {
            if (touches.Count > 0) {
                // Perform touch handling here
    
                // The director is "part of" / "comes with" the Gameview the in the constructor of the scene
                // So no need to create your own director !!!!!!!!!
                Director.PushScene (new  GameScene (GameView));   //    <------ Create new Scene with GameView
            }
        }
    }
    

    And for another scene

    public class GameScene : CCScene
    {
        public GameScene (CCGameView gameview):base(gameview)
        {
            this.AddLayer (new GameLayer());
        //  Add other layers like background etc   (even though I used CCLayerColor in this example)
        }
    }
    

    And the Layer for the scene.
    PopScene example to exit and return to previous scene

    public class GameLayer : CCLayerColor
    {
        CCSprite RedSq;
        CCSprite YellowSq;
    
        public GameLayer () : base (CCColor4B.AliceBlue)
        {
            // Load and instantate your assets here
            // Make any renderable node objects (e.g. sprites) children of this layer
    
            RedSq = new CCSprite ("redsq");
            RedSq.PositionX = 200;
            RedSq.PositionY = 200;
    
            YellowSq = new CCSprite ("yellowsq");
            YellowSq.PositionX = 400;
            YellowSq.PositionY = 500;
        }
    
        protected override void AddedToScene ()
        {
            base.AddedToScene ();
    
            // other stuff from new templates etc
    
            AddChild (RedSq);
            AddChild (YellowSq);
        }
    
        void OnTouchesEnded (List<CCTouch> touches, CCEvent touchEvent)
        {
            if (touches.Count > 0) 
            {
                // Perform touch handling here
                Director.PopScene ();   //  exits and return to previous scene
            }
        }
    }
    
  • DnyaneshDnyanesh Member ✭✭

    attached 2 files with this , can u suggest me where i m wrong

Sign In or Register to comment.