2015-06-08 63 views
-1

我只是玩C++ SFML的東西,我有點不明白爲什麼我的代碼不工作。我想要做的事情就是讓我們說5,在窗口中使用矢量隨機放置在窗口中的正方形,但我不明白爲什麼它不起作用。C++ SFML 2.2矢量

這是主要的遊戲類:

#include "main_game.h" 
#include "main_menu.h" 

void main_game::Initialize(sf::RenderWindow* window) 
{ 
    this->Player = new player(); 
    this->Player->setOrigin(this->Player->getGlobalBounds().width/2, this->Player->getGlobalBounds().height/2); 

    this->TestObject = new testObject(); 
    this->TestObject->Initialize(); 
    this->TestObject->setOrigin(this->TestObject->getGlobalBounds().width/2, this->TestObject->getGlobalBounds().height/2); 
} 

void main_game::Update(sf::RenderWindow* window) 
{ 
    this->Player->setPosition(sf::Mouse::getPosition(*window).x, sf::Mouse::getPosition(*window).y); 
    this->Player->Update(); 

    if (this->Player->CheckCollision(TestObject)) 
    { 
     this->TestObject->setColor(sf::Color::Red); 
    } 
    else 
    { 
     this->TestObject->setColor(sf::Color::Cyan); 
    } 

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) 
    { 
     coreState.SetState(new main_menu()); 
    } 
} 

void main_game::Render(sf::RenderWindow* window, std::vector<sf::Sprite> sprites) 
{ 
    this->TestObject->Render(*window, sprites); 

    window->draw(*this->Player); 
} 

void main_game::Destroy(sf::RenderWindow* window) 
{ 
    delete this->Player; 
    delete this->TestObject; 
} 

這是testObject.h類

#pragma once 

#include "entity.h" 

class testObject : public Entity 
{ 
public: 
    testObject(); 
    void Initialize(); 
    void Render(sf::RenderWindow window, std::vector<sf::Sprite> sprites); 
    void Update(); 
private: 
    sf::RenderWindow window; 
}; 

這是testObject.cpp類

#include "testObject.h" 

testObject::testObject() 
{ 
    this->Load("testObject.png"); 
} 

void testObject::Initialize() 
{ 
    sf::Texture testObjectTexture; 
    sf::Sprite testObjectSprite; 

    testObjectTexture.loadFromFile("testObject.png"); 
    testObjectSprite.setTexture(testObjectTexture); 

    std::vector<sf::Sprite> sprites(5, sf::Sprite(testObjectSprite)); 

    srand(time(0)); 

    for (unsigned int i = 0; i < sprites.size(); i++) 
    { 
     sprites[i].setPosition(1 + (rand() % 1024 - 32), rand() % 640 - 32); 
    } 
} 

void testObject::Render(sf::RenderWindow window, std::vector<sf::Sprite> sprites) 
{ 
    for (unsigned int i = 0; i < sprites.size(); i++) 
    { 
     window.draw(sprites[i]); 
    } 
} 

void testObject::Update() 
{ 
    Entity::Update(); 
} 

該錯誤消息

1>------ Build started: Project: Blahblah, Configuration: Debug Win32 ------ 
1> testObject.cpp 
1>d:\visual studio projects\blahblah\blahblah\testobject.cpp(18): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data 
1>d:\visual studio projects\blahblah\blahblah\testobject.cpp(22): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data 
1> main_game.cpp 
1>d:\visual studio projects\blahblah\blahblah\main_game.cpp(16): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data 
1>d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\window\window.hpp(521): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable' 
1>   d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable' 
1>   d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable' 
1>   This diagnostic occurred in the compiler generated function 'sf::Window::Window(const sf::Window &)' 
1>d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\graphics\rendertarget.hpp(419): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable' 
1>   d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable' 
1>   d:\visual studio projects\blahblah\3rdpartylibs\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable' 
1>   This diagnostic occurred in the compiler generated function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget &)' 
1> Generating Code... 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

請你的問題不是*多一點的描述:「我不明白爲什麼它不工作」 * –

+0

基本上我也嘗試全新的項目,與矢量繪製多個對象,在只有main.cpp中main(),它像一個魅力。但這裏的問題可能是我不能在類和多個函數之間正確執行。 – KuziNs

+0

我寧願給你描述你的意思*「它不工作」*。這是一個編譯器錯誤?如果是這樣,那麼錯誤信息是什麼?或者它是一個運行時錯誤?如果是這樣,實際發生了什麼事情,它與預期會發生什麼有什麼不同? –

回答

0

此特定錯誤,(雖然我敢肯定,你將有更多的,一旦你解決這個問題),可能來源於此:

void testObject::Render(sf::RenderWindow window, std::vector<sf::Sprite> sprites) 

您傳遞RenderWindow的價值,但它是非可複製。改爲通過引用或指針傳遞它。

+0

好的,我解決了這個問題,但現在它不會給我任何錯誤,所以這很好,但它不會呈現任何內容。 – KuziNs

+0

@KuziNs:這是一個完全不同的問題。它屬於不同的職位。 –

+0

你的精靈沒有顯示的原因是因爲它已經被卸載,你需要創建一個常量數組/矢量/地圖來存儲紋理,然後在其他對象中設置對紋理的引用,然後設置你的精靈。 – Canvas

0

好的,我解決了這個問題,但現在它不會給我任何錯誤,所以這很好,但它不會渲染任何東西。

'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Debug\Blahblah.exe'. Symbols loaded. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Blahblah\sfml-graphics-d-2.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Blahblah\sfml-system-d-2.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'D:\Visual Studio Projects\Blahblah\Blahblah\sfml-window-d-2.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\opengl32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\glu32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ddraw.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmmbase.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dciman32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\devobj.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\nvinit.dll'. Cannot find or open the PDB file. 
'Blahblah.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Cannot find or open the PDB file. 
+1

這不是一個答案。如果你想更新它,你應該編輯你的問題。另外,您發佈的消息不會指出任何錯誤 - 他們只是告訴您系統庫沒有可用的調試符號 - 這是完全正常的。 –