Noobie在这里。 我正在尝试跨多个类更改Player对象mainCharacter 。 我目前有一个Player对象,声明如下所示。 Player可以传送到各种世界并与怪物战斗。
所有这些代码的作品。 一旦一个世界的敌人被击败,他们就会被击败。 我的问题是,当他传送到另一个世界时, Player的统计数据全部重置为默认值; 即使在前世界遭受敌人伤害之后,他仍然拥有充分的生命值。
如何跨多个类或世界对同一个Player对象进行更改? 我认为我的声明存在问题,但我不确定。 我很欣赏任何输入。 谢谢!
在声明mainCharacter对象的地方:
class SpaceList { protected: class SpaceNode { friend class SpaceList; Player mainCharacter; Space* thisSpace; SpaceNode* next; SpaceNode(int m, SpaceNode* next1 = NULL) { if(m == 0) { thisSpace = new EntranceHall(&mainCharacter); } else if(m == 1) { thisSpace = new WaterSpace(&mainCharacter); }Player.hpp :
class Player: public Interactable { protected: Backpack myBackpack; public: Player(); virtual interactableType getInteractableType(); virtual int interact(); virtual int attack(); virtual void defend(int);Player.cpp :
Player::Player() { healthPoints = 10; numberOfAttackDice = 1; sidesOfAttackDice = 6; numberOfDefendDice = 1; sidesOfDefendDice = 6; }mainCharacter从Entrance ( Entrance.cpp )开始:
EntranceHall::EntranceHall(Interactable* mainCharacter) { interactableGrid[6][3] = mainCharacter; interactableGrid[0][3] = new Portal(0);//entrance portal interactableGrid[3][3] = new InterestPoint(0);//stone mural }mainCharacter可能后来传送到水世界,默认值重置( Waterspace.cpp ):
WaterSpace::WaterSpace(Interactable* mainCharacter) { interactableGrid[3][0] = mainCharacter; interactableGrid[3][3] = new Boss(this->getSpaceType());Noobie here. I'm trying to make changes to the Player object mainCharacter across multiple classes. I currently have a Player object declared as seen below. The Player is able to teleport to various worlds and fight monsters.
All of that code works. Once the enemy of one world is defeated, they stay defeated. My problem is that when he teleports to another world, the Player's stats are all reset to their default values; he has full life points again even after sustaining damage from the enemy in the previous world.
How do I make changes to the same Player object across multiple classes, or worlds? I figure there's a problem in my declarations but I'm not sure. I appreciate any input. Thanks!
Where the mainCharacter object is declared:
class SpaceList { protected: class SpaceNode { friend class SpaceList; Player mainCharacter; Space* thisSpace; SpaceNode* next; SpaceNode(int m, SpaceNode* next1 = NULL) { if(m == 0) { thisSpace = new EntranceHall(&mainCharacter); } else if(m == 1) { thisSpace = new WaterSpace(&mainCharacter); }Part of Player.hpp:
class Player: public Interactable { protected: Backpack myBackpack; public: Player(); virtual interactableType getInteractableType(); virtual int interact(); virtual int attack(); virtual void defend(int);Part of Player.cpp:
Player::Player() { healthPoints = 10; numberOfAttackDice = 1; sidesOfAttackDice = 6; numberOfDefendDice = 1; sidesOfDefendDice = 6; }mainCharacter starts off at Entrance (Entrance.cpp):
EntranceHall::EntranceHall(Interactable* mainCharacter) { interactableGrid[6][3] = mainCharacter; interactableGrid[0][3] = new Portal(0);//entrance portal interactableGrid[3][3] = new InterestPoint(0);//stone mural }mainCharacter may later teleport to Water World, default values reset (Waterspace.cpp):
WaterSpace::WaterSpace(Interactable* mainCharacter) { interactableGrid[3][0] = mainCharacter; interactableGrid[3][3] = new Boss(this->getSpaceType());最满意答案
删除SpaceNode和Player之间的has-a关系 - 在外部某个地方创建一个Player实例,并使用一个指针来引用它,就像你习惯的那样。 或者只是将其SpaceNode static ,以便只有一个实例不会被重构(或者对每个SpaceNode单独SpaceNode )。
笔记:
不要自己实现链表,这个数据结构甚至不适合这里。 尝试std::vector 。
更好地切换到智能指针。 你甚至可能在不知情的情况下泄漏记忆。
Remove the has-a relationship between SpaceNode and Player - create an instance of Player somewhere outside and use a pointer to refer to it, like you're used to. Or just make it static, so that there's only one instance that does not get reconstructed (or rather constructed separately for each SpaceNode).
Notes:
Don't implement linked lists yourself, this data structure does not even fit here. Try std::vector.
Better switch to smart pointers. You might be leaking memory without even knowing it.
c ++如何跨多个类对同一对象进行更改?(c++ How to make changes to the same object across multiple classes?)Noobie在这里。 我正在尝试跨多个类更改Player对象mainCharacter 。 我目前有一个Player对象,声明如下所示。 Player可以传送到各种世界并与怪物战斗。
所有这些代码的作品。 一旦一个世界的敌人被击败,他们就会被击败。 我的问题是,当他传送到另一个世界时, Player的统计数据全部重置为默认值; 即使在前世界遭受敌人伤害之后,他仍然拥有充分的生命值。
如何跨多个类或世界对同一个Player对象进行更改? 我认为我的声明存在问题,但我不确定。 我很欣赏任何输入。 谢谢!
在声明mainCharacter对象的地方:
class SpaceList { protected: class SpaceNode { friend class SpaceList; Player mainCharacter; Space* thisSpace; SpaceNode* next; SpaceNode(int m, SpaceNode* next1 = NULL) { if(m == 0) { thisSpace = new EntranceHall(&mainCharacter); } else if(m == 1) { thisSpace = new WaterSpace(&mainCharacter); }Player.hpp :
class Player: public Interactable { protected: Backpack myBackpack; public: Player(); virtual interactableType getInteractableType(); virtual int interact(); virtual int attack(); virtual void defend(int);Player.cpp :
Player::Player() { healthPoints = 10; numberOfAttackDice = 1; sidesOfAttackDice = 6; numberOfDefendDice = 1; sidesOfDefendDice = 6; }mainCharacter从Entrance ( Entrance.cpp )开始:
EntranceHall::EntranceHall(Interactable* mainCharacter) { interactableGrid[6][3] = mainCharacter; interactableGrid[0][3] = new Portal(0);//entrance portal interactableGrid[3][3] = new InterestPoint(0);//stone mural }mainCharacter可能后来传送到水世界,默认值重置( Waterspace.cpp ):
WaterSpace::WaterSpace(Interactable* mainCharacter) { interactableGrid[3][0] = mainCharacter; interactableGrid[3][3] = new Boss(this->getSpaceType());Noobie here. I'm trying to make changes to the Player object mainCharacter across multiple classes. I currently have a Player object declared as seen below. The Player is able to teleport to various worlds and fight monsters.
All of that code works. Once the enemy of one world is defeated, they stay defeated. My problem is that when he teleports to another world, the Player's stats are all reset to their default values; he has full life points again even after sustaining damage from the enemy in the previous world.
How do I make changes to the same Player object across multiple classes, or worlds? I figure there's a problem in my declarations but I'm not sure. I appreciate any input. Thanks!
Where the mainCharacter object is declared:
class SpaceList { protected: class SpaceNode { friend class SpaceList; Player mainCharacter; Space* thisSpace; SpaceNode* next; SpaceNode(int m, SpaceNode* next1 = NULL) { if(m == 0) { thisSpace = new EntranceHall(&mainCharacter); } else if(m == 1) { thisSpace = new WaterSpace(&mainCharacter); }Part of Player.hpp:
class Player: public Interactable { protected: Backpack myBackpack; public: Player(); virtual interactableType getInteractableType(); virtual int interact(); virtual int attack(); virtual void defend(int);Part of Player.cpp:
Player::Player() { healthPoints = 10; numberOfAttackDice = 1; sidesOfAttackDice = 6; numberOfDefendDice = 1; sidesOfDefendDice = 6; }mainCharacter starts off at Entrance (Entrance.cpp):
EntranceHall::EntranceHall(Interactable* mainCharacter) { interactableGrid[6][3] = mainCharacter; interactableGrid[0][3] = new Portal(0);//entrance portal interactableGrid[3][3] = new InterestPoint(0);//stone mural }mainCharacter may later teleport to Water World, default values reset (Waterspace.cpp):
WaterSpace::WaterSpace(Interactable* mainCharacter) { interactableGrid[3][0] = mainCharacter; interactableGrid[3][3] = new Boss(this->getSpaceType());最满意答案
删除SpaceNode和Player之间的has-a关系 - 在外部某个地方创建一个Player实例,并使用一个指针来引用它,就像你习惯的那样。 或者只是将其SpaceNode static ,以便只有一个实例不会被重构(或者对每个SpaceNode单独SpaceNode )。
笔记:
不要自己实现链表,这个数据结构甚至不适合这里。 尝试std::vector 。
更好地切换到智能指针。 你甚至可能在不知情的情况下泄漏记忆。
Remove the has-a relationship between SpaceNode and Player - create an instance of Player somewhere outside and use a pointer to refer to it, like you're used to. Or just make it static, so that there's only one instance that does not get reconstructed (or rather constructed separately for each SpaceNode).
Notes:
Don't implement linked lists yourself, this data structure does not even fit here. Try std::vector.
Better switch to smart pointers. You might be leaking memory without even knowing it.
发布评论