Pages

Sunday, April 19, 2015

A simple space shooter: Part #2 Movement and Shooting


First right click and save the image below, name it "laser1.png", it will serve as the ships weapon projectiles.

Next add the following variable declarations to your GameScene.swift file.


Next add the following code to your GameScene.swift file, bellow the didMoveToView method.


Here is where you add the overrides to handle the touch inputs, this example is a control scheme that I did not come up with, where you touch the screen and the ship quickly moves to that position then you drag around and the ship follows, also you fire the weapon at the same time, for a mobile platform with touch screen only in my opinion the one touch controls are best to be able to enjoy the game, rather than be trying to push small buttons on screen.


Now add an important function that will be called to fire a projectile.


This function does the following:

  1. You create a SKSpriteNode that will be the projectile, then position it to the ship coordinates offset y by the ships height divided by 2 to make the laser ball come off the tip of the ship.

  2. Then the simple v = d/t which you rearrange for time is now t = d/v as you can see travelTime for the projectile is distance divided by speed, you can change the speed value to make to projectiles faster or slower, this calculation is done because if you move your ship through the screen the distance between the start point and the top of the screen changes and projectiles would fire slower as you go higher on the screen, this way the speed remains constant.

  3. Next you run a sequence of actions on the projectile so that it will travel to the set point at the top of the screen, and then it will remove itself from the scene.

  4. The timeSinceLastFire variable is there to control the rate of fire since this will get called by the update method of your GameScene which gets called before each frame, that means if the game is running well it would be 60 times in 1 second, here I add delta to this value then check if the appropriate time has passed before firing another projectile.


Last you make your update override function look like this:



Build and Run the project you should see the same scene only now you can touch and the ship move and shoots!

Here is the entire GameScene.swift file as it is after Step #2:


On the next step we will add something to shoot at because empty outer space can be boring.

Saturday, April 18, 2015

A simple space shooter: Part #1 Setup

Ever since my first time looking at code on a computer monitor i've wanted to write a space shooter, its as fun as playing those old school titles at the arcade. Actually every programmer i've come across mentions attempting to write a space shooter at some point.

So the following is my version of a really simple yet functional space shooter that later can be evolved into something really special. This first step is just creating the new project in Xcode and adding some code, along with using SpriteKit's particle effects to create a moving star field to simulate space ship motion.

From the menu click  File \ New \ Project...

Type in your space shooter name and of course select SpriteKit and Swift as the language.

On the next screen select where you would like to save your project and click Create.

After this steps you have a standard SpriteKit project which you can click Run and displays a gray scene with the words "Hello, World!".


So now to turn this into a star field with a ship.
Next you want to click on File \ New \ File...
On the screen Select Resource under iOS then select SpriteKit Particle File:

Next select Snow.


Next type in "StarField" as a name and click Create.

On your Navigator select the StarField.sks file.


In the StarField.sks file you need to change your emitter settings to match these.

  • Particle: 12 
  • Lifetime: 20 
  • Position Range: X: 420 Y: 5 
  • Angle: 270
  • Speed: Start: 80 Range: 120 
  • Alpha: Start:1 Range: 0.5 
  • Scale: Start: 0.04 Range: 0.01

Click anywhere in the black space and you will see the particle effect at work, these settings can be tweaked for a better effect if you want to, these are just baseline settings for a working star field.


Next you need to add the following code to your didMoveToView(view: SKView) method on your GameScene.swift file.

This code handles the following:
  1. Sets scene background color to black.

  2. Creates SKEmitterNode using the StarField.sks file, sets nodes position and zPosition and it's important to call advanceSimulationTime this makes sure you have stars when the scene shows up.

  3. Create the starship SKSpriteNode set it's scale and position.

  4. Add both nodes to the scene as children.


Now your GameScene.swift file should look like this.


You will notice that I deleted the code that displays the "Hello, World" message since its not needed. That is all for Part #1.

Now just Build and Run the project.

Your scene should look like this.