gpt4 book ai didi

c++ - 我正在尝试用 vector 拍摄

转载 作者:行者123 更新时间:2023-11-30 04:05:22 27 4
gpt4 key购买 nike

我目前正在使用 direct X 用 c++ 编写游戏,目前有一个 vector 来存储绘制我所有的 Sprite 。我无法让子弹在 vector 中工作。子弹射了,但应该有 50 颗却只射出一颗

//this vector also contains all other sprites i have like my player and my score and other sprites 
///Draw is a class

vector<Draw*> chap;
vector<Draw*>::iterator it;
Draw *bullet = NULL;

///initialization of stuff
for (int i = 0; i < gt; i++)
{
bullet = new Draw[gt];
//this sets the x position and y position and other properties
bullet[i].setDraw(blet,0,0,.1,.1,0,64,64,1,0,1,color,0,0,90,0,0,false);
chap.push_back(&bullet[i]);
}
//end initialization

///game loop
for (int i = 0; i < bell; i++)
{
for (it = chap.begin(); it != chap.end(); it++)
{
(*it)->testShoot(&bullet[i], ME);
ME->playerMove();
monster1->move();
}
}
///end loop there is other stuff in loop but doesn't effect anything

/// what i'm using
void Draw::testShoot(Draw *sprite, Draw *sprite1)
{
if ((int)timeGetTime() < timer + 100)return;
timer = timeGetTime();

for (int i = 0; i < 50; i++)
{
if (Key_Down(DIK_SPACE))
{
if (!sprite[i].alive)
{
sprite[i].x = sprite1->x + sprite1->width / 4;
sprite[i].y = sprite1->y + sprite1->height / 4;
sprite[i].velx = 1;
sprite[i].vely = 0;
sprite[i].alive = true;
break;
}
}
}
}

最佳答案

bullet = new Draw[gt]; 应该在 for 循环之前,而不是在其中。现在,您在循环的每次迭代中都创建了一个新数组,它只有一个填充元素,并且在每次覆盖 bullet 中的值时都会丢失之前创建的数组。

换句话说,这:

for (int i = 0; i < gt; i++)
{
bullet = new Draw[gt];
bullet[i].setDraw( ...

应该是这样的:

bullet = new Draw[gt];
for (int i = 0; i < gt; i++)
{
bullet[i].setDraw( ...

关于c++ - 我正在尝试用 vector 拍摄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23319174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com