广西北海溺水事故,蘑菇街2013新款冬装,小狒狒音乐天空
cclayer类是用来接收触摸输入的。不过你要首先启用这个功能才可以使用它。你通过设置istouchenabled为yes来让层接收触摸事件:
一旦启用istouchenabled属性,许多与接收触摸输入相关的方法将会开始被调用。这些事件包括:当新的触摸开始的时候,当手指在触摸屏上移动的时候,还有在用户手指离开屏幕以后。很少会发生触摸事件被取消的情况,所以你可以在大多数情况下忽略它,或者使用cctouchesended方法来处理。
当手指首次触摸到屏幕时调用的方法:
手指在屏幕上移动时调用的方法:
当手指从屏幕上提起时调用的方法:
当触摸事件被取消时调用的方法:
取消事件的情况很少发生,所以在大多数情况下它的行为和触摸结束时相同。
因为触摸事件由cocoa touchapi接收,所以触摸的位置必须被转换为opengl的坐标。
以下是一个用来转换坐标的方法:
默认情况下,层接收到的事件和苹果uiresponder类接收到的是一样的。cocos2d也支持有针对性的触摸处理。和普通处理的区别是:它每次只接收一次触摸,而uiresponder总是接收到一组触摸。有针对性的触摸事件处理只是简单的把一组触摸事件分离开来,这样就可以根据游戏的需求提供所需的触摸事件。更重要的是,有针对性的处理允许你把某些触摸事件从队列里移除。这样的话,如果触摸发生在屏幕某个指定的区域,你会比较容易识别出来;识别出来以后你就可以把触摸标记为已经处理,并且其它所有的层都不再需要对这个区域再次做检查。
在你的层中添加以下方法可以启用有针对性的触摸事件处理:
注:如果你把registerwithtouchdispatcher方法留空,你将不会接收到任何触摸事件!如果你想保留此方法,而且使用它的默认处理方式,你必须调用[super registerwithtouchdispatcher]这个方法。
现在,你将使用一套有点不一样的方法来代替默认的触摸输入处理方法。它们几乎完全一样,除了一点:用 (uitouch *)touch 代替 (nsset *)touches 作为方法的第一个参数:
-(void) cctouchmoved:(uitouch *)touch withevent:(uievent *)event {}
-(void) cctouchended:(uitouch *)touch withevent:(uievent *)event {}
-(void) cctouchcancelled:(uitouch *)touch withevent:(uievent *)event {}
这里很重要的一点是:cctouchbegan返回的是一个布尔值(bool)。如果你返回了yes,那就意味着你不想让当前的触摸事件传导到其它触摸事件处理器。你实际上是“吞下了”这个触摸事件。
下面来看一个完整的例子:
在自己的layer里面,添加
以下方法是cocos2d类库的方法:
{
if( istouchenabled_ != enabled ) {
istouchenabled_ = enabled;
if( isrunning_ ) {
if( enabled )
[self registerwithtouchdispatcher];
else {
ccdirector *director = [ccdirector shareddirector];
[[director touchdispatcher] removedelegate:self];
}
}
}
}
//这句是关键
-(void) registerwithtouchdispatcher
{
ccdirector *director = [ccdirector shareddirector];
[[director touchdispatcher] addstandarddelegate:self priority:0];
}
接下来就实现方法:
{
// for( uitouch *touch in touches ) {
// cgpoint location = [touch locationinview: [touch view]];
//
// location = [[ccdirector shareddirector] converttogl: location];
// cgpoint touchpos = location;
//
// nslog(@"point==%@",nsstringfromcgpoint(touchpos));
// }
uitouch* touch = [touches anyobject];
cgpoint location = [touch locationinview: [touch view]];
location = [[ccdirector shareddirector] converttogl: location];
//self.position = location;
nslog(@"point==%@",nsstringfromcgpoint(location));
}
- (void)cctouchended:(uitouch *)touch withevent:(uievent *)event {
self.ismoving = false;
}
- (bool)cctouchbegan:(uitouch *)touch withevent:(uievent *)event {
// you can set up a check here if you're not interested in handling every touch.
// for example if your player is already moving, return no...
bool handletouch = false;
if (!self.ismoving) {
handletouch = true;
self.ismoving = true;
}
return handletouch;
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
网友评论