2011-12-01 112 views
0

我正在將對象從一個軸移動到另一個軸,當我觸摸它時停止在該位置上,但是如何才能找到它的當前位置,當用戶觸摸時停在那個位置,當用戶雙擊它時,它會向另一個方向移動,我想知道當用戶觸摸時停在那個位置時對象的當前位置。如何在cocos2d中找到精靈的當前位置

任何想法。 。 關於那個。


日Thnx的答覆阿爾薩蘭,但我的問題是,我有3個精靈..

sprite.positoin ..,sprite1。位置.....,sprite2.position ......,並在我的遊戲邏輯中做這件事。

[sprite addChild:sprite1]; [sprite addChild:sprite2]; ok

我在sprite中插入了一個動作。但是當用戶觸摸它時會停止動畫,我必須記下精靈的最後位置以及精靈1的位置。 (記住,sprite1是精靈的subchild。)

我怎麼能找出sprite1.position.x值

任何解決方案?

+0

嗯...... CGPoint = sprite.position? – gixdev

回答

0

嘛獲得CCSprite的位置在遊戲中,你可以使用下面的代碼

mySprite.position; 

要確定任何時候,如果你有感動你需要首先獲得精靈的矩形精靈

CGRect rect = CGRectMake(mySprite.position.x-(mySprite.contentSize.width/2), mySprite.position.y-(mySprite.contentSize.height/2), 
         mySprite.contentSize.width, mySprite.contentSize.height; 

然後您必須檢查觸摸位置是否在精靈矩形內。

if (CGRectContainsPoint(rect, touchLocation)) { 
    // code to stop the movement of sprite 
} 

通過上面的代碼,你可以有一個想法,繼續在哪個方向。您還可以查看點Apple Documentation點和rects。

問候,

阿爾斯蘭

+0

Aslam o alkum,阿爾斯蘭,我向前發佈了我的確切問題。但看到這個,並給我任何建議.. – Developer

+1

thnx的答覆Arsalan,但我的問題是,我有3個精靈.. sprite.positoin ..,sprite1。位置.....,sprite2.position ......,並在我的遊戲邏輯中做這件事。 [sprite addChild:sprite1]; [sprite addChild:sprite2];好吧 我在精靈中插入了一個動作。但是當用戶觸摸它時會停止動畫,我必須記下精靈的最後位置以及精靈1的位置。 (請記住,sprite1是sprite的子元素。) 如何找到sprite1.position.x值 的任何解決方案? – Developer