2017-10-21 121 views
0

enter image description here循環我的代碼,使用戶可以繼續玩

import SpriteKit 
import GameplayKit 

class GameScene: SKScene, SKPhysicsContactDelegate { 


let balls = [ 
    SKSpriteNode(imageNamed: "blueball.png"), 
    SKSpriteNode(imageNamed: "greenball.png"), 
    SKSpriteNode(imageNamed: "realredball.png"), 
    ] 
let redRectangle = SKSpriteNode(imageNamed: "redrectangle.png") 
let blueRectangle = SKSpriteNode(imageNamed: "bluerectangle.png") 
let greenRectangle = SKSpriteNode(imageNamed: "greenrectangle.png") 
let wall1 = SKSpriteNode(imageNamed: "drop_wall.png") 
let wall2 = SKSpriteNode(imageNamed: "drop_wall.png") 
let bottom = SKSpriteNode(imageNamed:"drop_bottom.png") 
let top = SKSpriteNode(imageNamed:"drop_bottom.png") 

let blueBallCategory :UInt32 = 0x1 << 0 
let greenBallCategory :UInt32 = 0x1 << 1 
let realRedBallCategory :UInt32 = 0x1 << 2 
let redRectangleCategory : UInt32 = 0x1 << 3 
let blueRectangleCategory : UInt32 = 0x1 << 4 
let greenRectangleCategory : UInt32 = 0x1 << 5 


override func didMove(to view: SKView) { 
    spawnBalls() 
    spawnRectangles() 
    physicsWorld.contactDelegate = self 
    moveRectangles() 
} 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for ball in balls{ 
     ball.isUserInteractionEnabled = false 
    } 

    physics() 
     } 
func didBegin(_ contact: SKPhysicsContact) { 




    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask 


    switch contactMask { 

    case blueBallCategory | blueRectangleCategory: 
     for ball in balls{ 
      ball.removeFromParent() 





     } 




     print("Alive! Blue ball has hit blue rectangle.") 

    case greenBallCategory | greenRectangleCategory: 

     print("Alive! Green ball has hit green rectangle.") 

    case realRedBallCategory | redRectangleCategory: 

     print("Alive! Red ball has hit red rectangle.") 

    case blueBallCategory | redRectangleCategory: 

     print("dead") 

    case blueBallCategory | greenRectangleCategory: 

     print("dead") 

    case realRedBallCategory | blueRectangleCategory: 

     print("dead") 

    case realRedBallCategory | greenRectangleCategory: 

     print("dead") 

    case greenBallCategory | redRectangleCategory: 

     print("dead") 

    case greenBallCategory | blueRectangleCategory: 

     print("dead") 

    default: 

     print("missed") 
    } 



} 

func spawnRectangles() { 

    redRectangle.position = CGPoint(x: 0, y: -400) 
    redRectangle.size = CGSize(width: 200, height: 20) 
    blueRectangle.position = CGPoint(x: -300, y: -200) 
    blueRectangle.size = CGSize(width: 200, height: 20) 
    greenRectangle.position = CGPoint(x: 100, y: -550) 
    greenRectangle.size = CGSize(width: 200, height: 20) 
    self.addChild(redRectangle) 
    self.addChild(blueRectangle) 
    self.addChild(greenRectangle) 
    wall1.position = CGPoint(x: -367.04, y: 0) 
    wall1.size = CGSize(width: 20, height: 1350) 
    wall1.physicsBody = SKPhysicsBody(rectangleOf: wall1.size) 
    wall1.physicsBody?.isDynamic = false 
    wall1.physicsBody?.affectedByGravity = false 
    self.addChild(wall1) 
    wall2.position = CGPoint(x: 367.04, y: 0) 
    wall2.size = CGSize(width: 20, height: 1350) 
    wall2.physicsBody = SKPhysicsBody(rectangleOf: wall2.size) 
    wall2.physicsBody?.isDynamic = false 
    wall2.physicsBody?.affectedByGravity = false 
    self.addChild(wall2) 
    top.position = CGPoint(x: 0, y: 657) 
    top.size = CGSize(width: 765, height: 20) 
    top.physicsBody = SKPhysicsBody(rectangleOf: top.size) 
    top.physicsBody?.isDynamic = false 
    top.physicsBody?.affectedByGravity = false 
    self.addChild(top) 
    bottom.size = CGSize(width: 765, height: 20) 
    bottom.position = CGPoint(x: 0, y: -657) 
    bottom.physicsBody = SKPhysicsBody(rectangleOf: bottom.size) 
    bottom.physicsBody?.isDynamic = false 
    bottom.physicsBody?.affectedByGravity = false 
    self.addChild(bottom) 

} 
func physics(){ 
    for ball in balls{ 
     ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2) 
     ball.physicsBody?.contactTestBitMask = blueRectangleCategory | greenRectangleCategory | redRectangleCategory 
       } 
    redRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size) 
    redRectangle.physicsBody?.affectedByGravity = false 
    redRectangle.physicsBody?.isDynamic = false 
    blueRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size) 
    blueRectangle.physicsBody?.affectedByGravity = false 
    blueRectangle.physicsBody?.isDynamic = false 
    greenRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size) 
    greenRectangle.physicsBody?.isDynamic = false 
    greenRectangle.physicsBody?.affectedByGravity = false 
    balls[0].physicsBody?.categoryBitMask = blueBallCategory 
    balls[1].physicsBody?.categoryBitMask = greenBallCategory 
    balls[2].physicsBody?.categoryBitMask = realRedBallCategory 
    redRectangle.physicsBody?.categoryBitMask = redRectangleCategory 
    blueRectangle.physicsBody?.categoryBitMask = blueRectangleCategory 
    greenRectangle.physicsBody?.categoryBitMask = greenRectangleCategory 

      } 
func moveRectangles(){ 
    let redMoveRight = SKAction.moveTo(x: 300, duration: 2) 
    let redMoveLeft = SKAction.moveTo(x: -280, duration: 2) 
    let redWholeMovement = SKAction.repeatForever(SKAction.sequence([redMoveRight,redMoveLeft])) 
    redRectangle.run(redWholeMovement) 
    let blueMoveRight = SKAction.moveTo(x: 300, duration: 2) 
    let blueMoveLeft = SKAction.moveTo(x: -280, duration: 1.5) 
    let blueWholeMovement = SKAction.repeatForever(SKAction.sequence([blueMoveRight,blueMoveLeft])) 
    blueRectangle.run(blueWholeMovement) 
    let greenMoveRight = SKAction.moveTo(x: 300, duration: 2) 
    let greenMoveLeft = SKAction.moveTo(x: -280, duration: 1.5) 
    let greenWholeMovement = SKAction.repeatForever(SKAction.sequence([greenMoveLeft,greenMoveRight])) 
    greenRectangle.run(greenWholeMovement) 






} 
func spawnBalls(){ 
    let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))] 
    ball.position = CGPoint(x: 0, y: 250) 
    ball.size = CGSize(width: 70, height: 70) 
    self.addChild(ball) 
} 

} 

我想要一個新的球在屏幕頂部產卵,如果球擊中相同顏色的矩形。現在,當我運行應用程序時,頂部會產生一個隨機着色的球,當用戶單擊球滴時。如果球接觸到相同顏色球的移動矩形,則遊戲應該繼續進行。但它只是在以後結束。如果有人能幫上忙,那就太好了。謝謝!

+0

您確定碰撞正在計數嗎?打印語句是否出現在控制檯中? –

+0

是的一切工作正常我只是無法弄清楚如何讓遊戲繼續 –

+0

像重啓功能? –

回答

0

您只在spawnBalls()中產生了一球。所以當你觸摸時只會出現一個球。你應該嘗試將spawnBalls()移動到touchesBegan()。