Touch Handling in Cocos2D 3.x(六)

简介:

使英雄变成可触碰的对象

这是另一个非常有用的特性.很多用户需要捡起已经存在的英雄然后满屏幕移动它们.让我们按以下步骤实现该功能:

  • 如果用户触摸屏幕空白位置,一个新的英雄将被创建
  • 如果用户触摸一个已经存在的英雄,不会有新英雄被创建,相反Ta可以移动已存在的那个英雄

为了实现这些,我们需要创建一个CCSprite的子类.前进去创建一个名为CCDragSprite的类,该类继承自CCSprite:

这里写图片描述

在CCDragSprite.h中添加以下一行导入Cocos2d的头文件:

#import "cocos2d.h"

现在我们可以在CCDragSprite.m中关注实现部分了!我想你保证它将惊人的简单.我们需要做如下步骤:

  • 一旦CCDragSprite出现在屏幕就打开用户交互选项
  • 让CCDragSprite跟随着触摸而移动

在CCDragSprite.m中的@implementation和@end之间添加如下代码:

- (void)onEnter {
    self.userInteractionEnabled = TRUE;
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
}
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    // we want to know the location of our touch in this scene
    CGPoint touchLocation = [touch locationInNode:self.parent];
    self.position = touchLocation;
}

现在你应该大致了解上面的代码了.当我们的CCDragSprite进入屏幕时,我们打开它的交互选项.我们需要增加一个touchBegan方法的空白实现,这是为了让Cocos2d知道我们想要认领该触摸(否则下一层的responder将会接收到这次触摸).

相关文章
|
6月前
|
编译器 Android开发 Windows
[√]cocos2dx关于retain release
[√]cocos2dx关于retain release
32 0
|
C++ Windows
编译WINDOWS版SDL2:You should run hg revert SDL_config.h
编译WINDOWS版SDL2:You should run hg revert SDL_config.h
516 0
|
JavaScript
support mobile touch event
If extra event support is needed, jQuery mobile event plugin is loaded to support tap, swipe and scrollstart/stop events. The old touch to mouse event simulation will be deregistered and the new one will be activated.
95 0
support mobile touch event
SAP UI5 busy Dialog debug
# Created by Wang, Jerry, last modified on Jul 04, 2015
102 0
SAP UI5 busy Dialog debug