2012-11-28 25 views
2

我是C++新手和Cocos2d-X,所以我不明白爲什麼這是錯的。 代碼無法轉換「的cocos2d :: CCPoint」到「的cocos2d :: CCPoint」

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類型作爲參數,但它失敗位置 標題:

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?

+0

您能否提一下,錯誤的確切位置? – Ajay

+0

__pShip-> setMove(position); – Sugar

+0

指針或引用是不是很糟糕? – Sugar

回答

1

用途:

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

或改變功能本身:

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

如果CCPoint是一個小班,使用的值(如圖所示),或者如果它是更大(副本昂貴),使用評論的原型。