2011年6月16日 星期四

let the CCSprite detect touch(讓CCSprite 觸發 touch 事件)

1. 首先在你的 CCLayer  開啟觸碰
    self.isTouchEnabled = YES;


2. 然後一樣在 CCLayer 加入這一段

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    UITouch* touch = [touches anyObject];
    //先取得touch 的坐標
  CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
  //驗證坐標
CCSprite *m_targetSprite = xxx; <==取得目標物件
    if (CGRectContainsPoint(m_targetSprite.boundingBox, location))
    {
//touch.....
}
}
附註: 有時CCLayer 的坐標也要考慮進去才行~~~

沒有留言:

張貼留言