2010-05-09 21 views
0

(學習的Cocos2D)如何獲取作爲CCScene的子項的CCLabel的位置(x,y)?

創建CCLabel並將其添加到一個CCLayer這樣後:

//From HelloWorldScene.m 

// create and initialize a Label 
CCLabel* label1 = [CCLabel labelWithString:@"Hello" fontName:@"Marker Felt" fontSize:10]; 

label1.position = ccp(35, 435); 

// add the label as a child to this Layer 
[self addChild: label1]; 

如何確定當用戶觸摸屏幕上的標籤?

回答

0

首先你需要註冊你的場景來接收觸摸事件。因此在現場的-(id)init方法中設置self.isTouchEnabled = YES。然後在場景中添加一個方法來註冊觸摸調度器。

- (void)registerWithTouchDispatcher 
{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self 
              priority:0 
              swallowsTouches:YES]; 
} 

的獲得來自UITouch位置時,現場得到了ccTouchBegan:withEvent:消息。

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 
} 

最後,您可以通過查看標籤的邊界框來測試相對於標籤位置的觸摸位置。

if (CGRectContainsPoint([label boundingBox], location)) { 
    // The label is being touched. 
} 
相關問題