0
if(GetKeyState(VK_SPACE) && Player1.canFire == true)
{
    Player1.canFire = false;
    Player1.hasFired = true;
    Bullet Bullet1((Player1.Xpos + (Player1.Skin->GetWidth() / 2)), 
                    Player1.Ypos - ProjectileSkin->GetHeight() );
}
if(Player1.hasFired = true)
{
    Bullet1.Projectile->CopyTo(m_Screen, Bullet1.m_xPos, Bullet1.m_yPos);
    Bullet1.m_yPos -= 5;
}

在我说的地方Bullet1.Projectile->CopyTo,我得到了Bullet1未定义的错误,但我已经在上面的 if 块中实例化了它。我究竟做错了什么?

4

3 回答 3

2

当它到达第 9 行时,您Bullet1的内容超出了范围。您需要将Bullet1变量声明移到if语句之外。

但是,如果您无论如何都需要初始化,我根本不明白放置第二个if语句的意义。Bullet1所以,

if(GetKeyState(VK_SPACE) && Player1.canFire == true)
{
    Player1.canFire = false;
    Player1.hasFired = true;
    Bullet Bullet1((Player1.Xpos + (Player1.Skin->GetWidth() / 2)), 
                Player1.Ypos - ProjectileSkin->GetHeight() );
    Bullet1.Projectile->CopyTo(m_Screen, Bullet1.m_xPos, Bullet1.m_yPos);
    Bullet1.m_yPos -= 5;
}
于 2012-09-20T18:04:18.850 回答
0
Bullet Bullet1((Player1.Xpos + (Player1.Skin->GetWidth() / 2)), 
                    Player1.Ypos - ProjectileSkin->GetHeight() );

你的子弹是一个局部变量。它将以 } 结尾。可能的解决方案是不在本地创建。或将其作为参数发送给两个函数。

这里还有一个严重的错误:

如果(Player1.hasFired = true)

应该是==

于 2012-09-20T18:04:35.017 回答
0

的范围Bullet1仅在IF块内。如果您也想在其他IF块中使用它,请在外部定义它,如下所示:

Bullet Bullet1((Player1.Xpos + (Player1.Skin->GetWidth() / 2)), 
                Player1.Ypos - ProjectileSkin->GetHeight() );
if(GetKeyState(VK_SPACE) && Player1.canFire == true)
{
    Player1.canFire = false;
    Player1.hasFired = true;
}
if(Player1.hasFired = true)
{
    Bullet1.Projectile->CopyTo(m_Screen, Bullet1.m_xPos, Bullet1.m_yPos);
    Bullet1.m_yPos -= 5;
}
于 2012-09-20T18:04:50.247 回答