在我的遊戲中,我有兩種類型的手勢;一個輕拍和一個按住。當我將手指放在屏幕上時,會導致調用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