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

Author Topic: moving a Sprite using class (.h file)  (Read 2875 times)

0 Members and 1 Guest are viewing this topic.

rustam2735

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
moving a Sprite using class (.h file)
« on: November 21, 2012, 05:03:29 pm »
Hi, I can make the sprite character move on the with out any problem with out using any classes or headers, however when ever I try make use of .h files my sprite resets back to the original position. it just wont save the curent location, always resets it to the original coordinates.

i know its really badly written and there some exces staff on it. Please any one help, I pretty sure it is something stupid that i am missing

player.h
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"

class Player
{
public:
        void loadPlayer();
        void playerMovement(sf::RenderWindow &window);
        void drawPlayer(sf:: RenderWindow &window);
        int inc(sf::RenderWindow &window){
                        c=100;
                        if(window.GetInput().IsKeyDown(sf::Key::C)) {c++;
                                std::cout<<"c:"<<c<<std::endl;}
               
        }
                ~Player(){
                        c--;
                        std::cout<<"c:"<<c<<std::endl;
                }
protected:
        Animation playerAnimation;
        //playerAnimation.isActive();
private:
       
        sf::Image playerImage;
        sf::Sprite playerSprite;
        float VelocityX,velocityY;
        float coordinateX, coordinateY, moveSpeed;
        int sourceX, sourceY;
        static int c;
               
};
 

player.cpp

#include "player.h"

void Player::loadPlayer(){
        if(playerImage.LoadFromFile("player.png")){
                playerSprite.SetImage(playerImage);}
}
int Player::c=0;


void Player::playerMovement(sf::RenderWindow &window){
        int sDown=0;
        int sLeft=48;
        int sUp=144;
        int sRight=96;
        coordinateX=0,coordinateY=0;
        coordinateX=10, coordinateY=10;
        sourceX=0, sourceY=0;
        moveSpeed=7;
       
        if (window.GetInput().IsKeyDown(sf::Key::Right)){
                sourceY=sRight;
                coordinateX+=moveSpeed*window.GetFrameTime();//needs work
        }
        else if (window.GetInput().IsKeyDown(sf::Key::Left)){
                sourceY=sLeft;
                coordinateX=-moveSpeed;
        }
        else coordinateX=0;
       
        if (window.GetInput().IsKeyDown(sf::Key::Down)){
                sourceY=sDown;
                coordinateY=moveSpeed;
        }
        else if (window.GetInput().IsKeyDown(sf::Key::Up)){
                sourceY=sUp;
                coordinateY=-moveSpeed;
        }
        else coordinateY=0;
        playerAnimation.setPosition(1, coordinateX);
        playerAnimation.setPosition(2, coordinateY);
       
        coordinateX+=coordinateX;
        coordinateY+=coordinateY;
       
        if (coordinateX!=0||coordinateY!=0)
                sourceX+=playerImage.GetWidth()/3;
        else  sourceX=0;
        if (sourceX==playerImage.GetWidth()) sourceX=0;
       
        playerSprite.SetSubRect(sf::IntRect(sourceX,sourceY,sourceX+playerImage.GetWidth()/3,sourceY+playerImage.GetHeight()/4));
        playerSprite.SetPosition(coordinateX,coordinateY);
        window.Clear(sf::Color(c,255,255));    
        window.Draw(playerSprite);

        std::cout<<"velocityX:"<<coordinateX<<std::endl;


}

void Player::drawPlayer(sf:: RenderWindow &window){
        window.Draw(playerSprite);
}

 

animation.h
#include <SFML/Graphics.hpp>

class Animation
{
public:
        void setPosition(int axis, float value);
        bool isActive(bool active);
private:
        float coordinateX,coordinateY;
};
 

animation.cpp
#include "animation.h"

bool Animation::isActive(bool active){
        active=true;
}

void Animation::setPosition(int axis, float value){
        if(axis==1)
                value=coordinateX;
        else
                value=coordinateY;
       
        this->coordinateX=coordinateY;
        this->coordinateY=coordinateY;

}
 

main.cpp

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Graphics");
       
        Player mainCharecter;
        mainCharecter.loadPlayer();

        int i=1;
        bool active=true;
        /*
        float velocityX=0,velocityY=0;
        float x=10, y=10;
        int sourceX=0, sourceY=0;
        */

    while (active==true)
    {
        sf::Event event;
        while (window.GetEvent(event))
        {
            if (event.Type == sf::Event::Closed||event.Key.Code==sf::Key::Escape)
                active=false;
                }
                mainCharecter.playerMovement(window);

                window.SetFramerateLimit(10);

        window.Display();
    }

    return EXIT_SUCCESS;
}

 

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: moving a Sprite using class (.h file)
« Reply #1 on: November 21, 2012, 05:48:10 pm »
First off: try to isolate your problem. There is already too much code for us to be able to quickly spot your problem.

In the function Player::playerMovement() you set
coordinateX=0,  coordinateY=0;
coordinateX=10, coordinateY=10;
Why is this?

rustam2735

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: moving a Sprite using class (.h file)
« Reply #2 on: November 21, 2012, 06:43:37 pm »
that was just a mistake. as you can see it is really messy. sorry for that.

there aren't any errors, it just that in the window my sprite only moves 7 space (because my moveSpeed=7) in any direction. but the problem i think is in the player.cpp where it resets coordinateX and coordinateY back to 10.

however if i dont use any classes, then my sprite moves perfectly around the window

this is my main.cpp with out any classes.


#include <SFML/Graphics.hpp>

#define sDown 0
#define sLeft 48
#define sUp 144
#define sRight 96

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Sprite");
       
        sf::Image tempImage;
    if (!tempImage.LoadFromFile("walking2.png"))
        return EXIT_FAILURE;
       
    sf::Sprite playerSprite(tempImage);
       
        float velocityX=0, velocityY=0;
        float x=10,y=10, moveSpeed=7;
        int sourceX=0,sourceY=sDown;
       
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
                        if ((Event.Type==sf::Event::KeyPressed)&&(Event.Key.Code==sf::Key::Escape))
                                App.Close();
        }
                if (App.GetInput().IsKeyDown(sf::Key::Right)){
                        sourceY=sRight;
                        velocityX=moveSpeed;
                }
                else if (App.GetInput().IsKeyDown(sf::Key::Left)){
                        sourceY=sLeft;
                        velocityX=-moveSpeed;
                }
                else velocityX=0;
               
                if (App.GetInput().IsKeyDown(sf::Key::Down)){
                        sourceY=sDown;
                        velocityY=moveSpeed;
                }
                else if (App.GetInput().IsKeyDown(sf::Key::Up)){
                        sourceY=sUp;
                        velocityY=-moveSpeed;
                }
                else velocityY=0;
               
                x+=velocityX;
                y+=velocityY;
               
                if (velocityX!=0||velocityY!=0)
                        sourceX+=tempImage.GetWidth()/3;
                else  sourceX=0;
                if (sourceX==tempImage.GetWidth()) sourceX=0;
               
                App.Clear();
                playerSprite.SetSubRect(sf::IntRect(sourceX,sourceY,sourceX+tempImage.GetWidth()/3,sourceY+tempImage.GetHeight()/4));
                playerSprite.SetPosition(x,y);
               
               
                                App.SetFramerateLimit(10);
        // Clear screen
        App.Clear(sf::Color(255,255,255));
               
        // Display sprite in our window
        App.Draw(playerSprite);
               
        // Display window contents on screen
        App.Display();
    }
       
    return EXIT_SUCCESS;
}
 

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: moving a Sprite using class (.h file)
« Reply #3 on: November 21, 2012, 07:19:38 pm »
Well, if you want us to be able to help you, you should provide the code and only that code which should contain your error. Please read the rules and rephrase your question.

rustam2735

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: moving a Sprite using class (.h file)
« Reply #4 on: November 21, 2012, 08:29:25 pm »
ok, sorry about this

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: moving a Sprite using class (.h file)
« Reply #5 on: November 21, 2012, 09:45:21 pm »
You could just as well make your class inherit from sf::Transformable, you just use the functions you normally use with sf::Sprite without problems. I didn't read all of your code, it's far too big to actually entourage yourself in it. Write a minimal and complete example and you might get more help.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: moving a Sprite using class (.h file)
« Reply #6 on: November 21, 2012, 10:34:20 pm »
You are resetting the coordinate every frame in playerMovement(..), that's why your character isn't moving.

It might help to split your Player class into specific functions:
- an initialisation function, which sets the starting position of the player, sets up the sprites, etc..
- an update_input function, which reads in the keyboard input and converts it to a desired movement amount
- an update_position function, which takes the desired movement amount and adds it to the position
- a render function, which chooses the correct frame to draw, and renders the player

 

anything