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

Author Topic: Jumping Sprite triggers after few seconds  (Read 2313 times)

0 Members and 1 Guest are viewing this topic.

bissuas

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Jumping Sprite triggers after few seconds
« on: April 23, 2012, 10:47:32 am »
I am trying to jump my simple sprite and i have written some code but every time when i press up key which is for jumping the sprite sprite jumps after few seconds , is there any solution here is my code please help as soon..Thanks in advance.



using namespace std;

int main()
{
   sf::VideoMode Vmode1(600,400,32);
   sf::RenderWindow Window1(Vmode1, "Project Aasra");
   Window1.SetFramerateLimit(60);
   Window1.UseVerticalSync(true);

   //SECTION START: Images and Sprites

    double x,y,dx, dy;
    x=y=dx=dy=0;

   sf::Image mario2;
   if(!mario2.LoadFromFile("images/mario2.png")){return EXIT_FAILURE; }
   sf::Sprite mario2_sprite(mario2);


    bool hold =true;
    while(hold)
   {
      sf::Event Event1;

    bool space_down=false;
      while(Window1.GetEvent(Event1))
      {
         //window controls
         if(Event1.Type==sf::Event::Closed) { Window1.Close(); hold=false; }
      }

      //SECTION START: direction keys press
      if(Window1.GetInput().IsKeyDown(sf::Key::Right))  dx+=0.5;

      if(Window1.GetInput().IsKeyDown(sf::Key::Left))   dx-=0.5;

        mario2_sprite.SetPosition(x,y);

      x+=dx;
      y+=dy;

    /*friction*/
      dx*=0.775;


    /*jump*/

      if((Window1.GetInput().IsKeyDown(sf::Key::Up)))
      {
        dy=-4.0;
        space_down=true;
      }

      if (!Window1.GetInput().IsKeyDown(sf::Key::Up)) space_down=false;


    /*gravity*/
      if (mario2_sprite.GetPosition().y<252) dy+=0.1;

    /*Screen Limit*/
    if((mario2_sprite.GetPosition().y + mario2_sprite.GetSize().y) > 252)
    {
        mario2_sprite.SetY(252);
    }

      Window1.Clear(sf::Color(0,255,255));
      Window1.Draw(mario2_sprite);
      Window1.Display();
   }
}
 


« Last Edit: April 23, 2012, 10:54:45 am by Laurent »

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Jumping Sprite triggers after few seconds
« Reply #1 on: April 23, 2012, 03:12:18 pm »
I wrote a program from scratch with a sprite jumping.

Here you go.

Code: [Select]
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
using namespace std;

int main ()
{
float gravity = 0.2; // gravity... duh
float speed_y = 0; // vertical speed
bool ground = false; // if it's on the ground or in the air
sf::RenderWindow App(sf::VideoMode::GetMode(0), "Jumping Mario!", sf::Style::Fullscreen);
App.SetFramerateLimit(90);

sf::Image Mario;
Mario.LoadFromFile("mario2.png");

sf::Sprite mario;
mario.SetImage(Mario);

while(App.IsOpened())
    {
sf::Event Event;

while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}

// the movement

if (App.GetInput().IsKeyDown(sf::Key::Left))mario.Move(-300 * App.GetFrameTime(), 0);
if (App.GetInput().IsKeyDown(sf::Key::Right))mario.Move(300 * App.GetFrameTime(), 0);

// if it's on the ground

if (ground == true)
{
    if (App.GetInput().IsKeyDown(sf::Key::Space))
{
speed_y = -10;
ground = false;
}
}

// if it's in the air

if (ground == false)
{
speed_y = speed_y + gravity; // gravity takes effect
}
mario.Move(0, speed_y);

if (mario.GetPosition().y > App.GetHeight() - mario.GetSize().y) // tiny tiny tiny collision for the "floor"
{
ground = true;
mario.SetPosition(mario.GetPosition().x, App.GetHeight() - mario.GetSize().y);
}

App.Clear(sf::Color(255,255,255,255));
App.Draw(mario);
App.Display();
}
}

Ps. I hate the way the site messes up the spaces in your code.
« Last Edit: April 23, 2012, 03:17:53 pm by Dannehood »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10927
    • View Profile
    • development blog
    • Email
Re: Jumping Sprite triggers after few seconds
« Reply #2 on: April 23, 2012, 05:23:54 pm »
Ps. I hate the way the site messes up the spaces in your code.

Who says that's a problem of the forum?
I mean he did mix one spaces and two spaces for indention.

What annoys me more is that many people past their code in here and do not highlight it. It's dead simple just add a '=cpp' within the code-tag. ;)

As for the topic:
I guess you best sit down with a piece of paper and think what you want to do. For now you add up in every iteration some hight which will lift your sprite in a linear matter, what you actually want to perform is a curve jump movement, which then again needs a variable speed. In the beginning it has to be fast, towards the highst point it should slow down and shortly before it hits the ground it should be as fast as in the beginning.
That's the basic idea, although platformer don't act like physical objects would, so you'll have to adjust those phases to give the player the well known jump feeling.

As for your game loop, you first set the position, then calculate the new position, then draw and the loop. Mabye you should rethink the order.

And at last you should make your movement depend up on the fps (or sync a fixed time step with the fps) otherwise you'll get diffrent behaviour on diffrent machines. (Better PC will move quicker, slower PCs will be slower).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bissuas

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Jumping Sprite triggers after few seconds
« Reply #3 on: April 23, 2012, 07:44:46 pm »
Thank u both for ur suggestion. bcoz i am very new to all these stuffs, ur suggestion means a lot. thanks again.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10927
    • View Profile
    • development blog
    • Email
Re: Jumping Sprite triggers after few seconds
« Reply #4 on: April 24, 2012, 12:50:13 am »
Thank u both for ur suggestion. bcoz i am very new to all these stuffs, ur suggestion means a lot. thanks again.

You probably wrote that very quickly, but nevertheless please you normal English words. In this community meet people from all over the world, it's okay to make mistakes as I do all the time, but try your best in writing proper sentences and words.

You're welcome! :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bissuas

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Jumping Sprite triggers after few seconds
« Reply #5 on: April 24, 2012, 05:14:07 am »
OK dude, I will write complete words and sentences from now onwards...Thanks.

 

anything