【发布时间】:2013-12-24 21:47:07
【问题描述】:
请帮忙,我无法在C++ 上将此代码重写为objective-c
cocos2d (objective-c):
- (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);
scal1+= pinchDiff * kPinchZoomCoeff;
if (scal1>=1 &&(scal1<=3)) {
self.scale += pinchDiff * kPinchZoomCoeff; // kPinchZoomCoeff is constant = 1.0 / 200.0f Adjust it for your needs
scal1=self.scale;
}
scal1=self.scale;
NSLog(@"Scale %f",self.scale);
}
}
CGFloat sqrOfDistanceBetweenPoints(CGPoint p1, CGPoint p2)
{
CGPoint diff = ccpSub(p1, p2);
return diff.x * diff.x + diff.y * diff.y;
}
我刚开始学习cocos2d-x,因此很难重写) 如果有人可以帮助将这段代码重写为 cocos2d-x,我将不胜感激
【问题讨论】:
-
你用的是什么cocos2d-x版本,你试过什么?
-
cocos2d-x-2.2.1 比如不太明白这里怎么写:[_touches unionSet: touches];
标签: c++ objective-c cocos2d-iphone cocos2d-x translate