初始管道与以下管道不同

时间:2017-06-21 09:34:27

标签: swift sprite-kit

我正在制作一个游戏,其中球被认为要通过一些管道,当球接触管道时,游戏停止。有点像飞扬的鸟。我唯一的问题是初始管道阻塞了整个屏幕,而其余的管道则按照我的要求放置和随机化。这怎么可能?

这是随机类:

import Foundation
import CoreGraphics

public extension CGFloat {

public static func randomBetweenNumbers(firstNum: CGFloat, secondNum: 
CGFloat) -> CGFloat {

    return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - 
secondNum) + firstNum

}


}

这是玩家类:

import SpriteKit

struct ColliderType {
static let Ball: UInt32 = 1
static let Pipes: UInt32 = 2
static let Score: UInt32 = 3
}

class Ball: SKSpriteNode {

func initialize() {
    self.name = "Ball"
    self.zPosition = 1
    self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.height / 
2)
    self.setScale(0.7)
    self.physicsBody?.affectedByGravity = false
    self.physicsBody?.categoryBitMask = ColliderType.Ball
    self.physicsBody?.collisionBitMask = ColliderType.Pipes
    self.physicsBody?.contactTestBitMask = ColliderType.Pipes | 
ColliderType.Score
}
}

这是GameplayScene:

import SpriteKit

class GameplayScene: SKScene {

var ball = Ball()

var pipesHolder = SKNode()

var touched: Bool = false

var location = CGPoint.zero

override func didMove(to view: SKView) {
    initialize()
}

override func update(_ currentTime: TimeInterval) {
    moveBackgrounds()

    if (touched) {
        moveNodeToLocation()
    }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: 
UIEvent?) {
    touched = true
    for touch in touches {
        location = touch.location(in:self)
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: 
UIEvent?) {
    touched = false
}

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

    for touch in touches {
        location = touch.location(in: self)
    }
}

func initialize() {
    createBall()
    createBackgrounds()
    createPipes()
    spawnObstacles()
}

func createBall() {
    ball = Ball(imageNamed: "Ball")
    ball.initialize()
    ball.position = CGPoint(x: 0, y: 0)
    self.addChild(ball)
}

func createBackgrounds() {

    for i in 0...2 {
        let bg = SKSpriteNode(imageNamed: "BG")
        bg.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        bg.zPosition = 0
        bg.name = "BG"
        bg.position = CGPoint(x: 0, y: CGFloat(i) * bg.size.height)
        self.addChild(bg)
    }
}

func moveBackgrounds() {

    enumerateChildNodes(withName: "BG", using: ({
        (node, error) in

        node.position.y -= 15

        if node.position.y < -(self.frame.height) {
            node.position.y += self.frame.height * 3
        }

    }))

}

func createPipes() {
    pipesHolder = SKNode()
    pipesHolder.name = "Holder"

    let pipeLeft = SKSpriteNode(imageNamed: "Pipe")
    let pipeRight = SKSpriteNode(imageNamed: "Pipe")

    pipeLeft.name = "Pipe"
    pipeLeft.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    pipeLeft.position = CGPoint(x: 350, y: 0)
    pipeLeft.xScale = 1.5
    pipeLeft.physicsBody = SKPhysicsBody(rectangleOf: pipeLeft.size)
    pipeLeft.physicsBody?.categoryBitMask = ColliderType.Pipes
    pipeLeft.physicsBody?.affectedByGravity = false
    pipeLeft.physicsBody?.isDynamic = false

    pipeRight.name = "Pipe"
    pipeRight.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    pipeRight.position = CGPoint(x: -350, y: 0)
    pipeRight.xScale = 1.5
    pipeRight.physicsBody = SKPhysicsBody(rectangleOf: pipeRight.size)
    pipeRight.physicsBody?.categoryBitMask = ColliderType.Pipes
    pipeRight.physicsBody?.affectedByGravity = false
    pipeRight.physicsBody?.isDynamic = false

    pipesHolder.zPosition = 5

    pipesHolder.position.y = self.frame.height + 100
    pipesHolder.position.x = CGFloat.randomBetweenNumbers(firstNum: 
-250, secondNum: 250)

    pipesHolder.addChild(pipeLeft)
    pipesHolder.addChild(pipeRight)

    self.addChild(pipesHolder)

    let destination = self.frame.height * 3
    let move = SKAction.moveTo(y: -destination, duration: 
TimeInterval(10))
    let remove = SKAction.removeFromParent()

    pipesHolder.run(SKAction.sequence([move, remove]), withKey: "Move")

}

func spawnObstacles() {
    let spawn = SKAction.run({ () -> Void in
        self.createPipes()
    })

    let delay = SKAction.wait(forDuration: TimeInterval(1.5))
    let sequence = SKAction.sequence([spawn, delay])

    self.run(SKAction.repeatForever(sequence), withKey: "Spawn")
}

func moveNodeToLocation() {
    // Compute vector components in direction of the touch
    var dx = location.x - ball.position.x
    // How fast to move the node. Adjust this as needed
    let speed:CGFloat = 0.1
    // Scale vector
    dx = dx * speed
    ball.position = CGPoint(x:ball.position.x+dx, y: 0)
}

}

1 个答案:

答案 0 :(得分:0)

为了将来参考,根据我们在评论中的对话,他所要做的就是从他的createPipes函数中删除他的initialize函数。