In the last section, we completed the creation of the platform. So let's make the platform move. Platform move, we just need to write the moving method in the Platform factory class, and then control the Gamescene class uniformly.
In the Gamescene class, there is an update method that is called every time, to control the movement of the platform.
First, we define a moving speed in the Gamescene class:
Move speed var movespeed:cgfloat = 15
Then call the Move method of the Platform factory class in the Update method method:
Override func Update (currenttime:cftimeinterval) {platformfactory.move (self.movespeed)}
From the code above, we can see that the name of the mobile method to be defined in the Platform factory class is called move, and the received parameter is movespeed. So let's switch to the PlatformFactory class to implement it.
Func Move (speed:cgfloat) {//Traverse all platforms for p in platforms{//x coordinate changes Phoenix horizontal motion animation p.position.x = Speed}}
In the Move method, we traverse all platforms in the Platform factory class and then set the platform's x-coordinate changes. The motion of the platform is animated by the constant change of the X coordinate.
Run the program, we can see the screen before the beginning of the platform to start like a move, move away from the run disappeared ...
9.8 generate a steady stream of mobile platforms
Just now we threw a platform into the game scene, we have to throw a lot of platform into the game. So how do you build these platforms? Logically, the platform enters the game from the right side of the screen, creating a new platform when the platform fully enters the game scene, and by a certain interval.
When the platform completely moves out of the game scene, we want to remove it. Removed from the scene, removed from the array of platform factory classes.
The logic is over, let's see how it's done in the code.
Then let's transform the Flatformfactory class. First of all, we need to know how far away the platform will be before it can fully enter the game scene. The formula for this distance: the width of the platform + the x-coordinate of the platform-the width of the game scene. So you need to define a variable to store the width of the game scene:
Game scene width var scenewidth:cgfloat = 0
Then we'll pass the distance back to Gamescene, and in the Gamescene Update method with the move speed movespeed to determine if a new platform needs to be created. When it comes to data transfer, define the protocol implementation agent, then the protocol is:
Defines a protocol to receive data protocol Protocolmainscene { func ongetdata (dist:cgfloat)}
And the agent is:
Protocolmainscene agent var delegate:protocolmainscene?
Next we'll write a new way to create a random location platform. Of course, the previous Createplatform method is called in the method. The method name is Createplatformrandom. and call the agent's Ongetdata to pass the platform completely into the game scene distance to Gamescene.
Method of generating a random position platform Func createplatformrandom () {///random platform length let Midnum:uint32 = Arc4random ()%4 + 1//random interval let gap:cgfloat = CGFloat (Arc4random ()%8 + 1)//random x-coordinate let x:cgfloat = Self.scenewidth + cgfloat (midnum*50) + gap + 100//random y-coordinate let y:cgfloat = CGFloat (Arc4random ()%200 + +) let platform = Self.createplatform (True, Midnum:midnum, x:x, y:y)//return distance used to determine when to generate a new Platform for delegate?. Ongetdata (Platform.width + x-scenewidth) }
After this is done, we switch to the Gamescene class to continue coding: first let Gamescene follow the Protocolmainscene protocol, define a variable, and receive the variable that the platformfactory passed over:
var lastdis:cgfloat = 0.0
The width of the game scene is then passed to the Platform factory class instance in the Didmovetoview method, and the proxy is set to the Platform factory class instance:
To upload the width of the screen to the platform factory class Platformfactory.scenewidth = self.frame.width//Set proxy platformfactory.delegate = self
After these completion we want to use Lastdis in the update to constantly subtract the speed of the platform movement, when the Lastdis small equals 0 o'clock, the platform fully into the game scene, can Phoenix new class.
Lastdis-= Movespeedif Lastdis <= 0 {platformfactory.createplatformrandom ()} Finally, do not forget the method of implementing the Protocolmainscene protocol: func Ongetdata (dist:cgfloat) {Self.lastdis = dist }
Run the program, you can already see the game screen on the beginning of a steady stream of platform.
My public number.
I wrote a book: "Swift language Combat Promotion" http://item.jd.com/11641501.html
Swift language Combat Promotion-9th chapter game-Parkour Panda-7-8 mobile platform algorithm