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

Author Topic: Moving a sprite without holding a key  (Read 4382 times)

0 Members and 1 Guest are viewing this topic.

Waxabee

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving a sprite without holding a key
« on: June 16, 2012, 03:24:06 pm »
Hello!

I need help for a little problem in my game. I want to shoot a ball when I press space, but I need to hold it to make the ball move. This is the function that I wrote for it, which is not working :
 
Vector2f Jeu::shoot(Vector2f posBall, Vector2f posPlayer)
{
    hideBall = false;
    canFollow = false;

    posBall.y -= 2;

    if(posBall.y < 0)
    {
        isMoving = true;
        posBall.y = posPlayer.y+16;
        posBall.x = posPlayer.x+32;
    }

    return posBall;
}

I tried a loop but this function is situated in the main loop, and it crashes. I use SFML 2.0 RC. Also, sorry for my English, I know it is not perfect.  ;D

Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Moving a sprite without holding a key
« Reply #1 on: June 16, 2012, 04:47:41 pm »
I guess the function is okay but you're problem is that you call it just once, which will move the ball just once.
You need to call the function every loop iteration or at least as often to get your wanted speed.

You see this isn't GameMaker or some other simple enviroment, you're code with pure C++ now. So it's very precise but it's not that intuitive, you can't just say that something should work so and so, you need to wirte all the code on your own.

So in your case you could maybe use a boolean which gets set to true when you hit space and it will allow the ball to move. The next thing would then be how do you stop the ball again?

Although we're willing to help I don't think someone will take the time to basically program all the logic you need for whatever you do, just sit down and think about a bit what you're doing.
Also a good understanding of C++ can be very helpfull.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Waxabee

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Moving a sprite without holding a key
« Reply #2 on: June 16, 2012, 07:43:07 pm »
Thanks for your answer.

I guess the function is okay but you're problem is that you call it just once

I call it in the main loop, I don't call it once.

You see this isn't GameMaker or some other simple enviroment, you're code with pure C++ now.

Yes... I know...  ??? Why are you saying that?

Although we're willing to help I don't think someone will take the time to basically program all the logic you need for whatever you do

I don't asked someone to program all the logic for me, I just have a question and I don't know how to resolve my problem.


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving a sprite without holding a key
« Reply #3 on: June 16, 2012, 07:52:09 pm »
Quote from: Waxabee
This is the function that I wrote for it, which is not working
We know neither what is not working nor what you expect. See also this thread.

By the way, there is a french SFML forum ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Waxabee

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Moving a sprite without holding a key
« Reply #4 on: June 16, 2012, 08:02:56 pm »
Yeah, but it is less active, and I think my English is ok for posting here.  :P

So, I will try to be more clear:

What I expect: I press space, and the ball is propulsed to y: 0, without holding space, like in a normal game.

What is not working: I need to hold space for the ball to move.

My question is : How to move a sprite without holding a key? I tried loops, and currently I'm trying to do it with booleans. I think that I really need loops, but when I use them, the game is crashing. And I repeat, this function is called in the main loop, when I press the space key.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Moving a sprite without holding a key
« Reply #5 on: June 16, 2012, 08:12:14 pm »
What Nexus was talking about is that you need to share/show us your code so we can point at what you did wrong.

What I meant previsouly was kinda bit harsh since I don't know how good your programming skills are but the question just seems very beginner like. ;)

Anyways I'm not gonna code you an example since I believe the documentation and the examples that come with SFML provide enough information and then there's Google and not to forget the forum search...

So unless you provide your code and we can point at something you won't really get much help. ;)

Also read that post: http://en.sfml-dev.org/forums/index.php?topic=5559.0
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving a sprite without holding a key
« Reply #6 on: June 16, 2012, 08:28:10 pm »
My question is : How to move a sprite without holding a key?
With a variable frame rate, a structure like this is possible:
sf::Clock clock;

while (/* in main loop */)
{
    ... // Event handling

    sf::Time dt = clock.restart();
    sf::Vector velocity = /* user-defined */;

    sprite.move(dt * velocity);

    ... // Rendering
}

If you want to move a sprite only if a certain key is pressed, add a corresponding if-condition. And the principle is the same when you outsource the code to a separate function.

For a fixed framerate (sf::RenderWindow::setFramerateLimit()), the velocity is constant, no dt needs to be multiplied.
« Last Edit: June 16, 2012, 08:29:56 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Waxabee

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Moving a sprite without holding a key
« Reply #7 on: June 16, 2012, 08:39:17 pm »
I will post the part of my main.

while(window.isOpen)

//Events

        if(keyboard.isKeyPressed(Keyboard::D))
        {
            posPlayer = player.movePlayerShip(true, false, false, false, posPlayer);
        }

        if(keyboard.isKeyPressed(Keyboard::A))
        {
            posPlayer = player.movePlayerShip(false, true, false, false, posPlayer);
        }

        if(keyboard.isKeyPressed(Keyboard::W))
        {
            posPlayer = player.movePlayerShip(false, false, true, false, posPlayer);
        }

        if(keyboard.isKeyPressed(Keyboard::S))
        {
            posPlayer = player.movePlayerShip(false, false, false, true, posPlayer);
        }

        if(keyboard.isKeyPressed(Keyboard::Space))
        {
            posBall = player.shoot(posBall, posPlayer, pressedSpace);
            pressedSpace = true;
        }
        else if(canFollow)
        {
            posBall.y = posPlayer.y+16;
            posBall.x = posPlayer.x+32;
        }


//Render and position updates

 

Only look the code for the space key. I call the function shoot, which is this function:

Vector2f Jeu::shoot(Vector2f posBall, Vector2f posPlayer, bool pressed)
{
    hideBall = false;
    canFollow = false;

    posBall.y -= 2;

    if(posBall.y < 0)
    {
        isMoving = true;
        posBall.y = posPlayer.y+16;
        posBall.x = posPlayer.x+32;
        pressed = false;
    }

    return posBall;
}

When I press space and holding it, the ball is moving toward the y : 0 position. If I don't hold it, it returns to the player position. Also, when I try to do a loop, the game crashes.

So, one question: Can I have some help, yes or no?
Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Moving a sprite without holding a key
« Reply #8 on: June 16, 2012, 08:54:48 pm »
        if(keyboard.isKeyPressed(Keyboard::Space))
        {
            posBall = player.shoot(posBall, posPlayer, pressedSpace);
            pressedSpace = true;
        }
        else if(canFollow)
        {
            posBall.y = posPlayer.y+16;
            posBall.x = posPlayer.x+32;
        }

Right there's your problem of reset.
If the player presses [space] it set the new position, but as soon as you let go of [space] key (i.e. else if) and canFollow is true (which I assume since your ball gets resetted) the ball gets the position of the player (+16/+32) again.

What you now need to do is set canFollow to false while the ball should be moving somewhere, taking the posBall = player.shoot(posBall, posPlayer, pressedSpace); out of that if-block and insert it after the else-if-block. Within another if-block where you first check if pressedSpace is true:

        if(keyboard.isKeyPressed(Keyboard::Space))
        {
            pressedSpace = true;
            canFollow = false;
        }
        else if(canFollow)
        {
            posBall.y = posPlayer.y+16;
            posBall.x = posPlayer.x+32;
        }
       
        if(pressedSpace)
        {
            posBall = player.shoot(posBall, posPlayer, pressedSpace);
        }
        if(/* ball reached target */)
        {
            canFollow = true; // If wanted
            pressedSpace = false;
        }
 

Also I suggest to use some delta time depending on the framerate otherwise you're sprites will move diffrent on diffrent machines. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Waxabee

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Moving a sprite without holding a key
« Reply #9 on: June 17, 2012, 02:11:49 pm »
It's working now, thanks. :)  Also, I'll do some research about delta time.