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

Author Topic: Simple, yet difficult(at least for me)  (Read 3826 times)

0 Members and 1 Guest are viewing this topic.

praetoriaen

  • Newbie
  • *
  • Posts: 20
    • View Profile
Simple, yet difficult(at least for me)
« on: October 06, 2011, 10:05:38 pm »
I am trying to change sprites when the user presses a key. For instance, there is a sprite of a chicken, if the user presses M, the chicken changes into a plane, and if he/she presses it again, it changes into a snake. I tried putting if's in the event chain after the if that is supposed to pick up the key.. but no luck. Any help? I am sure the answer is simple, but I'm probably too stressed out to see it :P
Through passion I gain strength
Through strength I gain power
Through power I gain victory
Through victory my chains are broken
--------------------------------------------
On a scale from bloat to enterprise, how does my code rank?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Simple, yet difficult(at least for me)
« Reply #1 on: October 06, 2011, 10:25:57 pm »
You should show us your code first ;)
Laurent Gomila - SFML developer

praetoriaen

  • Newbie
  • *
  • Posts: 20
    • View Profile
Simple, yet difficult(at least for me)
« Reply #2 on: October 07, 2011, 12:07:16 am »
No problem :)

This is my event loop. Currently, it is supposed to change a sprite first from a Slowpoke to a Me Gusta face, and then change it to a Mudkip. And it fails to work:
Code: [Select]


#include <SFML/Graphics.hpp>
#include <iostream>

#include "utilities.h"
#include "masterSprite.h"


int main()
{

sf::RenderWindow App(sf::VideoMode::GetMode(0), "I AM SLOWPOKE KING OF SLOW", sf::Style::Fullscreen);
App.UseVerticalSync(true);

bool running = App.IsOpened();


int width = App.GetWidth();
int height = App.GetHeight();

int indicator = 0;

masterSprite *pSlowpoke = new masterSprite("slowpoke.png");
masterSprite *pMegusta = new masterSprite("megusta.png");
masterSprite *pMudkip = new masterSprite("mudkip.png");
masterSprite *pWobbufet = new masterSprite("wobbufet.png");

sf::Sprite Sprite;

Sprite.SetImage(pSlowpoke->Image);

const sf::Input &Input = App.GetInput();

std::cout << width << std::endl;
std::cout << height << std::endl;


while (running)
{
bool indicator = true;

sf::Event Event;


while(App.GetEvent(Event))
{

if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::M))
{
if(indicator = true)
{
Sprite.SetImage(pMegusta->Image);
indicator = false;
}
else if(indicator = false)
{
Sprite.SetImage(pMudkip->Image);
indicator = true;
}
}
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::R))
{
utilities::r++;
Sprite.Rotate(utilities::r);
}

if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::T))
{
utilities::t++;
Sprite.SetRotation(utilities::t);
}


if(Event.Type == sf::Event::Closed)
{
running = false;
App.Close();
}

if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
{
running = false;
App.Close();
}


if(Input.IsKeyDown(sf::Key::Right))
{
utilities::x += utilities::speed;
if(utilities::x > width)
{
utilities::x = 0;
Sprite.SetPosition(utilities::x,utilities::y);
}
else
Sprite.SetPosition(utilities::x,utilities::y);
}

if(Input.IsKeyDown(sf::Key::Left))
{
utilities::x -= utilities::speed;
if(utilities::x < 0)
{
utilities::x = (float)width;
Sprite.SetPosition(utilities::x,utilities::y);
}
else
Sprite.SetPosition(utilities::x,utilities::y);
}

if(Input.IsKeyDown(sf::Key::Up))
{
utilities::y -= utilities::speed;
if(utilities::y < 0)
{
utilities::y = (float)height;
Sprite.SetPosition(utilities::x,utilities::y);
}
else
Sprite.SetPosition(utilities::x,utilities::y);
}

if(Input.IsKeyDown(sf::Key::Down))
{
utilities::y += utilities::speed;
if(utilities::y > height)
{
utilities::y = 0;
Sprite.SetPosition(utilities::x,utilities::y);
}
Sprite.SetPosition(utilities::x,utilities::y);
}
}

App.Clear();

App.Draw(Sprite);

App.Display();

}
return 0;

}



masterSprite.h:
Code: [Select]

#ifndef MASTERSPRITE_H_

#include <iostream>
#include <SFML\Graphics.hpp>


class masterSprite
{

public:

masterSprite(std::string imgLocation);
~masterSprite() {};

//variables
sf::Image Image;
std::string imgLocation;
};

masterSprite::masterSprite(std::string imgLocation)
{
this->imgLocation;

Image.LoadFromFile(imgLocation);

}

#endif MASTERSPRITE_H_


utilities.h:
Code: [Select]

namespace utilities
{

int speed = 10; //movement speed

float r = 0; //angle for incremental rotation
float t = 0; //angle for regular rotation

float x = 0; //x axis for movement
float y = 0; //y axis for movement

}


Sorry if it's a little bit bloated, I'm making it like that on purpose :D
Through passion I gain strength
Through strength I gain power
Through power I gain victory
Through victory my chains are broken
--------------------------------------------
On a scale from bloat to enterprise, how does my code rank?

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Simple, yet difficult(at least for me)
« Reply #3 on: October 07, 2011, 12:11:39 am »
I haven't read through all of it because there were a lot of utilities:: that were making me dizzy, but I think this is your problem

Code: [Select]

            if(indicator = true)
            {
               Sprite.SetImage(pMegusta->Image);
               indicator = false;
            }
            else if(indicator = false)
            {
               Sprite.SetImage(pMudkip->Image);
               indicator = true;
            }


1. You need == not =
2. You need not compare to true or false at all!, and the second if condition is unnecessary. Change it to this

Code: [Select]

            if(indicator)
            {
               Sprite.SetImage(pMegusta->Image);
               indicator = false;
            }
            else
            {
               Sprite.SetImage(pMudkip->Image);
               indicator = true;
            }

praetoriaen

  • Newbie
  • *
  • Posts: 20
    • View Profile
Simple, yet difficult(at least for me)
« Reply #4 on: October 07, 2011, 12:15:36 am »
Quote from: "sbroadfoot90"
I haven't read through all of it because there is a lot of utilities:: that was making me dizzy, but I think this is your problem

Code: [Select]

            if(indicator = true)
            {
               Sprite.SetImage(pMegusta->Image);
               indicator = false;
            }
            else if(indicator = false)
            {
               Sprite.SetImage(pMudkip->Image);
               indicator = true;
            }


1. You need == not =
2. You need not compare to true or false at all!, and the second if condition is unnecessary. Change it to this

Code: [Select]

            if(indicator)
            {
               Sprite.SetImage(pMegusta->Image);
               indicator = false;
            }
            else
            {
               Sprite.SetImage(pMudkip->Image);
               indicator = true;
            }


Tried with both =='s and with your example, nothing. It just changes the sprite to the Me Gusta face on the first key press, and on the second it does nothing.

Sorry for the utilities part, as I said, my aim is to make this program bloated :D

I'm using VS2010, if it matters...
Through passion I gain strength
Through strength I gain power
Through power I gain victory
Through victory my chains are broken
--------------------------------------------
On a scale from bloat to enterprise, how does my code rank?

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Simple, yet difficult(at least for me)
« Reply #5 on: October 07, 2011, 01:11:24 am »
OK I found your bug, the fact you're using VS2010 doesn't have anything to do with it :)

you just put
Code: [Select]
     bool indicator = true; inside your main loop meaning that it was resetting to true each frame. You needed to put it before
Code: [Select]
while(running)

praetoriaen

  • Newbie
  • *
  • Posts: 20
    • View Profile
Simple, yet difficult(at least for me)
« Reply #6 on: October 07, 2011, 01:15:19 pm »
Quote from: "sbroadfoot90"
OK I found your bug, the fact you're using VS2010 doesn't have anything to do with it :)

you just put
Code: [Select]
     bool indicator = true; inside your main loop meaning that it was resetting to true each frame. You needed to put it before
Code: [Select]
while(running)


Tried it just now, it seems that whenever I press any key that is in the event loop it starts switching sprites like crazy without displaying the first sprite... I tried adding it to the end of the event loop, but no avail, it is the same except it displays the first sprite.
Through passion I gain strength
Through strength I gain power
Through power I gain victory
Through victory my chains are broken
--------------------------------------------
On a scale from bloat to enterprise, how does my code rank?

praetoriaen

  • Newbie
  • *
  • Posts: 20
    • View Profile
Simple, yet difficult(at least for me)
« Reply #7 on: October 07, 2011, 01:22:32 pm »
Quote from: "praetoriaen"
Quote from: "sbroadfoot90"
OK I found your bug, the fact you're using VS2010 doesn't have anything to do with it :)

you just put
Code: [Select]
     bool indicator = true; inside your main loop meaning that it was resetting to true each frame. You needed to put it before
Code: [Select]
while(running)


Tried it just now, it seems that whenever I press any key that is in the event loop it starts switching sprites like crazy without displaying the first sprite... I tried adding it to the end of the event loop, but no avail, it is the same except it displays the first sprite.


EDIT: I feel like an idiot... i didn't put the key detection routine... it works now flawlessly :) thank you all
Through passion I gain strength
Through strength I gain power
Through power I gain victory
Through victory my chains are broken
--------------------------------------------
On a scale from bloat to enterprise, how does my code rank?

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Simple, yet difficult(at least for me)
« Reply #8 on: October 07, 2011, 01:24:50 pm »
Quote from: "praetoriaen"

Code: [Select]

            if(indicator)
            {
               Sprite.SetImage(pMegusta->Image);
               indicator = false;
            }
            else
            {
               Sprite.SetImage(pMudkip->Image);
               indicator = true;
            }
 


Sorry, but... =)
Code: [Select]

Sprite.SetImage(indicator ? pMegusta->Image : pMudkip->Image);
indicator = !indicator;

praetoriaen

  • Newbie
  • *
  • Posts: 20
    • View Profile
Simple, yet difficult(at least for me)
« Reply #9 on: October 07, 2011, 01:30:59 pm »
Quote from: "ChronicRat"
Quote from: "praetoriaen"

Code: [Select]

            if(indicator)
            {
               Sprite.SetImage(pMegusta->Image);
               indicator = false;
            }
            else
            {
               Sprite.SetImage(pMudkip->Image);
               indicator = true;
            }
 


Sorry, but... =)
Code: [Select]

Sprite.SetImage(indicator ? pMegusta->Image : pMudkip->Image);
indicator = !indicator;


Ah, ye olde ternary operator :D powerful, yet used by few :P
Through passion I gain strength
Through strength I gain power
Through power I gain victory
Through victory my chains are broken
--------------------------------------------
On a scale from bloat to enterprise, how does my code rank?

 

anything