Touch Handling in Cocos2D 3.x(三)

简介:

取得触摸位置

最有趣的部分是触摸的位置.接下来我们将使用触摸位置在玩家每次点击的屏幕位置上添加精灵.为了完成这项功能我们需要修改touchBegan的实现,替换旧的代码如下:

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    // we want to know the location of our touch in this scene
    CGPoint touchLocation = [touch locationInNode:self];
    // create a 'hero' sprite
    CCSprite *hero = [CCSprite spriteWithImageNamed:@"hero.png"];
    [self addChild:hero];
    // place the sprite at the touch location
    hero.position = touchLocation;
}

该实现相当直截了当.最有趣的部分是取得实际触摸的位置.locationInNode方法将触摸的OpenGL视图坐标转换为作为一个参数传递给该方法的Node内的坐标.(在这个例子中,因为我们需要知道屏幕触摸实际在MainScene中的位置).

一旦我们取得了触摸位置,我们简单创建一个新的CCSprite节点并且将它添加到MainScene中去.其中”hero.png”图片资源是你在一开始下载的starter project中的一部分.

一旦你运行项目,你应该能满屏幕放置MGWU英雄们.

这里写图片描述

现在你已经对基础非常熟悉了,我们将进一步观察触摸生命周期以及你如何在你的游戏中创建复杂的触摸处理.

相关文章
|
11月前
|
Dart 安全
Flutter开发Cannot run with sound null safety报错
Flutter开发Cannot run with sound null safety报错
|
缓存 Android开发
【错误记录】Android Studio 编译报错 ( A problem occurred starting process ‘command ‘ninja.exe‘ ‘ )
【错误记录】Android Studio 编译报错 ( A problem occurred starting process ‘command ‘ninja.exe‘ ‘ )
886 0
【错误记录】Android Studio 编译报错 ( A problem occurred starting process ‘command ‘ninja.exe‘ ‘ )
|
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.
94 0
support mobile touch event