11.spriteKit Journey--The carnival of the knot at the end of the story
This is the end of the Sprite Kit Journey , which is just the beginning, of course. In addition sprite Kit
, I have completed a teaching software. Hope a lot of support.
Section directory:
01. About Sprite_kit
02.SpriteKit Forward View scene
03.sprite_kit-looking Tree of nodes
04.sprite_kit-looking textures
05.sprite_kit forward-looking node animations
06.sprite_kit to create a "real" world
07.spritekit Journey--Create the first scene
08.spritekit Journey--animation in the scene
09.spritekit Journey--The scene switch
10.spritkit Journey--use nodes to add content
11.spriteKit Journey--The carnival of the knot at the end of the story
In addition, my Spritekit Note app has been listed in the store for free, no in-app purchases, no ads, real-time updates to document content and code. Welcome to download, link address: https://itunes.apple.com/cn/app/spritekit-note/id973093466?mt=8
The end of the introductory chapter, followed by the wizard advanced, is expected to start in one months, but the app will be put in advance, I hope you support the download after the praise.
Business:
With the knowledge we have now, we can create multiple scenes, multiple elements, and nodes to perform their actions, which looks great, at least a shot. However, in the actual game development, many nodes must interact with each other in order to wipe out gorgeous sparks. This article will follow the example above to demonstrate that the task is to add a new node nodes
(for now I believe that many comrades from the beginning should understand the meaning of the node) into the scene, and then we will use the physical system to simulate their operation and execution of the collision effect. Very cool stuff!
Action and physic World Discussion
The Sprite Kit Framework provides a complete set of physics engines to simulate a near-real world, where the physical properties of the nodes can be automatically executed! Of course, we've talked about actionbefore, and by designing a set of actions for the node to execute, of course, it's good. Now with the physicWorld, the burden is greatly reduced, such as you only need to configure the quality of the node, the initial speed and so on (and real life associated with) attributes, finished into the physical universe can be automatically executed, if the node and the node has a collision between, The physical system detects and then automatically calculates and executes the results. Very good!
Demo, add physical simulations to the spaceship scene
Find newSpaceship
The method, add a property physics body
called, as the name implies is the physical body, there is a series of properties, such as quality, speed, acceleration, and adding it SKNode
to the spaceship instance, seeked is in There is a property called physicsBody
, which SKPhysicsBody
belongs to the class. First it is an optional type ( ?
), assuming that the default is not initialized nil
. Now add a physical body to our spaceship.
hull.physicsBody = SKPhysicsBody(rectangleOfSize: hull.size)
Try to build and Run, and you'll find "bie~" the ship has fallen .... Because the physical body is added, it drops naturally under the action of gravity. In addition, the previous Action was executed, but still cannot stop the drop ...
How can a spaceship fall? So we're going to stop it.
hull.physicsBody.dynamic = false
So when we run, the spacecraft is no longer affected by gravity and can work as before. Throw a question: Why do you add a physical body to it as before? Good question! Because then we have to deal with collisions! Whenever nodes interact between nodes, we have to give them physical ( physics body
). Here we force the hull physics to be static, relative to the dynamic! , so even if there is a collision will not affect the speed of the spacecraft pull!
-
To find the createscenecontents
function, let's add the code that produces the rock ( Spawn
, which is often used later), You know, the universe is full of crises! The
//spaceshipscene scene creates a steady stream of rocks!! First define the action, which is the execution of the rock function! Let makerocks = Skaction.sequence ([Skaction.runblock (Addrock), Skaction.waitforduration (0.10, withrange:0.15)]) The. Runaction (Skaction.repeatactionforever (makerocks))
Scene is also a node. Nature can also run a set of actions! What the above Action
Code does is call a Addrock
method, and it is called at intervals, and of course a rock is produced, and we have to wait for a while, But the waiting time is random (0.10±0.15). Emphasize! This uses the Runblock () closure method, which differs from the official document
Implementation addRock
method
//都是随机函数 没啥好看func skRandf()->CGFloat{ return CGFloat(CGFloat(random()) / CGFloat(RAND_MAX))}func skRand(low:CGFloat,high:CGFloat)->CGFloat{ return skRandf() * (high - low) + low}//着重看这里!func addRock(){ //设定精灵颜色 和 大小 let rock = SKSpriteNode(color: SKColor.brownColor(), size: CGSizeMake(8, 8)) //产生位置是随机的哦 rock.position = CGPointMake(skRand(0,high: self.size.width), self.size.height-50) //名字 rock.name = "rock" //设定物理体 这样就能自由落体喽 rock.physicsBody = SKPhysicsBody(rectangleOfSize: rock.size) rock.physicsBody?.usesPreciseCollisionDetection = true //添加到场景中 self 就是spaceshipScene 场景 self.addChild(rock)}
Build and run!! Under the rock rain, this I personally run a demo, feasible!
Achieve collisions! didSimulatePhysics
method is added to the scene! When the rock falls off the screen, remove it from the scene!
override func didSimulatePhysics() { self.enumerateChildNodesWithName("rock", usingBlock: { node,stop in if node.position.y < 0{ node.removeFromParent() } })}
- Curtain Call Temporarily, do not understand please blog message!
11.spritekit Journey--The carnival of the knot at the end of the story