Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Moving a Sprite with the keyboard

Author
Message
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 26th Feb 2013 21:12
So I am trying to move a sprite with the keyboard using Tier 2. Maybe I am not understanding what exactly GetRawKeyPressed() does but I was under the impression that If I send it the argument 0 then it will return 1. Now that is what I check for but I do not understand why it doesn't work.

My code is as such.


I thought that this would work as it is similar to what I have done using other engines in the past. And if it matters I'm doing this under xcode currently.

Thank you.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 26th Feb 2013 22:18
GetRawKeyPressed takes a value from zero to 255. The value is meant to indicate which key you are checking for.

This is the help page for the values to use for the different keys.

If you want to check for the arrow keys, those are values 37 through 40.

The command returns 1 if the key indicated by the passed value has been pressed.

It takes zero to 255, but there are not actually keys corresponding to many of those values.

There is no command that tells you that any key was pressed, only this one that tells you if a specific key was pressed.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 26th Feb 2013 23:08 Edited at: 26th Feb 2013 23:09
GetRawKeyPressed only returns 1 during the frame it was pressed.

GetRawKeyReleased returns 1 during the frame it was released.

GetRawKeyState should be used to check if a key is held down.

GetRawLastKey can be used to retrieve the keycode of the last key pressed.

Use this to print keycodes on the screen:
print( GetRawLastKey() )


Demo 3 is out now!
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 27th Feb 2013 06:29
Okay so right now I have this code



Is there a better way I should be doing this such as with one of the physics functions or does it matter? Also how exactly would I go about doing a jump. Do I have to set an upward force and then an equal downforce after a certain amount of time?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 27th Feb 2013 16:22
If you want to use physics, then you use force or impulse to move the player in the desired direction instead of setspriteposition.

Then, for jumping, you just add a larger force/impulse up.

If you have gravity set on, and no other force is applied, the player will eventually fall down after the jump up.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 27th Feb 2013 16:47
Okay that is what I was assuming would be the best idea. Now for if I have gravity set on it will just keep falling off the screen right? How would I go about setting boundaries for it to be "ground" where the sprite will stop falling.
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 27th Feb 2013 17:09
Some like me have made their own physics/movement while other have had success with the built in physics. I made my own since I couldn't get the results I wanted with the built in stuff.

Figuring out how to get an animated character to move and jump around on the screen is a good exercise in programming though.


Demo 3 is out now!
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 27th Feb 2013 17:24 Edited at: 27th Feb 2013 17:36
So I'm trying to use the SetSpritePhysicsImpulse but when I call it this way


nothing happens.

Here is the full codeblock for reference


I should also say that the sprite is set to be a physics sprite but it just rotates when I do this instead of going up.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 27th Feb 2013 22:04
You need to change your impulse call. Using just 0 and -10 is not correct.

For starters, you need to make sure that you use floating point values, since you are working in Tier 2. C++ can be very type sensitive.

Next, try this:


The idea behind using PhysicsImpulse is to push the sprite from its current location to the one you want it to go to.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 27th Feb 2013 22:24
Okay I think I am starting to understand it a little better now. So What would I do to make it act like a real jump as in I press up and it jumps and comes back down because now it hangs for a bit. Also For moving the sprite left to right should I give it a smaller impulse so it stops when released or do something like agk::GetRawKeyReleased() and give the sprite a impulse in the opposite direction to result in them negating?

Also I set my gravity in this way


Is that correct or should I make it a smaller value?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 27th Feb 2013 23:24
If you use a higher value for the gravity, it will fall back sooner.

And, again, use floating point values as the parameters.

The way you are using GetRawKeyState implies that as long as the key is held down, a new impulse will be imparted to the sprite each sync cycle.

If you want it to jump when a key is pressed (but not held down), use GetRawKeyPressed(<keyvalue>. This will return 1 if the key was pressed and released.

When you call SetSpritePhysicsImpulse, it pretty much changes the direction immediately. So, there is no need to override the previous call.

If you want the sprite to actually stop when the left/right buttons are released, use "agk::SetSpritePhysicsVelocity(<spriteid>,0.0,0.0);". This will instantly stop the sprite.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 28th Feb 2013 00:23
Alright I think I have a handle on the physics system now for what I need to do. Is there any particular practice that is considered best in AppGameKit for doing something like a doublejump. Like how I have it now you can just press 'W' and it will apply the impulse and then you just release and press again it applies a second impulse making the sprite move even higher. Now how could I limit that to say two so a player can only do a double jump and has to wait until on the ground/ any surface not moving to jump again?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 28th Feb 2013 16:24
I can see where a double jump would be a cool thing to use.

I suppose if you track the height of the sprite and it is above a certain point, don't allow another jump until the sprite is back at ground level.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 28th Feb 2013 17:03 Edited at: 28th Feb 2013 17:04
Or... You could have a 'Jump Count'. When the character initially jumps, set the force (I would imagine impulse) and increment the jump count by 1... If the jump button is pressed for a second time (whilst still airborne), do the same again, apply the force and increment the jump count to 2... Then only allow the impulse to be applied when Jump Count is LT 2. When the character hits the ground, reset the Jump Count to 0? Might work...

Login to post a reply

Server time is: 2024-05-02 08:53:58
Your offset time is: 2024-05-02 08:53:58