Simple SpriteKit demo in F#

So, you may have noticed iOS 7 was released yesterday, and the nice folks over at Xamarin released an update to Xamarin.iOS less than a day later. Nice!

One of the nice features on iOS7 is Sprite Kit. This includes 2D sprites, Physics (using box2D) and Animation.

So to help you get going, heres how to get started in F#. This code is based on the Objective-C sample, so isn’t hugely interesting to look at, but may be enough to get started.

First create an Xamarin iOS F# SingleView application. Target iOS 7 in the project options.

open the MonoTouch.SpriteKit namespace. Add a png to the project under the “Resources” folder. Call it whatever you like, but make sure you name it the same in the code below.

Then replace the code in ViewDidLoad in the UICiewController with this

 override x.ViewDidLoad () =
         base.ViewDidLoad ()
         //Create and configure the view
         let skView = new SKView()
         skView.Bounds <- RectangleF(0.f,0.f, 
                                     x.View.Bounds.Width * UIScreen.MainScreen.Scale, 
                                     x.View.Bounds.Height * UIScreen.MainScreen.Scale)
         skView.ShowsFPS <- true
         skView.ShowsNodeCount <- true
         x.View <- skView

         //Create and configure the screen
         let scene =  new SKScene(skView.Bounds.Size)
         scene.BackgroundColor <- UIColor.Cyan
         scene.ScaleMode <- SKSceneScaleMode.AspectFill

         //Create a sprite
         let spriteNode = new SKSpriteNode "Spaceship"
         spriteNode.Position <- PointF(200.f,200.f)

         //Set a repeating 360 degree rotation
         let action = SKAction.RotateByAngle(float32 Math.PI,1.0) 
         let action = SKAction.RepeatActionForever action
         spriteNode.RunAction action

         //Add sprite to scene
         scene.AddChild spriteNode

         //Show scene
         skView.PresentScene scene

Running you should see something like this:

iOS Simulator Screen shot 19 Sep 2013 23.31.08

I’m a big advocate of Monogame and Farseer, and for cross-platform scenarios it is definitely the way to go, but if you only want to target iOS, then Sprite Kit may be worth a look.

Next I’ll take a look at integrating some simple physics……

About thedo666

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

4 Responses to Simple SpriteKit demo in F#

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

  2. Pingback: SpriteKit and Physics in F# | Neil Danson's Blog

  3. Pingback: A Platform game in SpriteKit and F# – Part 1 – Game State Management | Neil Danson's Blog

  4. Pingback: A Platform game in SpriteKit and F# – Part 2 – Start building a level | Neil Danson'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