gpt4 book ai didi

c++,未在此范围内声明

转载 作者:太空宇宙 更新时间:2023-11-04 14:44:48 25 4
gpt4 key购买 nike

我正在尝试创建游戏,但我的功能出现了一些问题。

在包含以下内容的行中:myBeem.setBeemLocation(50, 50);我收到以下错误:未在此范围内声明 myBeem

为什么?

干杯


#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

using namespace std;

//WINDOW
sf::VideoMode VMode(800, 600, 32);
sf::RenderWindow Window(VMode, "Space Defender");


class laserbeem{

public:
laserbeem();
~laserbeem();
void setBeemLocation(float x, float y);

protected:
float beemLocationX;
float beemLocationY;

};

laserbeem::laserbeem(){
}

laserbeem::~laserbeem(){
}

void laserbeem::setBeemLocation(float x, float y){

beemLocationX = x;
beemLocationY = y;
}


int main()
{


//MUSIC
sf::Music music;
if (!music.openFromFile("music.ogg"))
std::cout << "Music failed to load!" << std::endl;
music.play();

//SOUNDS
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("laser.ogg"))
std::cout << "Laser failed to load!" << std::endl;
//return 0;

sf::Sound laser;
laser.setBuffer(buffer);


//BACKGROUND
sf::Texture Image;
if (!Image.loadFromFile("gal.jpg"))
{
std::cout << "Image failed to load!" << std::endl;
return 1;
}

sf::Sprite Sprite;
Sprite.setTexture(Image);

Sprite.setPosition(0.0f, 0.0f);
Sprite.setScale((float)VMode.width/(float)Sprite.getTexture()->getSize().x, (float)VMode.height/(float)Sprite.getTexture()->getSize().y);

//Sprite.setScale((float)VMode.width, (float)VMode.height);
//Sprite.setPosition(100.0f, 30.0f);
//Sprite.setScale(.5f, .3f);

//SHIP
sf::Texture Image2;
if (!Image2.loadFromFile("ship.png"))
return 1;

sf::Sprite Sprite2;
Sprite2.setTexture(Image2);

//float x = 400.0;
Sprite2.setPosition(400.0f, 450.0f);
Sprite2.setScale(.8f, .8f);


while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
std::cout << "The window was closed." << std::endl;
break;

case sf::Event::KeyPressed:
//LEFT KEY
if (Event.key.code == sf::Keyboard::Left)
Sprite2.move(-15,0);
//RIGHT KEY
if (Event.key.code == sf::Keyboard::Right)
Sprite2.move(15,0);
//UP KEY
if (Event.key.code == sf::Keyboard::Up)
Sprite2.move(0,-15);
//DOWN KEY
if (Event.key.code == sf::Keyboard::Down)
Sprite2.move(0,15);
//LEFT CTRL KEY
if (Event.key.code == sf::Keyboard::LControl)
laserbeem myBeem;
myBeem.setBeemLocation(50, 50);
laser.play();
break;

default:
break;
}
}

Window.clear(sf::Color(144, 144, 144));
Window.draw(Sprite);
Window.draw(Sprite2);
Window.display();
}

return 0;
}

最佳答案

以下内容:

    if (Event.key.code == sf::Keyboard::LControl)
laserbeem myBeem;
myBeem.setBeemLocation(50, 50);

相当于:

    if (Event.key.code == sf::Keyboard::LControl) {
laserbeem myBeem;
}
// myBeem no longer exists
myBeem.setBeemLocation(50, 50);

换句话说,myBeem 的范围仅限于 if 语句中的 block 。

您需要重新审视大括号在代码中的位置(以及缩进的使用,以提高可读性)。

关于c++,未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17456022/

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