克隆对象任意次数
Cloning objects an arbitrary number of times
我是一名使用SDL2的C++初学者。这是我的问题。
我尝试任意多次克隆同一类的对象,而不必为每个新对象指定特定的名称。
我定义了一个类Enemy1:
class Enemy1
{
public:
//The dimensions of the enemy
static const int Enemy1_WIDTH = 20;
static const int Enemy1_HEIGHT = 20;
//Maximum axis velocity of the dot
static const int Enemy1_VEL = 10;
//Initializes the variables
Enemy1();
//Moves the enemy
virtual void move();
//Shows the enemy on the screen
virtual void render();
private:
//The X and Y offsets of the enemy
int mPosX, mPosY;
//The velocity of the enemy
int mVelX, mVelY;
};
我定义了类中的所有函数,例如:
Enemy1::Enemy1()
{
//Initialize the offsets
mPosX = 320;
mPosY = 240;
//Initialize the velocity
mVelX = 5;
mVelY = 5;
}
然后在我的主循环中:
//Make first enemy
Enemy1 enemy1;
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
//Make more enemies
if (SDL_GetTicks() > spawnTime)
{
Enemy1 **arbitrary_name_of_copied_enemy**
spawnTime = spawnTime + spawnTimeInterval;
}
}
我该如何做到这一点而不必说出每个新敌人的名字?这个问题通常是如何处理的?我研究过复制构造函数和克隆,但它们似乎并不能解决这个问题。
如果使用复制构造函数并将其存储在向量或数组中,那么vector.push_back(new Enemy(orig))之类的东西就可以了。
相关文章:
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 当要访问的对象被多次封装时,如何正确使用setter
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- OpenGL - 在任意轴上平移对象
- C++析构函数调用两次,堆栈分配的复合对象
- 循环中本地对象的析构函数是否保证在下一次迭代之前被调用?
- 将任意对象附加到数组有什么问题
- 我们可以在c++中多次创建相同名称的对象吗
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 每 200 帧替换一次对象
- C++两次从文件保存对象读取多重继承
- 在条件运算符中使用对象两次会产生 UB 吗?
- 我应该只在免费商店上为一次调用创建对象吗?
- 我可以确保在编译时只创建一次对象吗?
- 将任意函数对象存储到类成员容器中,而无需知道其声明签名
- 使用 lambda 和可变参数模板延迟初始化对象 - 任意传递值
- 我如何仅一次或一次创建一个对象
- 在循环中声明的对象是否只构造一次
- 每隔几秒钟移动一次对象
- 使用char数组来容纳任意大小对象的数组,对齐问题