Cocos2d技巧之用户交互-触碰,按住和拖曳操作
触碰,按住和拖曳操作是应用和游戏中最常使用的交互技术。这三种操作是用户界面交互的基石,同时也是和游戏中物体交互的基础。
在这个示例中,我们创建了一个CCSprite的分类,从而可以让精灵对象处理触摸事件,并维持某种特定的状态。此外还添加了部分逻辑让用户可以触碰,按住和拖曳该精灵对象。
示例项目请参考附件(Xcode4.6+iOS6.1.2+cocos2dv2.1 rc0a)
http://vdisk.weibo.com/s/tlzvl
Step1.在Xcode中创建一个新项目,命名为TapHoldDrag
Step2.将resources文件夹中的文件拖到项目中,确保选中”copy….into…”,并勾选项目名称。
Step3.在Xcode中切换到HelloWorld.m,在文件顶部添加以下代码:
#import "ColorTouchSprite.h"
#import "Helpers.h"
然后在@implementationHelloWorldLayer后添加实例变量:
@implementation HelloWorldLayer{
ColorTouchSprite *colorTouchSprite;
CCLabelBMFont *message;
}
注意,这是iOS5之后的编码新规范,也即实例变量声明不需要再放在@interface部分,而是放在@implementation部分。
在init方法前添加以下方法:
-(void) step {
//Changecolor and message depending on ColorTouchSprite state
if(colorTouchSprite.touchedState ==TS_NONE){
[message setColor:ccc3(255,255,255)];
[message setString:@"Tap, hold or drag the square."];
[colorTouchSprite setColor:ccc3(255,255,255)];
}else if(colorTouchSprite.touchedState ==TS_TAP){
[message setColor:ccc3(255,0,0)];
[message setString:@"Tap."];
[colorTouchSprite setColor:ccc3(255,0,0)];
}else if(colorTouchSprite.touchedState ==TS_HOLD){
[message setColor:ccc3(0,255,0)];
[message setString:@"Hold."];
[colorTouchSprite setColor:ccc3(0,255,0)];
}else if(colorTouchSprite.touchedState ==TS_DRAG){
[message setColor:ccc3(0,0,255)];
[message setString:@"Drag."];
[colorTouchSprite setColor:ccc3(0,0,255)];
}
}
/* Process touch events */
-(void)ccTouchesBegan:(NSSet *)toucheswithEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
point = [[CCDirector sharedDirector] convertToGL: point];
if(pointIsInRect(point, [colorTouchSpriterect])){
[colorTouchSprite ccTouchesBegan:touches withEvent:event];
}
}
-(void)ccTouchesMoved:(NSSet *)toucheswithEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
point = [[CCDirector sharedDirector] convertToGL: point];
if(pointIsInRect(point, [colorTouchSpriterect])){
[colorTouchSprite ccTouchesMoved:touches withEvent:event];
}
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
point = [[CCDirector sharedDirector] convertToGL: point];
if(pointIsInRect(point, [colorTouchSpriterect])){
[colorTouchSprite ccTouchesEnded:touches withEvent:event];
}
}
然后更改init方法中的代码如下:
// on "init" you need to initialize your instance
-(id) init
{
//always call "super" init
// Applerecommends to re-assign "self" with the "super's" returnvalue
if( (self=[superinit]) ) {
self.touchEnabled =YES;
//Our message sprite
message = [CCLabelBMFontlabelWithString:@"Tap, hold or drag the square."fntFile:@"eurostile_30.fnt"];
message.position =ccp(240,260);
message.scale =0.75f;
[self addChild:message];
//Init the ColorTouchSprite
colorTouchSprite = [ColorTouchSpritespriteWithFile:@"blank.png"];
colorTouchSprite.position =ccp(240,160);
[colorTouchSprite setTextureRect:CGRectMake(0,0,100,100)];
[self addChild:colorTouchSprite];
[self schedule:@selector(step)];
return self;
}
return self;
}
这里最重要的就是可重用的ColorTouchSprite这个CCSprite的子类,我们保存了不同的状态变量,以区分触碰,按住和拖曳的几个不同状态。还指定了一个-(CGRect)rect方法来判断精灵是否被触碰。至于几个触摸的标准方法这里就不再赘述了。
当然,除了这种方法,还可以使用Cocos2d的CCTargetedTouchDelegate协议,来指定一个协议对象自动处理触摸事件,大家可以自己尝试。