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 / What is the Best Way to handle Sprite Movement in a Platformer Game?

Author
Message
SageTech
19
Years of Service
User Offline
Joined: 3rd Dec 2004
Location: Orlando, Florida
Posted: 27th Jan 2012 03:57
I'm still learning the rops of AppGameKit and I'm trying to figure out the best way to handle the movement of the Player's Sprite. I'm creating a side scroller platformer game, so the player more or less moves left or right. I know you can just set the sprite position each frame, but since I'm using the Physics system this is probably not the way it should be done.

How do you handle moving a player within the physics system with a traditional joystick? I'm guessing using forces, but how do you do the kind of suden stop/start motion common to games?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 27th Jan 2012 09:34
I was thinking of doing a demo of something like this for a future AppGameKit Bitesize tutorial. Perhaps I could whip up some simple code and expand on it for the tutorial... but yes the physics system can be used to simplify this a lot.

A few pointers:
-Use a "dynamic" physics sprite for your player
-Use "static" physics sprite for your scenery
-Use "kinematic" physics sprite for moving platforms etc.
-I would set the velocity of your player sprite rather than applying forces (in X for walking and minus Y for jumping)

I'll start a simple example for you as soon as I have time.

bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 27th Jan 2012 10:15
A tutorial for controlling the player using physics, and ideas how to build big levels using tiles would be greatly appreciated
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 27th Jan 2012 10:22
I might have to separate those into two but yes I like both of those ideas. I wanted to work on some of the concepts required for both so that's a great way forward. February's AppGameKit Bitesize is an introduction to the physics system and how to build a simple "Angry Birds" style game. Platformers would be a nice one to cover a little more physics and the virtual joystick and button commands. I'll work on it for March's edition but I'll try to post some code here too.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 30th Jan 2012 16:14 Edited at: 30th Jan 2012 16:15
Took a while but I finally got around to writing a very simple platform example for one of the AppGameKit tutorials. Here is a simple version:


Oglong
12
Years of Service
User Offline
Joined: 25th Feb 2012
Location:
Posted: 25th Feb 2012 22:31
I'm new to AppGameKit and I have been looking for some tutorials on side scrolling platform games but other then the examples that come with AppGameKit its been difficult.

I have managed to create a level that scrolls between 2 images by using setviewoffset command when the character moves but finding it difficult to stop the screen scrolling when character gets to the second image.

Can anyone point me in the right direction? Perhaps there is better way of coding

Thanks

Dan
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 25th Feb 2012 23:01
Perhaps you could share some code? It's hard to give pointers without knowing what you are doing right or wrong

kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 26th Feb 2012 10:11
Here's a sample of what are you trying to achieve.

BASIC code:



C++ code:



I hope this helps

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 26th Feb 2012 20:11
@Oglong, I would set a variable which represents the level size. Check your viewport x co-ords, and only allow the scrolling when x is less than the level size variable. Same idea for moving left but checking if the viewport x is greater than the start of the level.

Oglong
12
Years of Service
User Offline
Joined: 25th Feb 2012
Location:
Posted: 26th Feb 2012 21:34
Thanks guys, appreciate the quick response - ill check through your recommendations and let you know how i get on.

This is my code for the main loop so far, probably a little messy as things have got a little out of hand since trying different ideas.



do

SetViewOffset ( x# + -35, 0.0 )

joystickX# = GetVirtualJoystickX ( 1 )
joystickY# = GetVirtualJoystickY ( 1 )

// face the correct way
if ( joystickX# < 0.0)

SetSpriteFlip ( 5, 1, 0)
elseif ( joystickX# > 0.0)

SetSpriteFlip (5,0,0)
endif

// find player position
x# = GetSpriteX ( 5 )
y# = GetSpriteY ( 5 )

if ( joystickX# = 0.0 and joystickY# = 0.0 )

StopSprite ( 5 )
SetSpriteFrame ( 5, 3 )
else
// play animation
if ( GetSpritePlaying ( 5 ) = 0 )
PlaySprite ( 5, 10, 1, 0, 2 )
endif

endif
rem character jump
if getSpriteCollision(5,1)=1
jump = GetVirtualButtonPressed(1)
if jump>0
setSpritePhysicsVelocity(5,getSpritePhysicsVelocityX(5),-jump*170)
endif
endif
rem move character

if GetSpriteX (5) < 0
setSpritePhysicsVelocity(5,GetVirtualJoystickX(1)*80,getSpritePhysicsVelocityY(5))
endif

if GetSpriteX (5) > 0
setSpritePhysicsVelocity(5,GetVirtualJoystickX(1)*80,getSpritePhysicsVelocityY(5))
endif

if GetSpriteX (5) > 900
setSpritePhysicsVelocity(5,GetVirtualJoystickX(1)*0,getSpritePhysicsVelocityY(5))
endif

Sync()

loop

Dan
Oglong
12
Years of Service
User Offline
Joined: 25th Feb 2012
Location:
Posted: 26th Feb 2012 22:22
Apologies I missed the /code at the end

Dan
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 27th Feb 2012 11:03
Quote: "Apologies I missed the /code at the end"

You can edit posts by clicking "Edit Post" at the bottom left of any post you have made.

Zaeffer
12
Years of Service
User Offline
Joined: 28th Feb 2012
Location:
Posted: 28th Feb 2012 07:17
This is awesome... I'm relatively new to programming (and just starting my COMP science degree) and I have had my heart set on making a side-scroller for my first big project -- think 'robot unicorn attack' or 'oven break' though, where sprite position is fixed on the x axis. BTW baxslash, your example snippet has an interesting flaw, in that your sprite can't decide which surface it is more attracted to, and constantly jumps from one to tother...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 09:02
Quote: "BTW baxslash, your example snippet has an interesting flaw, in that your sprite can't decide which surface it is more attracted to, and constantly jumps from one to tother..."

Not quite sure what you mean? Perhaps try changing the shape of the player to a circle or a polygon for a more realistic effect?

Zaeffer
12
Years of Service
User Offline
Joined: 28th Feb 2012
Location:
Posted: 28th Feb 2012 10:31
Nah, that comment was about a 'glitch' (at least I think it was a glitch?) in the code snippet whereby, when I compiled/run etc, the app worked, but the sprite would continuously jump between its current platform, and either the 'roof' or floor.. as if the jump function were perma looping or the physics engine couldn't decide which direction for the sprite to fall in...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 11:58
I haven't had that problem... strange. Could you upload your project folder?

Login to post a reply

Server time is: 2024-11-23 05:30:18
Your offset time is: 2024-11-23 05:30:18