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

Author Topic: Sprite returning to orginal posistion, after setPosition  (Read 1158 times)

0 Members and 2 Guests are viewing this topic.

Overkiller

  • Newbie
  • *
  • Posts: 8
    • View Profile
Sprite returning to orginal posistion, after setPosition
« on: July 24, 2013, 07:02:42 pm »
Hi!
It's my first post here, so I want to thank to LaurentGomila for creating such great lib!

Here is my code, not very complex and please don't blame for it. I have small C++ experience:
Mouse class:
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
using namespace sf;
// class for maitain mouse
class mysz
{
public:
        mysz(void);
        Vector2i pozycjaMyszy; // variable for mouse coordinates
        Vector2i pobierzPozycjeMyszy(); // method for getting them
        Vector2f pozycjaFloat; // same as pozycjaMyszy but in float(for moving sprite)
        void ustawPozycje(); // later iplemented
        float poprzedniaPozycjaX;
        float poprzedniaPozycjaY;
        ~mysz(void);
};
 
And their implementation:
#include "mysz.h"


mysz::mysz(void)
{
}
Vector2i mysz::pobierzPozycjeMyszy()
{
        pozycjaMyszy = Mouse::getPosition();
        pozycjaFloat = static_cast<Vector2f>(pozycjaMyszy); //changing from Vector2i to Vector2f
        poprzedniaPozycjaX = pozycjaFloat.x;
        poprzedniaPozycjaY = pozycjaFloat.y;
        return pozycjaMyszy;
}

mysz::~mysz(void)
{
}

 
Image class:
//maitain images
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
using namespace sf;

class obrazek
{
public:
        obrazek(void);
        void stworz();
        void nadajKolor();
        void zmienPozycje(float x, float y); //for setting position of image
        void ustawPozycje(float x, float y);
        int zaladuj();
        ~obrazek(void);
        Texture teksturaObrazka;
        Sprite duchObrazek;
};
 
And implementation:
#include "obrazek.h"


obrazek::obrazek(void)
{
}
void obrazek::stworz()
{
}
void obrazek::nadajKolor()
{

}
void obrazek::zmienPozycje(float x, float y)
{
        duchObrazek.setPosition(x,y);
}
int obrazek::zaladuj()
{
        if(!teksturaObrazka.loadFromFile("01.png"))
        {
                return EXIT_FAILURE;
        }else
        {
                duchObrazek.setTexture(teksturaObrazka);
                duchObrazek.setOrigin(400,300);
                return EXIT_SUCCESS;
        }
}
void obrazek::ustawPozycje(float x, float y)
{
        duchObrazek.setPosition(x,y);
}
obrazek::~obrazek(void)
{
}
 
Main:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "obrazek.h"
#include "mysz.h"
using namespace sf;
int main()
{
    RenderWindow oknoGry(VideoMode(1024, 768), "Okno Glowne", Style::Close);
        while(oknoGry.isOpen())
        {
                obrazek obrazekJeden;
                mysz myszGry;
                myszGry.pobierzPozycjeMyszy();
                obrazekJeden.zaladuj();
                Event Zdarzenia;
               
               
                while(oknoGry.pollEvent(Zdarzenia))
                {
                        if(Zdarzenia.type == Event::Closed)
                        {
                                oknoGry.close();
                                return 0;
                        }
                        if(Keyboard::isKeyPressed(Keyboard::P))
                        {
                                obrazekJeden.zmienPozycje(myszGry.pozycjaFloat.x, myszGry.pozycjaFloat.y);
                        }
                       
                }
               
                oknoGry.clear();
       
                oknoGry.draw(obrazekJeden.duchObrazek);
                oknoGry.display();
        }
    return 0;
}
 
My issuse is following: after releasing "P" key my sprite is returning to left upper corner. I was tryin to fix that but, it doesn't worked.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Sprite returning to orginal posistion, after setPosition
« Reply #1 on: July 24, 2013, 07:20:32 pm »
I can't read the code properly but I think the problem is that the variable "obrazek obrazekJeden" is declarated in the "Game loop". Because of that you get every frame a new instance from "obrazek obrazekJeden" and so, your object only moves when you change it in the current frame but it doesn't save the new position.

You should create this variable before the "while(oknoGry.isOpen())" but after the "RenderWindow".



AlexAUT

Overkiller

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Sprite returning to orginal posistion, after setPosition
« Reply #2 on: July 24, 2013, 07:45:16 pm »
AlexAUT, sorry for not translating variables. I'm from Poland and with my Polish friend are writng a game for competition, so i'm using Polish names for easietr understandingo of code, for my friend.

Your anwser repaired my problem so, thank you for help. :) I was thinking about issue, but any of my try wasn't repairing issue.