sfml 中 C++ 中的对象数组

array of object in C++ in sfml

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

这是有效的代码,但我认为它优化得最少

#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 完全无关,而是更为普遍。

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

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<...> 的标准构造函数,然后再对其进行更改。

在第一个版本中,您使用 push_back 成功创建了一个包含 4 个元素的向量。

在第二个版本中,您永远不会为 4 个元素分配 space,因此当您尝试为向量的元素分配值时,这就是您的崩溃。

如果您真的不想使用 push_back,您可以使用 resize 来设置向量的大小。

这是您要执行的操作的简化版本

#include <vector>

class classBackground {
    std::vector<int> rect;
public:
    classBackground() {
        rect.resize(4);
        rect[0] = 1;
        rect[1] = 2;
        rect[2] = 3;
        rect[3] = 4;
    }
};

int main() {
    classBackground cb;
    // there, it worked.
}

演示:http://ideone.com/YYq0oA

或者 如果你有 C++11 并且你的向量总是大 4 个元素,在这种情况下看起来可能是这样,你应该考虑使用std::array 相反。

#include <array>

class classBackground {
    std::array<int, 4> rect;
public:
    classBackground() {
        rect[0] = 1;
        rect[1] = 2;
        rect[2] = 3;
        rect[3] = 4;
    }
};

int main() {
    classBackground cb;
    // there, it worked.
}

数组在大多数方面都像向量一样工作,只是不能更改大小,但它可能更有效率。