gpt4 book ai didi

javascript - 如何在cocos2d-js中使用滑动手势?

转载 作者:行者123 更新时间:2023-11-30 17:28:07 25 4
gpt4 key购买 nike

我正在研究如何在 cocos2d-js 中使用滑动手势,发现在 cocos2d 中使用了 UISwipeGestureRecognizer。但我找不到它用于 cocos2d-js。

Gestures in cocos2d

还有 github 中的 cocos2d-x:

CCGestureRecognizer

对于cocos2d-js我只找到了

        cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesMoved:function (touches, event) {
event.getCurrentTarget().processEvent(touches[0]);
}
}, this);

具有其他事件类型:

onTouchesBegan
onTouchesEnded
onTouchesCancelled

这就是 cocos2d-js 中检测左、右、上、下滑动的所有帮助吗?

最佳答案

这是我的解决方案,针对 cocos2d-js 3.0a2 进行了测试:

   if( true || 'touches' in cc.sys.capabilities ) { // touches work on mac but return false
cc.eventManager.addListener(cc.EventListener.create({
event: cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesBegan: function(touches, event) {
console.log("onTouchesBegan!");

var touch = touches[0];
var loc = touch.getLocation();

self.touchStartPoint = {
x: loc.x,
y: loc.y
};

self.touchLastPoint = {
x: loc.x,
y: loc.y
};
},

onTouchesMoved: function(touches, event) {
var touch = touches[0];
var loc = touch.getLocation(),
start = self.touchStartPoint;

// check for left
if( loc.x < start.x - self.touchThreshold ) {
// if direction changed while swiping left, set new base point
if( loc.x > self.touchLastPoint.x ) {
start = self.touchStartPoint = {
x: loc.x,
y: loc.y
};
self.isSwipeLeft = false;
} else {
self.isSwipeLeft = true;
}
}

// check for right
if( loc.x > start.x + self.touchThreshold ) {
// if direction changed while swiping right, set new base point
if( loc.x < self.touchLastPoint.x ) {
self.touchStartPoint = {
x: loc.x,
y: loc.y
};
self.isSwipeRight = false;
} else {
self.isSwipeRight = true;
}
}

// check for down
if( loc.y < start.y - self.touchThreshold ) {
// if direction changed while swiping down, set new base point
if( loc.y > self.touchLastPoint.y ) {
self.touchStartPoint = {
x: loc.x,
y: loc.y
};
self.isSwipeDown = false;
} else {
self.isSwipeDown = true;
}
}

// check for up
if( loc.y > start.y + self.touchThreshold ) {
// if direction changed while swiping right, set new base point
if( loc.y < self.touchLastPoint.y ) {
self.touchStartPoint = {
x: loc.x,
y: loc.y
};
self.isSwipeUp = false;
} else {
self.isSwipeUp = true;
}
}

self.touchLastPoint = {
x: loc.x,
y: loc.y
};
},

onTouchesEnded: function(touches, event){
console.log("onTouchesEnded!");

var touch = touches[0],
loc = touch.getLocation()
size = self.size;

self.touchStartPoint = null;

if( !self.isSwipeUp && !self.isSwipeLeft && !self.isSwipeRight && !self.isSwipeDown ) {
if( loc.y > size.height*0.25 && loc.y < size.height*0.75 ) {
(loc.x < size.width*0.50)? self.isTouchLeft = true : self.isTouchRight = true;
} else if( loc.y > size.height*0.75 ) {
self.isTouchUp = true;
} else {
self.isTouchDown = true;
}
}

self.isSwipeUp = self.isSwipeLeft = self.isSwipeRight = self.isSwipeDown = false;

//location.y = self.size.height;
//event.getCurrentTarget().addNewTileWithCoords(location);
}
}), this);
} else {
cc.log("TOUCH_ALL_AT_ONCE is not supported");
}

关于javascript - 如何在cocos2d-js中使用滑动手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23821451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com