Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Error when trying to display background and image  (Read 2868 times)

0 Members and 1 Guest are viewing this topic.

snowboarder6009

  • Newbie
  • *
  • Posts: 3
    • AOL Instant Messenger - boarderwsu
    • View Profile
    • http://www.dotnetjunkies.com/WebLog/chris.taylor/archive/2006/09/30/148798.aspx
Error when trying to display background and image
« on: November 26, 2008, 03:39:55 am »
As the title says im trying to display my background and an image on top of the background but when i run it i get an error.

Debug Assertion Failed!
Program:..
File d:\programmes\visual studio 2005\vc\include\vector
Line: 756

Expression: vector subscript out of range

Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
//Windows is included in Graphics
#include <iostream>

#define PLAYER_SPEED 100

int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Tutorial");

//BackGround drawing pieces
sf::Image bgImage;
sf::Sprite bgSprite;

sf::Image plImage;
sf::Sprite plSprite;

//load image
/*if(!bgImage.LoadFromFile("background.jpg"))
{
return EXIT_FAILURE;
}

if(!plImage.LoadFromFile("terror.png"))
{
return EXIT_FAILURE;
}*/

// point out  sprite to our image
bgSprite.SetImage(bgImage);
plSprite.SetImage(plImage);

//set center
plSprite.SetCenter(plImage.GetWidth() * .5, plImage.GetHeight() * .5);

//plSprite.Setposition(App.Width * .5, App.height * .5);
plSprite.SetPosition(400, 300);

while(App.IsOpened())
{
//update
// handle player input

//get our frame time
float deltaTime = App.GetFrameTime();

if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left)
{
if(App.GetInput().GetMouseX();

//check for left key
if(App.GetInput().IsKeyDown(sf::Key::Left));
{
float newX = plSprite.GetPosition().x - (deltaTime * PLAYER_SPEED);
//Keep the image on the screen
if(newX -(.5*plImage.GetWidth()) < 0)
{
newX = .5 *plImage.GetWidth();
}
plSprite.SetPosition(newX, plSprite.GetPosition().y());
}
if(App.GetInput().IsKeyDown(sf::Key::Right));
{
float newX = plSprite.GetPosition().x + (deltaTime * PLAYER_SPEED);
if(newX < App.GetWidth())
{
newX = App.GetWidth();
}
plSprite.SetPosition(newX, plSprite.GetPosition().y());
}


if(App.GetInput().IsKeyDown(sf::Key::Up));
{
float newY = plSprite.GetPosition().y - (deltaTime * PLAYER_SPEED);
plSprite.SetPosition(plSprite.GetPosition().x, newY);
}
if(App.GetInput().IsKeyDown(sf::Key::Down));
{
float newX = plSprite.GetPosition().x + (deltaTime * PLAYER_SPEED);
plSprite.SetPosition(plSprite.GetPosition().x, newY);
}

//Check for window event(like closing)
sf::Event myEvent;

//process all events
while(App.GetEvent(myEvent))
{
if (myEvent.Type == sf::Event::Closed)
{
App.Close();
}
}
App.Draw(bgSprite);
App.Draw(plSprite);
App.Display();
}
 
    return EXIT_SUCCESS;
}



MrQuizzles

  • Newbie
  • *
  • Posts: 15
    • View Profile
Error when trying to display background and image
« Reply #1 on: November 26, 2008, 04:20:29 am »
Code: [Select]
     //check for left key
      if(App.GetInput().IsKeyDown(sf::Key::Left));
      {
         float newX = plSprite.GetPosition().x - (deltaTime * PLAYER_SPEED);
         //Keep the image on the screen
         if(newX -(.5*plImage.GetWidth()) < 0)
         {
            newX = .5 *plImage.GetWidth();
         }
         plSprite.SetPosition(newX, plSprite.GetPosition().y());
      }
      if(App.GetInput().IsKeyDown(sf::Key::Right));
      {
         float newX = plSprite.GetPosition().x + (deltaTime * PLAYER_SPEED);
         if(newX < App.GetWidth())
         {
            newX = App.GetWidth();
         }
         plSprite.SetPosition(newX, plSprite.GetPosition().y());
      }


I believe your problem is within here. You're referencing a method of sf::Vector2 called y(), but nothing of the sort exists. It shouldn't have compiled.

Change "plSprite.GetPosition().y()" to simply "plSprite.GetPosition().y" like you have with the .x ones.

 

anything