2

我是 C++ 和 cocos2d-x 的新手,所以我不明白为什么会出错。代码

void
MainLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    // Get any touch and convert the touch position to in-game position.
    CCTouch* touch = (CCTouch*)pTouches->anyObject();
    CCPoint position = touch->locationInView();
    position = CCDirector::sharedDirector()->convertToGL(position);

   __pShip->setMove(position);
}

这是功能代码。

Ship::setMove(CCPoint *newPosition)
{
    __move=*newPosition;
}

如您所见,它使用CCPoint类型作为参数,但它与位置 Header 失败:

class Ship : public AnimatedObject
{
public:
    Ship();
    bool init(const char* frameName, CCSpriteBatchNode* pSpriteSheet);
    void setMove(CCPoint* newPosition);
    void move();

 private:
     /**
     * A CCMoveTo action set in the move() method.
     */
    cocos2d::CCFiniteTimeAction* __pMoveAction;

    /**
    * This value specifies the ship's speed.
    */
    float   __speed;

     /**
     * This value specifies position to which the ship should move.
     * It's set in touch events callbacks in MainLayer class.
     */
    CCPoint  __move;    
};

我究竟做错了什么?为什么此代码无法转换 CCPoints?

4

1 回答 1

1

采用:

__pShip->setMove(&position);  // Address-of

或者改变函数本身:

Ship::setMove(CCPoint newPosition) // better: const CCPoint& newPosition
{
    __move = newPosition;
}

如果CCPoint是一个小类,按值使用(如图所示),或者如果它更大(复制成本高),则使用注释原型。

于 2012-11-28T09:03:51.877 回答