2017-06-21 48 views
-1

我正在製作一個遊戲,假設球穿過一些管道,當球接觸到管道時,遊戲停止。有點像飛揚的鳥。我遇到的唯一問題是初始管道堵塞整個屏幕,而其餘管道按照我的意願放置並隨機化。這怎麼可能?不同於以下管道的初始管道

這是Random類:

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) 
} 

} 
+0

你能向我們展示一個問題的圖像嗎?我有一個難以理解的情況。 – sicvayne

+0

另外,你有沒有嘗試從初始化函數中刪除createPipes? – sicvayne

+0

你是傳奇人物!完全做到了! – Flinigan

回答

0

以供將來參考的基礎上,談話我們在評論中,他所要做的就是從內部刪除他的createPipes函數他的initialize功能。