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

Author Topic: Sprite doesn't move along with handler exception  (Read 3191 times)

0 Members and 1 Guest are viewing this topic.

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Sprite doesn't move along with handler exception
« on: December 28, 2016, 01:44:40 pm »
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:-
Quote
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!!!!!!!!!! :-[ :-[ :'(
« Last Edit: December 29, 2016, 01:18:17 pm by Laurent »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Sprite doesn't move along with handler exception
« Reply #1 on: December 28, 2016, 05:51:35 pm »
Quote
Access violation executing location 0x00000000.
the memory location corresponds to NULL ( 0x00000000 ) this means that you are dereferencing or writing on a NULL location.

I couldn't detect this error on your code "at glance" but you should do a more deep inspection.

Then if you believe that your code is not writing/reading a NULL pointer check if your .libs and .dll are the corrects for your compiler and debug/release are not mixed.
I would like a spanish/latin community...
Problems building for Android? Look here

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Sprite doesn't move along with handler exception
« Reply #2 on: December 29, 2016, 01:07:34 pm »
I have checked the dlls and lib and they are not mixed up as far as I can tell.What about the sprite not moving?Any suggestions?

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Sprite doesn't move along with handler exception
« Reply #3 on: December 29, 2016, 07:56:24 pm »
I have checked the dlls and lib and they are not mixed up as far as I can tell.What about the sprite not moving?Any suggestions?

Right now i´m kinda bussy.

But if you clean up and organize your code i could help you better.

But i recommend you something: First try to find what cause the access violation, this is worst than "my sprite is not moving".

By the way, if all your .dll and .libs are in the right places the best attempt you can do is rewrite all your code.

Remember that behind a good code there is a good algorithm and a good knowledge of the language that is being used.
I would like a spanish/latin community...
Problems building for Android? Look here

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Sprite doesn't move along with handler exception
« Reply #4 on: December 29, 2016, 09:46:50 pm »
the problem is that it shows after the program has run for a few minutes.And on breaking at that point it shows the next line to run is window.display().So I have no idea why suddenly the sprite can't be displayed when it was being displayed for the last few minutes.Anyways thnx

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Sprite doesn't move along with handler exception
« Reply #5 on: December 30, 2016, 05:01:19 am »
i agree with DarkRoku about you need to clean up and organize your code.

in your current code is unreadable, you need to pick proper names for classes and member functions. you should read more about C++ classes, more importantly use comments to describe what your classes and its member functions actually do. this will help you to grasp what you might learn in c++ and others who will read your code. take SFML source code as example,

it seems that you are creating animation class. look to this example . it is well written and excellent source example of C++/SFML to start of for your game.

about your question why your application crash after few minutes. crashing programs usually generates a core dump file that can be loaded in windbg a debug tool that have an image of the memory at the time the program crashed. there's nothing much you can do with it, but a least it gives you the last call stack.


 

anything