Please help, I can not rewrite this code to objective-c
on 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;
}
I just started learning cocos2d-x and therefore difficult to rewrite) I would be very grateful if someone can help rewrite this code to cocos2d-x
CCSet in cocos2d-x don't have methods to join two sets or remove objects from one set in another. You can use touch delegate instead of standard delegate and use methods:
virtual bool ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent);
virtual bool ccTouchMoved (CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded (CCTouch *pTouch, CCEvent *pEvent);
Then you can add or remove every single touch from or to _touches.
If you want use touch delegate you must override registerWithTouchDispatcher();
void Strona::registerWithTouchDispatcher() {
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, this->getTouchPriority(), true);
}
Or you can simply iterate in every touch in touches from ccTouchesBegan and add every touch from touches to _touches.