Cocos2d-x V3 如何正确覆盖::create()?
Cocos2d-x V3 How to override ::create() properly?
我的玩家 class 定义如下:
"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 .. 等
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);
可以用吗?
您可以重载 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;
}
}
我的玩家 class 定义如下:
"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 .. 等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);
可以用吗?
您可以重载 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;
}
}