- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章iOS开发中使用cocos2d添加触摸事件的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
CCLayer类是用来接收触摸输入的。不过你要首先启用这个功能才可以使用它。你通过设置isTouchEnabled为YES来让层接收触摸事件
。
此项设定最好在init方法中设置。你可以在任何时间将其设置为NO或者YES.
。
一旦启用isTouchEnabled属性,许多与接收触摸输入相关的方法将会开始被调用。这些事件包括:当新的触摸开始的时候,当手指在触摸屏上移动的时候,还有在用户手指离开屏幕以后。很少会发生触摸事件被取消的情况,所以你可以在大多数情况下忽略它,或者使用ccTouchesEnded方法来处理.
当手指首次触摸到屏幕时调用的方法
。
手指在屏幕上移动时调用的方法
。
当手指从屏幕上提起时调用的方法
。
当触摸事件被取消时调用的方法
。
取消事件的情况很少发生,所以在大多数情况下它的行为和触摸结束时相同.
因为触摸事件由Cocoa TouchAPI接收,所以触摸的位置必须被转换为OpenGL的坐标.
以下是一个用来转换坐标的方法
。
。
默认情况下,层接收到的事件和苹果UIResponder类接收到的是一样的。cocos2d也支持有针对性的触摸处理。和普通处理的区别是:它每次只接收一次触摸,而UIResponder总是接收到一组触摸。有针对性的触摸事件处理只是简单的把一组触摸事件分离开来,这样就可以根据游戏的需求提供所需的触摸事件。更重要的是,有针对性的处理允许你把某些触摸事件从队列里移除。这样的话,如果触摸发生在屏幕某个指定的区域,你会比较容易识别出来;识别出来以后你就可以把触摸标记为已经处理,并且其它所有的层都不再需要对这个区域再次做检查.
在你的层中添加以下方法可以启用有针对性的触摸事件处理
。
。
注:如果你把registerWithTouchDispatcher方法留空,你将不会接收到任何触摸事件!如果你想保留此方法,而且使用它的默认处理方式,你必须调用[super registerWithTouchDispatcher]这个方法.
现在,你将使用一套有点不一样的方法来代替默认的触摸输入处理方法。它们几乎完全一样,除了一点:用 (UITouch *)touch 代替 (NSSet *)touches 作为方法的第一个参数
-(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,那就意味着你不想让当前的触摸事件传导到其它触摸事件处理器。你实际上是“吞下了”这个触摸事件.
下面来看一个完整的例子: 在自己的layer里面,添加 。
。
以下方法是cocos2d类库的方法:
-(void) setIsTouchEnabled:(BOOL)enabled 。
。
{ 。
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],
} 。
。
。
接下来就实现方法:
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 。
。
{ 。
// 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,
。
} 。
。
最后此篇关于iOS开发中使用cocos2d添加触摸事件的方法的文章就讲到这里了,如果你想了解更多关于iOS开发中使用cocos2d添加触摸事件的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
IO 设备如何知道属于它的内存中的值在memory mapped IO 中发生了变化? ? 例如,假设内存地址 0 专用于保存 VGA 设备的背景颜色。当我们更改 memory[0] 中的值时,VGA
我目前正在开发一个使用Facebook sdk登录(通过FBLoginView)的iOS应用。 一切正常,除了那些拥有较旧版本的facebook的人。 当他们按下“使用Facebook登录”按钮时,他
假设我有: this - is an - example - with some - dashesNSRange将使用`rangeOfString:@“-”拾取“-”的第一个实例,但是如果我只想要最后
Card.io SDK提供以下详细信息: 卡号,有效期,月份,年份,CVV和邮政编码。 如何从此SDK获取国家名称。 - (void)userDidProvideCreditCardInfo:(Car
iOS 应用程序如何从网络服务下载图片并在安装过程中将它们安装到用户的 iOS 设备上?可能吗? 最佳答案 您无法控制应用在用户设备上的安装,因此无法在安装过程中下载其他数据。 只需在安装后首次启动应
我曾经开发过一款企业版 iOS 产品,我们公司曾将其出售给大型企业,供他们的员工使用。 该应用程序通过 AppStore 提供,企业用户获得了公司特定的配置文件(包含应用程序配置文件)以启用他们有权使
我正在尝试将 Card.io SDK 集成到我的 iOS 应用程序中。我想为 CardIO ui 做一个简单的本地化,如更改取消按钮标题或“在此保留信用卡”提示文本。 我在 github 上找到了这个
我正在使用 CardIOView 和 CardIOViewDelegate 类,没有可以设置为 YES 的 BOOL 来扫描 collectCardholderName。我可以看到它在 CardIOP
我有一个集成了通话工具包的 voip 应用程序。每次我从我的 voip 应用程序调用时,都会在 native 电话应用程序中创建一个新的最近通话记录。我在 voip 应用程序中也有自定义联系人(电话应
iOS 应用程序如何知道应用程序打开时屏幕上是否已经有键盘?应用程序运行后,它可以接收键盘显示/隐藏通知。但是,如果应用程序在分屏模式下作为辅助应用程序打开,而主应用程序已经显示键盘,则辅助应用程序不
我在模拟器中收到以下错误: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedIm
如 Apple 文档所示,可以通过 EAAccessory Framework 与经过认证的配件(由 Apple 认证)进行通信。但是我有点困惑,因为一些帖子告诉我它也可以通过 CoreBluetoo
尽管现在的调试器已经很不错了,但有时找出应用程序中正在发生的事情的最好方法仍然是古老的 NSLog。当您连接到计算机时,这样做很容易; Xcode 会帮助弹出日志查看器面板,然后就可以了。当您不在办公
在我的 iOS 应用程序中,我定义了一些兴趣点。其中一些有一个 Kontakt.io 信标的名称,它绑定(bind)到一个特定的 PoI(我的意思是通常贴在信标标签上的名称)。现在我想在附近发现信标,
我正在为警报提示创建一个 trigger.io 插件。尝试从警报提示返回数据。这是我的代码: // Prompt + (void)show_prompt:(ForgeTask*)task{
您好,我是 Apple iOS 的新手。我阅读并搜索了很多关于推送通知的文章,但我没有发现任何关于 APNS 从 io4 到 ios 6 的新更新的信息。任何人都可以向我提供 APNS 如何在 ios
UITabBar 的高度似乎在 iOS 7 和 8/9/10/11 之间发生了变化。我发布这个问题是为了让其他人轻松找到答案。 那么:在 iPhone 和 iPad 上的 iOS 8/9/10/11
我想我可以针对不同的 iOS 版本使用不同的 Storyboard。 由于 UI 的差异,我将创建下一个 Storyboard: Main_iPhone.storyboard Main_iPad.st
我正在写一些东西,我将使用设备的 iTunes 库中的一部分音轨来覆盖 2 个视频的组合,例如: AVMutableComposition* mixComposition = [[AVMutableC
我创建了一个简单的 iOS 程序,可以顺利编译并在 iPad 模拟器上运行良好。当我告诉 XCode 4 使用我连接的 iPad 设备时,无法编译相同的程序。问题似乎是当我尝试使用附加的 iPad 时
我是一名优秀的程序员,十分优秀!