gpt4 book ai didi

c++ - Sprite 不会在 SFML 中移动

转载 作者:行者123 更新时间:2023-11-30 05:16:45 25 4
gpt4 key购买 nike

我正在使用 SFML 处理我的第一个小项目。我制作了一个在屏幕上弹跳的球(射弹)和一个可移动的播放器。然而,当我试图为我的对象创建一个对象处理程序时,问题就出现了。在让 handler 控制对象的绘制和更新后,玩家和球就停止不动了。我的问题是为什么?球:

#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML\Graphics.hpp>
using namespace std;

class Projectile : public sf::Drawable {

private:
sf::Texture texture;
sf::Sprite sprite;
public:
Projectile();
virtual ~Projectile();
sf::Sprite getSprite() const;
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
};

#endif //PROJECTILE_H

#include "Projectile.h"
#include <iostream>

Projectile::Projectile() {

if (!this->texture.loadFromFile("../Resources/projectile.png")) {
cout << "Error! Projectile sprite could not be loaded!" << endl;
}
this->sprite.setPosition(sf::Vector2f(0.0f, 0.0f));
this->sprite.setTexture(this->texture);
sf::FloatRect boundingBoxProjectile = this->sprite.getGlobalBounds();
}

Projectile::~Projectile() {
}

sf::Sprite Projectile::getSprite() const{
return this->sprite;
}

void Projectile::draw(sf::RenderTarget & target, sf::RenderStates states) const {
target.draw(sprite, states);
}

处理程序:

#ifndef OBJECTHANDLER_H
#define OBJECTHANDLER_H
#include "Block.h"
#include "Player.h"
#include "Projectile.h"
using namespace std;

class ObjectHandler : public sf::Drawable {

private:
Player player;
Block block;
Projectile projectile;
int hitX = 0;
int hitY = 0;
float checkX;
float checkY;
float keyFrameDuration = 0.0f;
float speed = 500.0f;
public:
ObjectHandler();
virtual ~ObjectHandler();
void updateProjectile(float dt);
void updatePlayer(float dt);
void updateBlock(float dt);
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
};

#endif OBJECTHANDLER_H

#include "ObjectHandler.h"

ObjectHandler::ObjectHandler() {
this->projectile = projectile;
this->block = block;
this->player = player;
}

ObjectHandler::~ObjectHandler() {
}

void ObjectHandler::updateProjectile(float dt) {
sf::Vector2f direction;

if (hitX == 0) {
direction = sf::Vector2f(0.5f, checkY);
checkX = 0.5f;
if (this->projectile.getSprite().getPosition().x >= 700) {
hitX = 1;
}
}
else if (hitX == 1) {
direction = sf::Vector2f(-0.5f, checkY);
checkX = -0.5f;
if (this->projectile.getSprite().getPosition().x <= 0) {
hitX = 0;
}
}

if (hitY == 0) {
direction = sf::Vector2f(checkX, 0.5f);
checkY = 0.5f;
if (this->projectile.getSprite().getPosition().y >= 460) {
hitY = 1;
}
}
else if (hitY == 1) {
direction = sf::Vector2f(checkX, -0.5f);
checkY = -0.5f;
if (this->projectile.getSprite().getPosition().y <= 0) {
hitY = 0;
}
}

this->projectile.getSprite().move(direction * speed * dt);
}

void ObjectHandler::draw(sf::RenderTarget & target, sf::RenderStates states) const {
this->block.draw(target, states);
this->projectile.draw(target, states);
this->player.draw(target, states);
}

和游戏:

#ifndef GAME_H
#define GAME_H

#include <SFML\Graphics.hpp>
#include "ObjectHandler.h"

class Game : public sf::Drawable {
private:
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
sf::Texture backgroundTexture;
sf::Sprite backgroundSprite;
ObjectHandler object;
public:
Game();
virtual ~Game();
void update(float dt);
};

#endif // GAME_H

#include "Game.h"
#include <iostream>
using namespace std;

void Game::draw(sf::RenderTarget & target, sf::RenderStates states) const {
target.draw(backgroundSprite, states);
target.draw(this->object, states);
}

Game::Game() {
if (!backgroundTexture.loadFromFile("../Resources/levelOne.jpg")) {
cout << "The background could not be loaded!" << endl;
}
backgroundSprite.setTexture(backgroundTexture);
}

Game::~Game() {
}

void Game::update(float dt) {
this->object.updatePlayer(dt);
this->object.updateProjectile(dt);
this->object.updateBlock(dt);
}

最佳答案

问题是您的 sf::Sprite Projectile::getSprite()const 返回实际 Sprite 的拷贝。因此 projectile.getSprite().move(...) 被拷贝调用。

为您的 Projectile 创建一个方法 move:

void Projectile::move(const sf::Vector2f& amount)
{
sprite.move(amount);
}

并在 ObjectHandler 中调用它:projectile.move(...)

您可能还想在 getSprite 中返回对 Sprite 的常量引用以避免多次复制:const sf::Sprite& Projectile::getSprite()const。当您尝试通过调用 projectile.getSprite().move(...) 修改它时,它会导致错误,您会更快地看到错误。

关于c++ - Sprite 不会在 SFML 中移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489020/

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