SFML 1.6 - sf :: Sprite.Move()不在main()函数之外工作

时间:2012-10-25 03:51:37

标签: debugging sfml

我在SFML 1.6中移动我的精灵已经有一段时间了。虽然我的程序中的所有变量都已正确设置,但函数sf::Sprite.Move()仅在main()函数内有效。我在下面的main.cpp文件中的函数playerMoveTest()中复制了该问题。如果有人知道这个bug的解决方案,或者愿意提供帮助,那将非常感激。

#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); 
}

1 个答案:

答案 0 :(得分:0)

player sprite仅在main中定义,因此您无法在playerMoveTest(void)中引用(它超出范围)。尝试将其作为参数传递。

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


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