Cocos2D for iPhone 的觸摸事件

來源:互聯網
上載者:User

From:http://b.imi.im/?p=215

除了 Layer 可以接受觸摸事件, 在Cocos2D

0.8以後加入一個新的特性,從而讓所有的對象都可以接受觸摸事件. 發現大家都不怎麼用這個方法,這兒簡單介紹一下.

首先添加事件接收者:

[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:NO];

//self為接收者, 優先順序參數從0開始 數字越小優先順序越高,就會越先接收到事件, 最後一個參數表示是否阻止此次事件冒泡

然後實現3個方法:

#pragma mark TouchDispatcherDelegate

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

//你的代碼

return YES; //這兒如果返回NO 此次觸摸將被忽略

}

- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event

{

//你的代碼

}

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{

//你的代碼

}

這樣,就可以像處理UIView

裡的事件一樣去處理coco2d了.

Enjoy ur coding

Edit: 別忘了刪除監聽者, 要不然……

[[TouchDispatcher sharedDispatcher] removeDelegate:self];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.