Search code examples
visual-c++cocos2d-x

Unable to convert "cocos2d::CCPoint" to "cocos2d::CCPoint"


I am newbie in C++ and cocos2d-x, so i do not understand why it's wrong. Code

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);
}

This is code of function.

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

As you can see it uses CCPoint type as parameter, but it fails with position 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;    
};

What am I doing wrong? Why this code fails to converting CCPoints?


Solution

  • Use:

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

    Or change the function itself:

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

    If CCPoint is a small class, use by value (as shown), or if it is larger (copy is expensive), use the commented prototype.