【问题标题】:Rewrite Cocos2d (objective-c) on Coco2d-x (c + +) [closed]在 Coco2d-x (c++) 上重写 Cocos2d (objective-c) [关闭]
【发布时间】: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


【解决方案1】:

cocos2d-x 中的CCSet 没有加入两个集合或从另一个集合中删除对象的方法。您可以使用触摸委托代替标准委托并使用方法:

 virtual bool ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent);
virtual bool ccTouchMoved (CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded (CCTouch *pTouch, CCEvent *pEvent);

然后,您可以在 _touches 中添加或删除每个触摸。

如果你想使用触摸委托,你必须重写 registerWithTouchDispatcher();

void Strona::registerWithTouchDispatcher() {
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, this->getTouchPriority(), true);
}

或者您可以简单地在 ccTouchesBegan 中的 touches 中迭代每个 touches,并将 touches 中的每个 touches 添加到 _touches。

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多