C++ SFML在另一个类中绘制移动的精灵
我是SFML的新手,我正在尝试制作一个滚动平面游戏,每当我按下空格键时,它就会射出一颗子弹。我已经完成了所有的移动,但我正在尝试找出如何将项目符号放入另一个类文件中,以便以后可以拥有一整套项目符号(即create.bullet()、create.missle()等)。这是我现在拥有的代码C++ SFML在另一个类中绘制移动的精灵,c++,function,class,main,sfml,C++,Function,Class,Main,Sfml,我是SFML的新手,我正在尝试制作一个滚动平面游戏,每当我按下空格键时,它就会射出一颗子弹。我已经完成了所有的移动,但我正在尝试找出如何将项目符号放入另一个类文件中,以便以后可以拥有一整套项目符号(即create.bullet()、create.missle()等)。这是我现在拥有的代码 void create::bullet(int xPos, int yPos) { sf::Texture bulletTexture; if(!bulletTexture.loadFromFile("bull
void create::bullet(int xPos, int yPos)
{
sf::Texture bulletTexture;
if(!bulletTexture.loadFromFile("bullet.png"))
cout << "There was an error loading the bullet texture file." << endl;
sf::Sprite bulletSprite;
bulletSprite.setTexture(bulletTexture);
bulletSprite.setPosition(xPos, yPos);
window.draw(bulletSprite);
}
void create::bullet(intxpos,intypos)
{
sf::纹理和纹理;
如果(!bulletTexture.loadFromFile(“bullet.png”))
cout首先,从文件加载纹理很慢。你应该在程序或关卡启动时执行一次,然后将纹理存储到某个地方
创建一个Bullet类,而不是创建一个Create类。然后,你可以拥有一个子弹向量(或指向它们的指针/智能指针)。每次你想要一个新子弹,你只需将它推回到向量。如果需要销毁一个子弹,你就可以将其擦除。然后,对于游戏本身,你需要为每个子弹调用Move(),然后再绘制()对于向量中的每一个项目符号。完成后,可以添加碰撞检测等等
您还可以选择-每个子弹都可以有自己的sf::sprite并对其进行修改,或者您可以为每个游戏精灵拥有一个sf::sprite并为每个子弹重新定位它。
就我个人而言,我使用的是第二种方法。我的Bullet类如下所示:
Bullet::Bullet(std::string ntype, double nx, double ny, double nvx, double nvy):
type(ntype), x(nx), y(ny), vx(nvx), vy(nvy)
{
angle=atan2(vy, vx)*(180/M_PI);
}
void Bullet::Move()
{
x+=vx;
y+=vy;
};
void Bullet::Draw()
{
DrawSprite(type, x, y, angle+90);
};
void DrawSprite(std::string type, float x, float y, float angle)
{
sf::Sprite temp=sprites[type];
temp.setRotation(angle);
temp.setPosition(x, y);
window.draw(temp);
}
在单独的.cpp文件中,我有一个sf::sprites的字符串顺序映射
Bullet::Bullet(std::string ntype, double nx, double ny, double nvx, double nvy):
type(ntype), x(nx), y(ny), vx(nvx), vy(nvy)
{
angle=atan2(vy, vx)*(180/M_PI);
}
void Bullet::Move()
{
x+=vx;
y+=vy;
};
void Bullet::Draw()
{
DrawSprite(type, x, y, angle+90);
};
void DrawSprite(std::string type, float x, float y, float angle)
{
sf::Sprite temp=sprites[type];
temp.setRotation(angle);
temp.setPosition(x, y);
window.draw(temp);
}
首先,从文件加载纹理很慢。你应该在程序或关卡启动时执行一次,然后将纹理存储在某个地方
创建一个Bullet类,而不是创建一个Create类。然后,你可以拥有一个子弹向量(或指向它们的指针/智能指针)。每次你想要一个新子弹,你只需将它推回到向量。如果需要销毁一个子弹,你就可以将其擦除。然后,对于游戏本身,你需要为每个子弹调用Move(),然后再绘制()对于向量中的每一个项目符号。完成后,可以添加碰撞检测等等
您还可以选择-每个子弹都可以有自己的sf::sprite并对其进行修改,或者您可以为每个游戏精灵拥有一个sf::sprite并为每个子弹重新定位它。
就我个人而言,我使用的是第二种方法。我的Bullet类如下所示:
Bullet::Bullet(std::string ntype, double nx, double ny, double nvx, double nvy):
type(ntype), x(nx), y(ny), vx(nvx), vy(nvy)
{
angle=atan2(vy, vx)*(180/M_PI);
}
void Bullet::Move()
{
x+=vx;
y+=vy;
};
void Bullet::Draw()
{
DrawSprite(type, x, y, angle+90);
};
void DrawSprite(std::string type, float x, float y, float angle)
{
sf::Sprite temp=sprites[type];
temp.setRotation(angle);
temp.setPosition(x, y);
window.draw(temp);
}
在单独的.cpp文件中,我有一个sf::sprites的字符串顺序映射
Bullet::Bullet(std::string ntype, double nx, double ny, double nvx, double nvy):
type(ntype), x(nx), y(ny), vx(nvx), vy(nvy)
{
angle=atan2(vy, vx)*(180/M_PI);
}
void Bullet::Move()
{
x+=vx;
y+=vy;
};
void Bullet::Draw()
{
DrawSprite(type, x, y, angle+90);
};
void DrawSprite(std::string type, float x, float y, float angle)
{
sf::Sprite temp=sprites[type];
temp.setRotation(angle);
temp.setPosition(x, y);
window.draw(temp);
}