發表文章

目前顯示的是 12月, 2018的文章

Swift Sprite Kit add Tile Crash

#Swift Sprite Kit# ##Add Tile to sks crash## when you add image to Assets.xcassets then you go to your GameScene.sks add Tile Map Node every time XCODE crash. you can close your project first and goto your project folder only open your Map.sks and add image to sks file, when add completed save file and open project, then you can watch your map image and edit.

Swift Sprite Kit easy to create bullet

圖片
#Swift Sprite Kit# ##Easy create bullet## How to easy create bullet like picture 1. Create SkSpriteNode ``` var bullet = SKSpriteNode() ``` 2. Add SKSpriteNode to View ``` func bulletFire(){ bullet = SKSpriteNode(color: UIColor.green, size: CGSize(width: 5, height: 50)) bullet.name = "fireBullet" addChild(bullet) bullet.zPosition = 2 let move = SKAction.moveTo(y: self.frame.height, duration: 1) bullet.run(move) } ``` 3. Set remove Node after Node leave View ``` self.enumerateChildNodes(withName: "fireBullet") { (node : SKNode, nil) in if node.position.y > self.frame.height / 2 { node.removeFromParent() } } ``` 4. Add Node to TouchesBegin ``` override func touchesBegan(_ touches: Set , with event: UIEvent?) { for t in touches { self.touchDown(atPoint: t.location(in: self)) var pos : CGPoint = t.location(in: self) bulletFire() } } ```

Swift SpritKit : background image move

圖片
#Swift Spirte Kit# ##Background Image move# 如何連續移動背景圖,先看原理 先將 img-1 從 view 之中移動到 view 的下方直到看不到在移動回到 view 之中 再設定一組 img-2 從 view 的上面移動到 view 之中再回到 view 的上方 記住兩組的移動時間必須一至的 如何開始 宣告兩組變數 ``` var background = SKSpriteNode() var background02 = SKSpriteNode() ``` 設定第一組圖片 ``` background = SKSpriteNode(imageNamed: "image name") background.size = CGSize(width: self.size.width, height: self.size.height) background.position.y = self.size.height background.zPosition = -1 self.addChild(background) let move1 = SKAction.moveTo(y: self.frame.midY, duration: 30) let move2 = SKAction.moveTo(y: self.size.height, duration: 0) let seq = SKAction.sequence([move1,move2]) background.run(SKAction.repeatForever(seq)) ``` 設定第二組圖片 ``` background02 = SKSpriteNode(imageNamed: "country-platform-back") background02.size = CGSize(width: self.size.width, height: self.size.height) background02.zPosition = -1 background02.position.y = self.frame.midY //+ background02.size.height self

Swift Sprite Kit : SKShapeNode of circle

#Swift Sprite Kit# ##SKShapeNode of circle## I has create new circle on the Sprite Kit How to Create circle on Sprite Kit 1. Setup SKShapeNode ``` var newSprit = SKShapeNode() ``` 2. Create circle ``` newSprit = SKShapeNode(circleOfRadius: 25) self.addChild(newSprit) ``` 3. Setup Physicsbody ``` newSprit.physicsBody = SKPhysicsBody(circleOfRadius: 25) ``` Full Code : ``` var newSprit = SKShapeNode() func createNewSprite(point : CGPoint) { newSprit = SKShapeNode(circleOfRadius: 25) newSprit.strokeColor = UIColor.orange newSprit.fillColor = UIColor.orange newSprit.name = "TEST" newSprit.physicsBody = SKPhysicsBody(circleOfRadius: 25) self.addChild(newSprit) } ```

Switf Sprite remove NODE

#Swift Sprite Kit# ## Remove Node, if Node leave View## set ``` var newSprit = SKSpriteNode() ``` create function for Node create ``` func createNewSprite(point : CGPoint) { newSprit = SKSpriteNode(color: UIColor.orange, size: CGSize(width: 50, height: 50)) newSprit.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50)) newSprit.position = point self.addChild(newSprit) } ``` on ```override func update(_ currentTime: TimeInterval)``` add new code ``` self.enumerateChildNodes(withName: String, using: (SKNode, UnsafeMutablePointer) -> Void) ``` check your View first, if your View setup by Portrait, you may check your node leave view by point X if your View setup by Landscape, you may check your node leave view by point y Code: ``` self.enumerateChildNodes(withName: "TEST") { (node : SKNode, nil) in if node.position.x < -320 || node.position.x > 320 { print("remove node")