A Platform game in SpriteKit and F# – Part 3 – A Jumping dude

I know you can't tell, but he really is jumping!

Today’s post is going to be a short one – I’m going to add a dude who we can make jump.

Before I go ahead and do that it’s important to add some physical properties to allow our player to land on – otherwise he’ll simply fall through the scenery!

So first lets set the following properties on all our existing bits of the level:

sprite.PhysicsBody <- SKPhysicsBody.BodyWithRectangleOfSize sprite.Size
sprite.PhysicsBody.AffectedByGravity <- false
sprite.PhysicsBody.Dynamic <- false<

And via a really quick refactor I can put all that into a single function and then replace all my “new SKSpriteNode” calls with the function. So

use grass = new SKSpriteNode("grass")

goes to

let createLevelSprite (name:string) = 
   let sprite = new SKSpriteNode(name)
   sprite.PhysicsBody <- SKPhysicsBody.BodyWithRectangleOfSize sprite.Size
   sprite.PhysicsBody.AffectedByGravity <- false
   sprite.PhysicsBody.Dynamic <- false
   sprite
use grass = createLevelSprite "grass"

Adding a player

Firstly here’s the code to add the player:

 //Add a player, with physics which are affected by gravity and are dynamic
 use player = new SKSpriteNode("player")
 player.PhysicsBody <- SKPhysicsBody.BodyWithRectangleOfSize player.Size
 player.PhysicsBody.AffectedByGravity <- true
 player.PhysicsBody.AllowsRotation <- false
 player.PhysicsBody.Dynamic <- true
 player.Position <- PointF(320.f,100.f)
 scene.AddChild player

The interesting points are that

  1. We aren’t allowing rotation
  2. We are letting the player be dynamic (for now at least)

When we run this we have a little dude onscreen (once again from the Kenney game in the open.commonly.cc asset bundle).

To add control (and I heartily recommend you read and digest this post by Dave Thomas for a more complete view as to what is going on) we are simply going to allow an upwards swipe to apply an impulse to the player character. I’m not adding a more complex system in (yet) as I just want to demonstrate input and get us onto the next post which will add even more movement.

let swipeUp = new UISwipeGestureRecognizer(Direction=UISwipeGestureRecognizerDirection.Up)
swipeUp.AddTarget (fun () -> 
    if swipeUp.State = UIGestureRecognizerState.Ended && player.PhysicsBody.Velocity.dy = 0.f then
        player.PhysicsBody.ApplyImpulse (CGVector(0.0f, 200.f))) |> ignore

x.View.AddGestureRecognizer swipeUp

Let’s break this down a little as I lied (a little) when I said “Simply”:
The 1st line determines that we are adding a swipe for recognizing the up swipe.
The 2nd line adds function to be called when the up swipe happens.
Line 3 says – when the swipe is complete AND when the player’s Y velocity is 0 (for example when they are not falling) then
Line 4 – apply an upwards force
Line 6 – add the recognizer to the view – without this the previous 4 lines don’t do anything.

So line 3 checks that we’re not falling so that we only can jump from the ground. Without this check we’d be able to jump mid-air and that’s just CRAZY!

So there you have it – a jumping dude who can land on the floor and the moving platform if timed correctly.

Code – as always at Github with the specific changes in this file.

Next time

I’m going to make this little guy run left to right and scroll with him in our 1st big change to the structure of the code (and by big I mean really pretty small as we’re only up to around 150 LOC so far…).

Advertisements

About thedo666

Software developer trying to learn a new language - English!
This entry was posted in F#, iOS and tagged , , , . Bookmark the permalink.

One Response to A Platform game in SpriteKit and F# – Part 3 – A Jumping dude

  1. Pingback: F# Weekly #41, 2013 | Sergey Tihon's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s