0

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

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

1 回答 1

0

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

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


void playerMoveTest(sf:Sprite& player)
{
 player.Move(50,50); 
}
于 2012-11-12T16:58:54.703 回答