Chopra | iOS Game Development By Example | E-Book | www.sack.de
E-Book

E-Book, Englisch, 220 Seiten

Chopra iOS Game Development By Example

Learn how to develop an ace game for your iOS device, using Sprite Kit
1. Auflage 2025
ISBN: 978-1-78528-323-9
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)

Learn how to develop an ace game for your iOS device, using Sprite Kit

E-Book, Englisch, 220 Seiten

ISBN: 978-1-78528-323-9
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)



Game development has always been an exciting subject for game enthusiasts and players and iOS game development takes a big piece of this cake in terms of perpetuating growth and creativity. With the newest version of iOS and Sprite Kit, comes a series of breathtaking features such as Metal rendering support, camera nodes, and a new and improved Scene Editor.
Conceptualizing a game is a dream for both young and old. Sprite Kit is an exciting framework supported by Apple within the iOS development environment. With Sprite Kit, creating stunning games has become an easy avenue.
Starting with the basics of game development and swift language, this book will guide you to create your own fully functional game. Dive in and learn how to build and deploy a game on your iOS platform using Sprite Kit game engine.
Go on a detailed journey of game development on the iOS platform using the Sprite Kit game engine. Learn about various features implemented in iOS 8 that further increase the essence of game development using Sprite Kit. Build an endless runner game and implement features like physics bodies, character animations, scoring and other essential elements in a game. You will successfully conceive a 2D game along with discovering the path to reach the pinnacle of iOS game development.
By the end of the book, you will not only have created an endless runner game but also have in-depth knowledge of creating larger games on the iOS platform.
Style and approach An easy-to-follow, comprehensive guide that makes your learning experience more intriguing by gradually developing a Sprite Kit game. This book discusses each topic in detail making sure you attain a clear vision of the subject.

Chopra iOS Game Development By Example jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


Adding a sprite without using textures


Mostly in a game, we add texture to our sprite, but we can also make a sprite without using textures. A texture property is an optional property in the class. If texture is nil, that means we have no texture to stretch, so the contract parameter is ignored. Let's open our file and make a variable of , just below the declaration:

var spriteWithoutTexture : SKSpriteNode?

Now, with the preceding declaration, we have declared as optional. Since we have declared it optional, texture need not require a value. Now under , add following function:

func addSpriteWithoutTexture(){ spriteWithoutTexture = SKSpriteNode(texture: nil, color: UIColor.redColor(), size: CGSizeMake(100, 100)) addChild(spriteWithoutTexture!) }

After that, call this function inside , below the function:

addSpriteWithoutTexture()

Now tap on play and see what happens. In our there is no change. Well that's not what we desire. Actually, we missed the position of our texture. That's why it is rendering behind the background and not showing to us. Add this line in our function, before :

spriteWithoutTexture!.zPosition=1;

Run it. You will see a red square in the middle of the screen.

The code is self-explanatory. We made an instance of by instantiating it. We are passing nil as parameter for texture, meaning we don't want texture for this sprite. As we have made this sprite reference optional, we will have to unwrap it before using any properties, and we do so by using the mark after .

We can also initialize in another way. Delete the parameter from the initialization part:

spriteWithoutTexture = SKSpriteNode(texture: nil, color: UIColor.redColor(), size: CGSizeMake(100, 100))

Change the preceding initialization part as shown in the following:

spriteWithoutTexture = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 100))

Run the code and it will produce the same result as the previous one. It automatically assigns nil to texture, and initializes a sprite with a color and the specified bounds. Let's do something interesting with it.

Changing the color property


We are going to use property to change color when a user taps on this sprite. For this, first give a name to , so that we can recognize a tap on it:

spriteWithoutTexture!.name = "HELLO"

Add the following function in the file to change color, as shown in the following code:

var

Now, we use the function to detect touch by a user (as it was used previously in the class):

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { for touch: AnyObject in touches{ currentno = 0; func changeColor(){ switch(currentno%3){ case 0: spriteWithoutTexture!.color = UIColor.redColor() case 1: spriteWithoutTexture!.color = UIColor.greenColor() case 2: spriteWithoutTexture!.color = UIColor.blueColor() default : spriteWithoutTexture!.color = UIColor.blackColor() } } let location = touch.locationInNode(self) let node = self.nodeAtPoint(location) if node.name == spriteWithoutTexture!.name { currentno++ changeColor() } } }

Now, after running Xcode, click on the colorful area in . You will see that area changing its color.

In this code, when a user taps on the sprite, it will add a value to the current one and call the function. In the function, we have taken a case to determine the color property of . In Swift, case is used as in many other languages. We don't have to use the statement. Every statement must be . That means, we have to make every single case check for switch case. Hence, we have to write a value for every switch case.

If our texture is not nil, we can use the property to colorize the texture. We can use it for a tinting effect, such as damage taken in the game; is ignored if texture is nil. Its default value is , which means that the texture should remain unmodified. When we increase the value, texture color is replaced with the blended color.

Changing colorBlendFactor in MenuScene


Let's add a tint to our play button. Open and define a variable named inside the class as optional , so that we won't need to assign a value to it in the initializer:

var tintChanger : Float?

Add the following function in the class:

func tintPlayButton(){ if PlayButton.colorBlendFactor >= 1{ tintChanger = -0.02 } else if PlayButton.colorBlendFactor <= 0{ tintChanger = 0.02 } PlayButton.colorBlendFactor += CGFloat(tintChanger!) }

Call it from the function:

override func update(currentTime: NSTimeInterval) { tintPlayButton() }

Now run Xcode. You will see the Play button appearing and disappearing respectively.

In this code, we just make a type variable. In our function, we check if the value of its property is between to .

Now let's give it a color, inside the function:

PlayButton.color = UIColor.redColor()

Run it and you will see the Play button changing its color from the original one to reddish. Now, it's time to see the position property in action.

Changing the position of a sprite


Now, have a look at the property of . Let's open again, as we are going to see the property and the ways we can set it. Add this function below :

func changePosition(){ switch(currentno%3){ case 0: spriteWithoutTexture!.position = CGPointZero case 1: spriteWithoutTexture!.position = CGPointMake(self.size.width/2-spriteWithoutTexture!.size.width/2, 0) case 2: spriteWithoutTexture!.position = CGPointMake (-self.size.width/2+spriteWithoutTexture!.size.width/2, 0) default : spriteWithoutTexture!.position = CGPointMake(0, 0) } }

And call it just below the call.

changePosition()

Now if you will run it and tap inside your game scene, you will see changing its position and toggling between them.

The most part of the code is the same as in except the position. In , we set its position to . Position is measured in the unit. is equivalent to . The position of a sprite depends on its as well as its parent .

As we define to , it means any other node which will be added to will have the starting , from the middle of the screen. That's why the background and co-ordinate will be in middle of the screen.

Now, as we specified the of , it will take its default value of . This means that its will be in the center of it. Hence, in , it is rendering in the middle of the screen symmetrically. In and , we just moved it to the right middle corner and left middle corner of the screen.

Let's try to change and see what happens. Add this line inside :

spriteWithoutTexture!.anchorPoint = CGPointZero

Now run it.

Before tap

After tap

You will see that all the positions are not as they were before. Can you guess the reason for this?

In the preceding code line, we assigned the new value to , which will remove its default value . This means that its will not start from its middle. It will start from the bottom left of this. To visualize it, consider your sprite's top right corner as , and bottom left corner as . Now if you will set...


Chopra Samanyu :

Samanyu Chopra is a developer, entrepreneur, and Blockchain supporter with wide experience of conceptualizing, developing, and producing computer and mobile software's. He has been programming since the age of 11. He is proficient in programming languages such as JavaScript, Scala, C#, C++, Swift, and so on. He has a wide range of experience in developing for computers and mobiles. He has been a supporter of Bitcoin and blockchain since its early days and has been part of wide-ranging decentralized projects since a long time. You can write a tweet to him at @samdonly1.



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.