iOS Dev (70) 讓 CCLayer 接受觸摸或加速計事件

iOS Dev (70) 讓 CCLayer 接受觸摸或加速計事件

  • 博客:http://blog.csdn.net/prevention
  • 作者:大銳哥
  • 摘自:Learn iPhone and iPad cocos2d Game Development

-

觸摸事件

首先要開啓接受 touch events 的開關:

self.isTouchEnabled = YES;

然後是處理函數:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {}
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {}

ccTouchBegan返回的是一個布爾值(BOOL)。如果你返 回了YES,那就意味着你不想讓當前的觸摸事件傳導到其它觸摸事件處理器。你 實際上是“吞下了”這個觸摸事件。

加速計事件

開啓開關:

self.isAccelerometerEnabled = YES;

處理函數:

-(void) accelerometer:(UIAccelerometer *)accelerometer
    didAccelerate:(UIAcceleration *)acceleration
{
    CCLOG(@"acceleration: x:%f / y:%f / z:%f",
    acceleration.x, acceleration.y, acceleration.z);
}

-

轉載請註明來自:http://blog.csdn.net/prevention

發佈了153 篇原創文章 · 獲贊 3 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章