zoukankan      html  css  js  c++  java
  • Cocos-2d 坐标系及其坐标转换

    anchor point 究竟是怎么回事? 之所以造成不容易理解的是因为我们平时看待一个图片是 以图片的中心点 这一个维度来决定图片的位置的。而在cocos2d中决定一个 图片的位置是由两个维度 一个是 position 也就是图片的中心点 另外一个是anchor point。只要我们搞清楚他们的关系,自然就迎刃而解。

    他们的关系是这样的: 

    actualPosition.x = position.x + width*(0.5 - anchor_point.x); acturalPosition.y = position.y + height*(0.5 - anchor_point.y)

    actualPosition 是sprite实际上在屏幕显示的位置, poistion是 程序设置的, achor_point也是程序设置的。

    具体看下面的例子一:

    1. CCSprite *sprite = [CCSprite spritewithFile:@"blackSquare.png"];  
    2. sprite.position=ccp(0,0);  
    3. sprite.anchorPoint=ccp(0,0);  
    4. [self addChild:sprite];  


    具体效果如下:

    根据上面的公式: 假设精灵的width = height = 10.

    actualPosition.x = 0 + 10*(0.5 - 0) = 5; actualPosition.y  = 0 + 10*(0.5 - 0) = 5; 

    (5, 5) 这个结果正是现在图片的在屏幕上的实际位置。

    例子 二:

    1. CCSprite *sprite = [CCSprite spritewithFile:@"blackSquare.png"];  
    2. sprite.position=ccp(0,0);  
    3. sprite.anchorPoint=ccp(-1,-1);  
    4. [self addChild:sprite];  


    具体效果如下:

    根据上面的公式: 假设精灵的width = height = 10.

    actualPosition.x = 0 + 10*(0.5 - (-1)) = 15; actualPosition.y  = 0 + 10*(0.5 - (-1)) = 15; 

    (15, 15) 这个结果正是现在图片的在屏幕上的实际位置。

    例子三

    1. CCSprite *sprite = [CCSprite spritewithFile:@"blackSquare.png"];  
    2. sprite.anchorPoint=ccp(1,1);  
    3. sprite.position=ccp(sprite.contentSize.width , sprite.contentSize.height);  
    4. [self addChild:sprite];  

    根据上面的公式: 假设精灵的width = height = 10.

    actualPosition.x = 10 + 10*(0.5 - (1)) = 5; actualPosition.y  = 10 + 10*(0.5 - (1)) = 5; 

    (5, 5) 这个结果正是现在图片的在屏幕上的实际位置。

  • 相关阅读:
    light oj 1007
    51nod 1298 圆与三角形
    codeforces 899C Dividing the numbers
    zznu 1996 : 正三角形和圆的爱情
    zznu 2081 : 舰队管理
    zzun 2076 : 三花聚顶神功
    zznu 2054 : 油田
    机械设备--第九届省赛--深搜
    设计模式-单例模式、工厂模式
    Spring Boot 遇到空指针
  • 原文地址:https://www.cnblogs.com/wudan7/p/3853609.html
Copyright © 2011-2022 走看看