2012-10-25 30 views
0

我一直在SFML 1.6中移动我的精灵有一段时间了。尽管我的程序中的所有变量都已正确设置,但函数sf::Sprite.Move()仅在main()函数中有效。我已经在下面的main.cpp文件中的功能playerMoveTest()中复制了该问题。如果有人知道这个错误的解决方案,或愿意帮助,那将是非常感谢。SFML 1.6 - sf :: Sprite.Move()不能在main()函数之外工作

#include "main.h" 
#include "collision.cpp" 
#include "events.cpp" 
#include "animation.cpp" 
//#include "hud_pls_mnu.cpp" TODO Put Back in 
#include "physics.cpp" 

#define FPS 60 //Define frames per second for the program to run on 

#define WIN_DEM_X 800 
#define WIN_DEM_Y 600 

#define MNU_OFF 0 
#define MNU_MAIN 1 
#define MNU_PAUSE 2 
#define MNU_INV 3 
int menu=MNU_OFF; 

void playerMoveTest(void); 

int main() 
{ 
//Open a window 
sf::RenderWindow App(sf::VideoMode(WIN_DEM_X, WIN_DEM_Y), "Crispy Bacon"); 

bool firstframe=true; 

//Initialize the player 
if(!player_data.image.LoadFromFile("spritesheet.png")) 
    return EXIT_FAILURE; 
sf::Sprite player(player_data.image); 
player.SetPosition(10, 10); 
player_data.time_in_cycle.Reset(); 
player.Resize(300,300); //Resize "player" to be smaller than the window 

//Initialize an object 
if(!cursor_data.image.LoadFromFile("cursor.png")) 
    return EXIT_FAILURE; 
sf::Sprite cursor(cursor_data.image); 
cursor.SetPosition(400, 300); 
cursor_data.time_in_cycle.Reset(); 
cursor.Resize(50, 50); 

//NOTE Testing stuff here 
sf::Vector2f playerSize=player.GetSize(); 
sf::Vector2f objectSize=cursor.GetSize(); 
printf("Player size:\nX:%f\nY:%f\nObject Size:\nX:%f\nY:%f\n", playerSize.x, playerSize.y, objectSize.x, objectSize.y); 

App.SetFramerateLimit(60);//Set a FPS cap 

//Draw screen border.(Room 101) 
sf::Shape rm_101; 
rm_101.AddPoint(5, 5,      sf::Color(255, 0, 0),  sf::Color(0, 0, 0)); 
rm_101.AddPoint(5, WIN_DEM_Y-5,   sf::Color(255, 0, 0),  sf::Color(0, 0, 0)); 
rm_101.AddPoint(WIN_DEM_X-5, WIN_DEM_Y-5, sf::Color(255, 0, 0),  sf::Color(0, 0, 0)); 
rm_101.AddPoint(WIN_DEM_X-5, 5,   sf::Color(255, 0, 0),  sf::Color(0, 0, 0)); 
rm_101.SetOutlineWidth(20); 
rm_101.EnableFill(false); 

// Define a text string for version number 
sf::String version_num("Pre-Alpha v0.0.0"); 
version_num.SetPosition(10.f, 550.f); 
version_num.SetColor(sf::Color::Black); 

//Create a view 
sf::View view(sf::FloatRect(0, 0, 800, 600)); 


//Start running loop 
while (App.IsOpened()) 
{ 
    // Get elapsed time 
    Frametime = App.GetFrameTime(); 

    //Process Events(i.e. the window was told to close) 
    switch(menu) 
    { 
    case MNU_OFF: 
     game_events(App); 
     break; 

    case MNU_MAIN: 
     if(firstframe==true) break; 

    } 

    //Move stuff TODO:Make so uses linked lists(in phisics file? 
    move(player, player_data); 
    playerMoveTest(); 

    //Match proper player animation frame 
    animateBody(player, player_data); 

    //Clear Screen NOTEVarious screnclears for testing 
    if(collide_rec(player, cursor)) 
    App.Clear(sf::Color(100, 100, 100)); 
    else 
    App.Clear(sf::Color(255, 255, 255)); 

    //Set View to player 
    view.SetCenter(player.GetPosition()); 

    //Display stuff 
    App.SetView(view);//Set View to normal 
    App.Draw(rm_101); 
    App.Draw(player); 
    App.Draw(cursor); 


    //After the view is set to the HUD position 
    App.SetView(App.GetDefaultView());//Set veiw to HUD position 
    App.Draw(version_num); 

    App.Display(); 

    firstframe=false; 
} 
} 

void playerMoveTest(void) 
{ 
player.Move(50,50); 
} 
+0

有几个问题:为什么你仍然使用SFML 1.6而不是2.0?你有没有尝试将你的代码剥离到展示你所看到的错误的最小样本?你的意思是'move'功能只在main中有效吗?究竟发生了什么问题? – Alex

回答

0

player精灵在main只被定义,所以你不能指在playerMoveTest(void)(它是超出范围)。尝试将它作为参数传递。

int main(){ 
.... 
playerMoveTest(player); 
.... 
} 


void playerMoveTest(sf:Sprite& player) 
{ 
player.Move(50,50); 
}