你的位置:首页 > ASP.net教程

[ASP.net教程]03、坐标和锚点


一、坐标

1、在CocosSharp里面坐标系的原点即(0,0)位于画面的左下角,左上角即(0,y),右下角即(x,0),右上角则(x,y)

PositionAndAnchorPoint

2、我们在画面里插入文本来显示坐标,右击项目,创建一个名为“PositionAndAnchorPointLayer”的文件,删除一些暂时没有用的代码,以及小小修改后

2.1 PositionAndAnchorPointLayer.cs文件代码:

using System;
using System.Collections.Generic;
using CocosSharp;

namespace htest
{
    public class PositionAndAnchorPointLayer:CCLayerColor
    {
        public PositionAndAnchorPointLayer ():base(CCColor4B.Black)
        {
            
        }

        protected override void AddedToScene ()
        {
            base.AddedToScene ();
        }
    }
}

2.2 ViewController.cs文件代码:

using System;
using System.Collections.Generic;
using UIKit;
using CocosSharp;

namespace htest
{
    public partial class ViewController : UIViewController
    {
        public ViewController (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            if (GameView != null) {
                GameView.ViewCreated += LoadGame;
            }
        }

        public override void ViewWillDisappear (bool animated)
        {
            base.ViewWillDisappear (animated);

            if (GameView != null)
                GameView.Paused = true;
        }

        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);

            if (GameView != null)
                GameView.Paused = false;
        }

        public override void DidReceiveMemoryWarning ()
        {
            base.DidReceiveMemoryWarning ();
        }

        void LoadGame (object sender, EventArgs e)
        {
            CCGameView gameView = sender as CCGameView;

            if (gameView != null) {
                var contentSearchPaths = new List<string> () { "Fonts", "Sounds" };
                CCSizeI viewSize = gameView.ViewSize;

                int width = viewSize.Width;
                int height = viewSize.Height;

                gameView.DesignResolution = new CCSizeI (width, height);

                if (width < viewSize.Width) {
                    contentSearchPaths.Add ("Images/Hd");
                    CCSprite.DefaultTexelToContentSizeRatio = 2.0f;
                } else {
                    contentSearchPaths.Add ("Images/Ld");
                    CCSprite.DefaultTexelToContentSizeRatio = 1.0f;
                }

                gameView.ContentManager.SearchPaths = contentSearchPaths;

                CCScene gameScene = new CCScene (gameView);
                gameScene.AddLayer (new PositionAndAnchorPointLayer());
                gameView.RunWithScene (gameScene);

            }
        }
    }
}

3、编写代码,显示坐标,修改PositionAndAnchorPointLayer类里面的AddedToScene方法,运行起来就是我们《1》看到的样子

protected override void AddedToScene ()
        {
            base.AddedToScene ();

            CCSize size = VisibleBoundsWorldspace.Size;

            CCLabel l1 = new CCLabel ("0,0","Arial", 50, CCLabelFormat.SystemFont);
            l1.Color = new CCColor3B (CCColor4B.Blue);
            l1.PositionX = 0;
            l1.PositionY = 0;
            l1.AnchorPoint = new CCPoint (0, 0);
            AddChild (l1);

            CCLabel l2 = new CCLabel ("x,0","Arial", 50, CCLabelFormat.SystemFont);
            l2.Color = new CCColor3B (CCColor4B.Blue);
            l2.PositionX = size.Width;
            l2.PositionY = 0;
            l2.AnchorPoint = new CCPoint (1, 0);
            AddChild (l2);

            CCLabel l3 = new CCLabel ("0,y","Arial", 50, CCLabelFormat.SystemFont);
            l1.Color = new CCColor3B (CCColor4B.Blue);
            l3.PositionX = 0;
            l3.PositionY = size.Height ;
            l3.AnchorPoint = new CCPoint (0, 1);
            AddChild (l3);

            CCLabel l4 = new CCLabel ("x,y","Arial", 50, CCLabelFormat.SystemFont);
            l1.Color = new CCColor3B (CCColor4B.Blue);
            l4.PositionX = size.Width ;
            l4.PositionY = size.Height ;
            l4.AnchorPoint = new CCPoint (1, 1);
            AddChild (l4);
        }

二、锚点

1、所谓的锚点就是向画面输出对象时的基准点。锚点也和坐标一样,也有x和y组成,但是范围都是0-1之间。x=0表示基准点在对象的左侧,x=1在对象的右侧,y=0在对象的底部,则y=1时在对象的上方,锚点通常都有对象的AnchorPoint属性来设置。当x和y都等于0.5的时候,锚点就在对象的正中间。

申明:未经允许不得转载