Hello!So I want to create a game which i like a 2d puzzle of moving stuff around and solving them however I have encountered a few bugs.First of all,when i execute the program and press w,a,s or d it seems like it is trying to move but it can't move.....You'll now what i mean if you copy paste the codes in your visual studio c++(BTW its VS 2015 here).The animations for going up,down,right,left whatever work correctly.HOWEVER,ignoring that it doesn't move,after it works for a few minutes it stops working giving a handler exception:-
Exception thrown at 0x00000000 in ConsoleApplication1.exe: 0xC0000005: Access violation executing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.
Here's the source code:-
#include <SFML\Graphics.hpp>
#include <iostream>
#include"Animatrix.h"
#include"move.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(1360, 720), "Larger SFML", sf::Style::Default);
sf::Texture texture;
texture.loadFromFile("bahamut.png");
texture.setRepeated(false);
float fraps = 0.0;
sf::Clock fps;
move playa(&texture, sf::Vector2u(4, 4), 1.f, window);
while (window.isOpen())
{
sf::Event event;
fraps = fps.restart().asSeconds();
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
std::cout << std::endl << "I wont let u close!MUAHAHAHAHAHA!!";
return 0;
break;
case sf::Event::Resized:
std::cout << "New height" << event.size.height << " New width" << event.size.width << std::endl;
break;
case sf::Event::TextEntered:
printf("You have pressed =%c \n", event.text.unicode);
break;
}
}
sf::Sprite potty=playa.moves(fraps, 72.f, 1, 2, 3, 0);
window.clear();
potty.move(playa.movements);
window.draw(potty);
window.display();
}
return 0;
}
Here's the header of move:-
#pragma once
#include"Animatrix.h"
#include"SFML\Graphics.hpp"
class move
{
public:
Animatrix mmovement;
sf::RenderWindow window;
sf::Vector2f movements;
move(sf::Texture* texture, sf::Vector2u num, float stime, sf::RenderWindow &window);
sf::Sprite moves(float fps,float speed, int left, int right, int up, int down);
~move();
};
Here's the definition:-
#include "move.h"
#include"Animatrix.h"
#include<iostream>
move::move(sf::Texture* texture,sf::Vector2u num,float stime,sf::RenderWindow &window):
mmovement(texture,num,stime)
{
movements.x = 0;
movements.y = 0;
window.clear();
window.display();
}
sf::Sprite move::moves(float fps,float speed,int left,int right,int up,int down)
{
sf::Sprite playas;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
{
movements.y = 0;
movements.x = -speed*fps;
mmovement.currentimage.y = left;
mmovement.Update(fps);
playas = mmovement.show();
std::cout << " " << movements.x;
playas.move(movements);
return playas;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
{
movements.y = 0;
movements.x = speed*fps;
mmovement.currentimage.y = right;
mmovement.Update(fps);
std::cout <<" "<< movements.x;
playas = mmovement.show();
playas.move(movements);
return playas;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
{
movements.y = speed*fps;
movements.x = 0;
mmovement.currentimage.y = down;
mmovement.Update(fps);
playas = mmovement.show();
std::cout << " " << movements.x;
playas.move(movements);
return playas;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
{
movements.y = -speed*fps;
movements.x = 0;
mmovement.currentimage.y = up;
mmovement.Update(fps);
playas = mmovement.show();
std::cout << " " << movements.x;
playas.move(movements);
return playas;
}
else
{
mmovement.currentimage.y = down;
mmovement.Update(fps);
playas = mmovement.show();
return playas;
}
}
move::~move()
{
}
Here's the header for animatrix:-
#pragma once
#include "SFML\Graphics.hpp"
class Animatrix
{
public:
Animatrix(sf::Texture* texture,sf::Vector2u num,float stime);
sf::Vector2u currentimage;
sf::Vector2u size;
float stime, fps;
float totaltime;
sf::Sprite show();
void Update(float fps);
sf::Sprite sprite;
sf::Vector2u count;
int j;
~Animatrix();
};
Here's the definition for animatrix:-
#include "Animatrix.h"
#include"SFML\Graphics.hpp"
#include<iostream>
Animatrix::Animatrix(sf::Texture* texture,sf::Vector2u num,float stime)
{
sprite.setTexture(*texture);
count.x = num.x;
count.y = num.y;
size.x= texture->getSize().x / count.x ;
size.y = texture->getSize().y / count.y ;
std::cout << std::endl << texture->getSize().x;
currentimage.x = 0;
currentimage.y = 0;
totaltime = 0.0;
this->stime = stime;
j = 0;
}
void Animatrix::Update(float fps)
{
this->fps = fps;
totaltime = totaltime + this->fps;
if( totaltime >= stime )
{
totaltime = totaltime - stime;
if (j == 0)
{
if (currentimage.x < count.x)
{
currentimage.x++;
}
if(currentimage.x==count.x-1)
{
j++;
}
}
else
{
j++;
currentimage.x--;
if (currentimage.x == 0)
{
j = 0;
}
}
}
}
sf::Sprite Animatrix::show()
{
sf::IntRect tempos;
tempos.left = currentimage.x*size.x;
tempos.top = currentimage.y*size.y;
tempos.width = size.x;
tempos.height = size.y;
sprite.setTextureRect(sf::IntRect(tempos.left, tempos.top,tempos.width,tempos.height));
return sprite;
}
Animatrix::~Animatrix()
{
}
PLEASE HELP!!!!!!!!!!