Swift 3,SpriteKit,如何使对象移动和消失并出现在随机点中

时间:2017-03-06 01:45:40

标签: swift xcode sprite-kit swift3 xcode8

我正在构建这个游戏需要帮助我正在尝试制作一个放置在视图中的对象以x轴移动,我希望更多这些对象出现在随机点中并从中移除游戏过去了,这就是我到目前为止所拥有的

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    let player = SKSpriteNode(imageNamed: "player")

    let floor = SKSpriteNode(imageNamed: "bottom")

        override func didMove(to view: SKView) {

        self.physicsWorld.gravity = CGVector(dx: 0, dy: -10)

        player.position = CGPoint(x: self.size.width/2 - 490, y: self.size.height/2 - 150)
       player.setScale(1.4)
        player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: player.size.width, height: player.size.height))
        player.physicsBody?.affectedByGravity = false
        self.addChild(player)

        floor.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 270)
        floor.setScale(2)
        floor.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: floor.size.width, height: floor.size.height))
        floor.physicsBody?.isDynamic = false
        floor.physicsBody?.affectedByGravity = false
        self.addChild(floor)
       createSteps()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        player.physicsBody?.affectedByGravity = true
        player.physicsBody?.applyForce(CGVector(dx: 0, dy: 3150))
        player.physicsBody?.velocity = (CGVector(dx: 0, dy: 0))

    }

   func createSteps() {

        let steps = SKSpriteNode(imageNamed: "object")

        steps.position = CGPoint(x: self.size.width/2 - 230 , y: self.size.height/2 - 100)
        steps.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: steps.size.width, height: steps.size.height))
        steps.physicsBody?.isDynamic = false
        steps.physicsBody?.affectedByGravity = false

        self.addChild(steps)

    }

   }

0 个答案:

没有答案