2010-07-14 59 views
0

我在花栗鼠中創建一個靜態形狀(使用SpaceManager)並將cpCCSprite附加到它。當連接到靜態物體時更改CCSprite錨點

但是,我需要精靈的錨點偏離中心,但是當我更改精靈的錨點時,精靈形狀不再對齊。

所以,如果我改變錨點這樣

[sprite setAnchorPoint:ccp(0.5, 0.3)]; 

的精靈被相應制定的,但我預計形狀爲「移動」的程度。這是我的意思。

how the cannon should look http://www.tomelders.com/bin/cannon.png

左邊是形狀和正確對齊的精靈。我沒有改變錨點。

右邊是與ccp(0.5, 0.3)

我也是換湯不換藥每一幀靜態形狀的錨點的精靈。

下面是它是如何創建

// create the sensor 
sensor = [spaceMgr addRectAt:pPoint mass:STATIC_MASS width:53 height:81 rotation:0]; 
    sensor->sensor = YES; 
    sensor->collision_type = 2; 

    //Create the sprite 
    CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bownce-sprites-comic-sized.png"]; 

    barrel = [[cpCCSprite node] initWithShape:sensor texture:texture rect:CGRectMake(3, 428, 53, 82)]; 
    [self addChild:barrel]; 

    // set the ancor point 
    [barrel setAnchorPoint:ccp(0.5, 0.3)]; 
    [barrel setPosition:pPoint]; 

回答

7

這是anchorPoints如何工作的一個普遍的誤解。 anchorPoint有什麼也沒有與節點的位置。相反,它是節點對象紋理的偏移量!

默認的anchorPoint位於0.5,0.5,這意味着紋理以節點的位置爲中心。如果將anchorPoint設置爲0,0,則紋理的左下角與節點的位置對齊。紋理有效地向右和向上移動。然而,節點的位置並沒有改變。

由於形狀與節點的位置對齊,所以當您更改anchorPoint時它不​​會移動。解決這個問題的最簡單方法是改變圖像,例如通過僅使用透明顏色將其尺寸增加到一側。我希望這是可以理解的。

+0

舊的線程,但如果您將錨點設置爲0,0這是否意味着精靈的位置與UIKit的方式相同? – 2011-10-28 21:59:42

+1

不完全。 UIView的0,0位置位於屏幕的左上角,向右和向下延伸。忽略不同的軸方向,您將不得不將anchorPoint設置爲0,1以定位類似於UIKit視圖的cocos2d節點。 – LearnCocos2D 2011-10-30 21:52:20

+0

FYI Cocos2D 0,0位置位於屏幕的左下角。 – LearnCocos2D 2011-10-30 21:52:51