SFML community forums
Help => General => Topic started by: snowboarder6009 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
#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;
}
-
//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.