2010-05-09 32 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. 
} 
相关问题