c++ - sfml 中 C++ 中的对象数组

标签 c++ vector sfml

我正在尝试在 sfml C++ 中使用 vector ,但我失败了一半,因为我无法创建由 4 个 RectangleShapes 组成的 vector :( 我跳过了一些不会导致问题的代码行

这是有效的代码,但在我看来它是最不优化的

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include <iostream>
#include "mainCharacter.h" // it consists the mainCharacter class

class classBackground{
public:
    sf::Texture texture;
    sf::Sprite image;
    sf::RectangleShape rectTopBorder,rectBotBorder,rectLeftBorder,rectRightBorder;
    std::vector<sf::RectangleShape> rect;
    classBackground(){
        texture.loadFromFile("wagon.png");
        image.setTexture(texture);
        image.setPosition(300,250);
        rectTopBorder.setSize(sf::Vector2f(230,5));
        rectTopBorder.setPosition(image.getPosition());
        rectTopBorder.move(0,60);
        rectBotBorder.setSize(sf::Vector2f(230,5));
        rectBotBorder.setPosition(image.getPosition());
        rectBotBorder.move(0,225);
        rectLeftBorder.setSize(sf::Vector2f(5,170));
        rectLeftBorder.setPosition(image.getPosition());
        rectLeftBorder.move(0,60);
        rectRightBorder.setSize(sf::Vector2f(5,170));
        rectRightBorder.setPosition(image.getPosition());
        rectRightBorder.move(225,60);
        rect.push_back(rectTopBorder);
        rect.push_back(rectBotBorder);
        rect.push_back(rectLeftBorder);
        rect.push_back(rectRightBorder);
    }
};

//in the main function
classMainCharacter mainCharacter; ///another class which has collision detection
classBackground background;
///in the loop
    for(int i = 0; i<=3; i++){
        mainCharacter.collision(background.rect[i]); ///colision is a function in the mainCharacter class (detects collision)
    }

这是我想要工作的代码,但我的游戏很糟糕:(

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include <iostream>
#include "mainCharacter.h" // it consists the mainCharacter class

class classBackground{
public:
    sf::Texture texture;
    sf::Sprite image;
    std::vector<sf::RectangleShape> rect; // it doesn't work if I write rect(4) or rect = {rect1,rect2,rect3,rect3} or even rect(4)={rect1,....}
    classBackground(){
        texture.loadFromFile("wagon.png");
        image.setTexture(texture);
        image.setPosition(300,250);
        rect[0].setSize(sf::Vector2f(230,5));
        rect[0].setPosition(image.getPosition());
        rect[0].move(0,60);
        rect[1].setSize(sf::Vector2f(230,5));
        rect[1].setPosition(image.getPosition());
        rect[1].move(0,225);
        rect[2].setSize(sf::Vector2f(5,170));
        rect[2].setPosition(image.getPosition());
        rect[2].move(0,60);
        rect[3].setSize(sf::Vector2f(5,170));
        rect[3].setPosition(image.getPosition());
        rect[3].move(225,60);
    }
};

//in the main function
classMainCharacter mainCharacter; ///another class which has collision detection
classBackground background;
///in the loop
    for(int i = 0; i<=3; i++){
        mainCharacter.collision(background.rect[i]); ///colision is a function in the mainCharacter class (detects collision)
    }

我做错了什么? :( 我试图在 vector 之前定义 rect,然后插入到 vector 中,但它也不起作用(这样 vector<...> rect = {......})。

最佳答案

好吧,即使我不希望这两个版本之间有太大差异,但无论如何,一个答案还是值得一提的,因为您的问题与性能和 sfml 完全无关,而是更普遍。

您尝试的是在其声明中实例化一个类成员,这是不可能的。但是你可以做的是以下方法之一:

1) 像这样使用带有 C++ 11 统一初始化语法的默认成员初始化器(这可能只是 (2) 在此之前的语法糖:http://en.cppreference.com/w/cpp/language/data_members#Member_initialization):

std::vector<sf::RectangleShape> rect{4}

2) 在构造函数中使用初始化列表:

classBackground() : rect(4) {...}

3)或者在构造函数中调用resize方法:

classBackground(){
    rect.resize(4);
    ...
}

最后一个可能是最糟糕的,因为它首先调用 vector<...> 的标准构造函数,然后再对其进行更改。

关于c++ - sfml 中 C++ 中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510266/

相关文章:

c++ - 当我将字符串转换为 vector<byte> 时出现字符串迭代器不兼容错误

c++ - 在 vector 上使用删除时发生段错误

c++从高精度 double 获取E?

C++ 可变参数模板 std::function 到 lambda 转换不起作用

c++ - 如何将 std::string 复制到 std::vector<char> 中?

c++ - 如何计算法 vector ?

c++ - 如何使用 XCode IDE 通过 SFML 中的相对路径加载图像

c++ - sf::Texture 作为类成员不起作用?

c++ - 使用普通make文件时如何实现cleanall

c# - 在 C# 中使用 COM dll