我正在尝试创建一个游戏,但我的功能遇到了一些问题。
在包含以下内容的行上: 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;
}