在我的游戏中,我有两种类型的手势;一个轻拍和一个按住。当我将手指放在屏幕上时,会导致调用touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
,因为我的tapper方法也被调用,因此两个手势都被调用。我怎样才能避免这个问题?我必须使用UIGestureRecognizers
还是有办法使用内置的cocos2d方法?我需要这些手势被专门称呼,而不是相互结合。如何区分轻按和按住?
typedef NS_ENUM(NSUInteger, BPMovementState) {
kTouchUp, //Finger is not on the screen
kTouchDown //Finger is on the screen
};
@implementation HelloWorldScene
{
CCSprite *_hero;
BPMovementState _touchState;
}
- (instancetype)init
self = [super init];
if (!self) return(nil);
// Enable touch handling on scene node
self.userInteractionEnabled = YES;
_touchState = kTouchUp;
return self;
}
- (void)fixedUpdate:(CCTime)delta{
if(_touchState == kTouchDown){
//_hero slide
_hero.position = ccp(_hero.position.x + 1, _hero.position.y);
}
}
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
_touchState = kTouchDown;
if(touch.tapCount == 1)
[_hero jump]
}
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
_touchState = kTouchUp;
}
您可以注意触摸开始和触摸结束之间的时间。之后,您可以自定义识别轻触并按住的条件。 – Renaissance 2014-09-19 23:44:16
水龙头是友谊的象征。持有是爱的标志。 – quellish 2014-09-20 03:16:07