检测UIGestureRecognizer何时上下左右Cocos2d上下左右、UIGestureRecognizer、Cocos2d

2023-09-06 14:46:16 作者:笑望红尘

我有一个 CCSprite,我想用手势四处移动.问题是我对 Cocos2D 完全陌生.我希望我的精灵在手势向上时执行一个动作,在手势向下时执行另一个动作,在手势正确时执行另一个动作,向左执行相同操作.有人可以指出我正确的方向吗?

I have a CCSprite that I want to move around using gestures. Problem is I'm completely new to Cocos2D. I want my sprite to perform one action when the gesture is up, another one when gesture is down, another action when gesture is right and same thing for left. Can someone point me in the right direction?

谢谢!

推荐答案

显然每个 UISwipeGestureRecognizer 只能检测到给定方向的滑动.即使方向标志可以被或在一起,UISwipeGestureRecognizer 也会忽略其他标志.

Apparently each UISwipeGestureRecognizer can only detect the swipe in the given direction. Even though the direction flags could be OR'ed together the UISwipeGestureRecognizer ignores the additional flags.

解决方案是为您希望识别滑动手势的每个方向添加一个 UISwipeGestureRecognizer,并将每个识别器的方向相应地设置为上、下、左和右.如果你想测试任何方向的滑动,你必须添加四个 UISwipeGestureRecognizers.

The solution is to add one UISwipeGestureRecognizer for each direction you want the swipe gesture to be recognized, and set each recognizer's direction accordingly to either up, down, left and right. If you want to test for a swipe in any direction you'll have to add four UISwipeGestureRecognizers.

这有点奇怪,但这是对我有用的唯一方法.

It's kind of odd but that's the only way it worked for me.