的Cocos2D: 你好。很长一段时间试图通过缩放捏,但并非不可能。你能告诉我们如何实现缩放。
答案 0 :(得分:5)
以下是我在其中一款游戏中用于缩放缩放的代码。
首先将此@property添加到要剪切缩放的场景的@interface(或更可能是图层):
@property (nonatomic, strong) NSMutableSet * touches;
以下是您可以添加到图层的@implementation的代码
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_touches unionSet:touches];
}
- (void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[_touches minusSet:touches];
}
- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[_touches minusSet:touches];
}
- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch * touch in touches)
{
[self pinchZoomWithMovedTouch:touch];
}
}
- (void) pinchZoomWithMovedTouch: (UITouch *) movedTouch
{
CGFloat minDistSqr = CGFLOAT_MAX;
UITouch * nearestTouch = nil;
UIView * mainView = [[CCDirector sharedDirector] view];
CGPoint newLocation = [movedTouch locationInView:mainView];
for (UITouch * touch in _touches)
{
if (touch != movedTouch)
{
CGFloat distSqr = sqrOfDistanceBetweenPoints([touch locationInView:mainView],newLocation);
if (distSqr < minDistSqr)
{
minDistSqr = distSqr;
nearestTouch = touch;
}
}
}
if (nearestTouch)
{
CGFloat prevDistSqr = sqrOfDistanceBetweenPoints([nearestTouch locationInView:mainView],
[movedTouch previousLocationInView:mainView]);
CGFloat pinchDiff = sqrtf(minDistSqr) - sqrtf(prevDistSqr);
self.scale += pinchDiff * kPinchZoomCoeff; // kPinchZoomCoeff is constant = 1.0 / 200.0f Adjust it for your needs
}
}
CGFloat sqrOfDistanceBetweenPoints(CGPoint p1, CGPoint p2)
{
CGPoint diff = ccpSub(p1, p2);
return diff.x * diff.x + diff.y * diff.y;
}
请注意,我已删除大部分代码并删除了无关的逻辑,并且无法启动此代码。如果您对此代码有任何问题,请与我们联系。
答案 1 :(得分:2)
因为我在Javascript中寻找它很久 现在尝试实现它,
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ALL_AT_ONCE,
swallowTouches: true,
kPinchZoomCoeff: 1 / 200,
_touches: {},
thresHold: [],
onTouchesBegan: function (touches, event) {
for (var i = 0; i < touches.length; i++) {
var touch = touches[i];
this._touches[touch.getID()] = touch.getLocationInView();
}
},
onTouchesCancelled: function (touches, event) {
for (var i = 0; i < touches.length; i++) {
var touch = touches[i];
delete this._touches[touch.getID()];
}
},
onTouchesEnded: function (touches, event) {
for (var i = 0; i < touches.length; i++) {
var touch = touches[i];
delete this._touches[touch.getID()];
}
},
onTouchesMoved: function (touches, event) {
for (var i = 0; i < touches.length; i++) {
var touch = touches[i];
this.pinchZoomWithMovedTouch(touch, event);
}
},
pinchZoomWithMovedTouch: function (movedTouch, event) {
var minDistSqr = 300000;
var nearestTouch = null;
var target = event.getCurrentTarget();
var newLocation = movedTouch.getLocationInView();
for (var i in this._touches) {
var location = this._touches[i];
if (i != movedTouch.getID()) {
var distSqr = this.sqrOfDistanceBetweenPoints(location, newLocation);
this.thresHold.push(distSqr);
if (distSqr < minDistSqr) {
minDistSqr = distSqr;
nearestTouch = location;
}
}
}
if (nearestTouch) {
var prevDistSqr = this.sqrOfDistanceBetweenPoints(nearestTouch, movedTouch.getPreviousLocationInView());
var pinchDiff = Math.sqrt(minDistSqr) - Math.sqrt(prevDistSqr);
cc.log(pinchDiff);
target.scale += pinchDiff * this.kPinchZoomCoeff;
}
},
sqrOfDistanceBetweenPoints: function (p1, p2) {
return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
}
});
cc.eventManager.addListener(listener, this);
答案 2 :(得分:1)
这似乎是similar to this post。我在那里给了一个(希望完整的)答案。请看一下。