程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 03、坐標和錨點,03,坐標

03、坐標和錨點,03,坐標

編輯:C#入門知識

03、坐標和錨點,03,坐標


一、坐標

1、在CocosSharp裡面坐標系的原點即(0,0)位於畫面的左下角,左上角即(0,y),右下角即(x,0),右上角則(x,y)

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的時候,錨點就在對象的正中間。

申明:未經允許不得轉載

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved