Cocos2d-x V3如何正确覆盖:: create()?

时间:2016-07-05 05:31:16

标签: c++ cocos2d-x cocos2d-x-3.0

我的Player类定义如下:

" Player.h"

#ifndef PLAYER_h
#define PLAYER_h

#include "cocos2d.h"

USING_NS_CC;

class Player: public Sprite {
public:
     Player();
     ~Player();


private:
    Sprite *playerSprite;

    __String name;

    int maxHP;
    int currHP;

    int maxMP;
    int currMP;

    int maxEXP;
    int currEXP;
 };

#endif

和" Player.cpp":

#include "Player.h"

我想在

上初始化int maxHP,int maxMP .. etc
Player* myPlayer = Player::create();

使用

这样的参数覆盖:: create()函数的正确方法是什么
// create(int maxHP, int currHP, int maxMP, int currMP, int maxEXP, int currEXP)
Player* myPlayer = Player::create(100, 100, 100, 100, 100, 100); 

可以用吗?

1 个答案:

答案 0 :(得分:1)

您可以重载create(具有相同名称但签名不同的多个函数)。

class Player : public Sprite {
public:
    static Player* create(int maxHP, int currHP, int maxMP, int currMP, int maxEXP, int currEXP) {
        Player* p = Player::create();
        p->maxHP = maxHP;
        // etc ...
        return p;
    }
}