2016-04-02 55 views
0

我已經查看了所有的例子,我可以罰款,沒有任何事情與我的問題有關。我有SKPhysicsContactDelegate,並添加了self.physicsworld.contactDelegate = self。我幾乎完成了我能想到的所有事情。這是我的項目的所有代碼。didBeginContact函數不被稱爲

// 
// GameScene.swift 
// arrow jump 
// 
// Created by Joy Cafiero on 3/29/16. 
// Copyright (c) 2016 3rd Dimension Studios inc. All rights reserved. 
// 

import SpriteKit 

var timer = NSTimer() 
var condition = 1 
var arrow = SKSpriteNode() 
var man = SKSpriteNode() 
var bg = SKSpriteNode() 
var ground = SKSpriteNode() 
var buttonRight = SKSpriteNode() 
var buttonLeft = SKSpriteNode() 
var buttonJump = SKSpriteNode() 
let moveGrounRight = SKAction.moveByX(200, y: 0, duration: 1) 
let repeatMoveGroundRight = SKAction.repeatActionForever(moveGrounRight) 
let moveGrounLeft = SKAction.moveByX(-200, y: 0, duration: 1) 
let repeatMoveGroundLeft = SKAction.repeatActionForever(moveGrounLeft) 
let runningMan1 = (SKTexture (imageNamed: "running man1.png")) 
let runningMan2 = (SKTexture (imageNamed: "running man2.png")) 
let runningMan3 = (SKTexture (imageNamed: "running man3.png")) 
let runningMan4 = (SKTexture (imageNamed: "running man4.png")) 
let runningMan5 = (SKTexture (imageNamed: "running man5.png")) 
let runningMan6 = (SKTexture (imageNamed: "running man6.png")) 
let nuetralMan = (SKTexture (imageNamed: "running man nuetral.png")) 
let jumpingMan1 = (SKTexture (imageNamed: "jumping man1")) 
let jumpingMan2 = (SKTexture (imageNamed: "jumping man2")) 
let jumpingMan3 = (SKTexture (imageNamed: "jumping man3")) 

let animation = SKAction.animateWithTextures([ runningMan1, runningMan2,  runningMan3, runningMan4, runningMan5, runningMan6], timePerFrame: 0.15) 
let jumpingAnimation = SKAction.animateWithTextures([jumpingMan1, jumpingMan2,   jumpingMan3], timePerFrame: 0.1) 
let nuetralAnimation = SKAction.animateWithTextures([nuetralMan], timePerFrame: 0.05) 
let repeatAnimation = SKAction.repeatActionForever(animation) 

let manGroup:UInt32 = 0x1 << 1 
let groundGroup:UInt32 = 0x1 << 2 
let arrowGroup:UInt32 = 0x1 << 3 


class GameScene: SKScene, SKPhysicsContactDelegate { 

override func didMoveToView(view: SKView) { 
    /* Setup your scene here */ 

    self.physicsWorld.contactDelegate = self 
    self.physicsWorld.gravity = CGVectorMake(0, -5) 

    man = SKSpriteNode(texture: nuetralMan) 
    man.position = CGPointMake(CGRectGetMidX(self.frame),  CGRectGetMidY(self.frame)) 
    man.size = CGSize(width: man.size.width * 2, height: man.size.height * 2) 
    man.zPosition = 15 
    man.physicsBody = SKPhysicsBody(rectangleOfSize: man.size) 
    man.physicsBody?.dynamic = true 
    man.physicsBody?.allowsRotation = false 
    man.physicsBody?.categoryBitMask = manGroup 
    man.physicsBody?.contactTestBitMask = groundGroup | arrowGroup 

    self.addChild(man) 

    let bgTexture = (SKTexture(imageNamed: "wild west landscape.png")) 

    bg = SKSpriteNode(texture: bgTexture) 
    bg.size = CGSize(width: self.frame.width, height: self.frame.height) 
    bg.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 100) 
    bg.zPosition = -1 
    self.addChild(bg) 

    let groundtexture = (SKTexture(imageNamed: "sandy ground.png")) 

    ground = SKSpriteNode(texture: groundtexture) 
    ground.size = CGSize(width: CGRectGetMaxX(self.frame), height: ground.size.height) 
    ground.position = CGPointMake(CGRectGetMidX(self.frame), 0) 
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, ground.size.height-255)) 
    ground.physicsBody?.dynamic = false 
    ground.zPosition = 20 
    ground.physicsBody?.categoryBitMask = groundGroup 
    ground.physicsBody?.contactTestBitMask = manGroup 


    self.addChild(ground) 

    timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("timerUpdate"), userInfo: nil, repeats: true) 

    buttonRight.color = SKColor.redColor() 
    buttonRight.position = CGPointMake(200, 200) 
    buttonRight.size = CGSize(width: 100, height: 100) 
    buttonRight.zPosition = 25 

    self.addChild(buttonRight) 

    buttonLeft.color = SKColor.redColor() 
    buttonLeft.position = CGPointMake(100, 200) 
    buttonLeft.size = CGSize(width: 100, height: 100) 
    buttonLeft.zPosition = 25 

    self.addChild(buttonLeft) 

    buttonJump.color = SKColor.greenColor() 
    buttonJump.position = CGPointMake(CGRectGetMaxX(self.frame) - 200, 200) 
    buttonJump.size = CGSize(width: 100, height: 100) 
    buttonJump.zPosition = 25 

    self.addChild(buttonJump) 


} 

func timerUpdate() { 
    let random = Int(arc4random_uniform(3)) 
    var timeIncrease:NSTimeInterval = 0 
    var randomArrow = [self.frame.height/20 + CGRectGetMidY(self.frame) - 65, self.frame.height/20 + self.frame.height/20 + CGRectGetMidY(self.frame) - 65, self.frame.height/20 + self.frame.height/20 + self.frame.height/20 + CGRectGetMidY(self.frame) - 65, self.frame.height/20 + self.frame.height/20 + self.frame.height/20 + CGRectGetMidY(self.frame) - 65] 
    timeIncrease = timeIncrease + 7 
    let arrowTexture = (SKTexture(imageNamed: "arrow.png")) 
    let movingArrow = SKAction.moveByX(-self.frame.size.width, y: 0, duration: 10-timeIncrease) 
    let removeArrows = SKAction.removeFromParent() 
    let repeatmoveArrows = SKAction.sequence([movingArrow, removeArrows]) 
    arrow = SKSpriteNode(texture: arrowTexture) 
    arrow.size = CGSize(width: arrow.size.width, height: arrow.size.height) 
    arrow.position = CGPointMake(CGRectGetMaxX(self.frame), randomArrow[random]) 
    arrow.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(arrowTexture.size().width - 100, arrow.size.height/2)) 
    arrow.physicsBody?.dynamic = false 
    arrow.zPosition = 10 
    arrow.physicsBody?.categoryBitMask = arrowGroup 
    arrow.physicsBody?.contactTestBitMask = manGroup 
    arrow.runAction(repeatmoveArrows) 

    self.addChild(arrow) 

} 


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    /* Called when a touch begins */ 
    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     if buttonRight.containsPoint(location) { 
      man.xScale = 1 
      man.runAction(repeatMoveGroundRight, withKey: "MoveRight") 
      man.runAction(repeatAnimation, withKey: "Run") 
     } 

     if buttonLeft.containsPoint(location) { 
      man.xScale = -1 
      man.runAction(repeatMoveGroundLeft, withKey: "MoveLeft") 
      man.runAction(repeatAnimation, withKey: "Run") 
     } 

     if buttonJump.containsPoint(location) { 
      if(condition == 1) { 
      man.physicsBody?.velocity = CGVectorMake(0, 0) 
      man.physicsBody?.applyImpulse(CGVectorMake(0, 400)) 
      man.runAction(jumpingAnimation) 
       condition = 0 
      } 
     } 


} 
} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     if buttonRight.containsPoint(location) { 
      man.removeActionForKey("MoveRight") 
      man.removeActionForKey("Run") 
      man.texture = nuetralMan 
     } 
     if buttonLeft.containsPoint(location) { 
      man.removeActionForKey("MoveLeft") 
      man.removeActionForKey("Run") 
      man.texture = nuetralMan 
     } 

    } 
} 


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     if buttonRight.containsPoint(location) { 
      man.removeActionForKey("MoveRight") 
      man.removeActionForKey("Run") 
      man.texture = nuetralMan 
     } 
     if buttonLeft.containsPoint(location) { 
      man.removeActionForKey("MoveLeft") 
      man.removeActionForKey("Run") 
      man.texture = nuetralMan 
     } 

    } 

    func didBeginContact(contact: SKPhysicsContact) { 

     var firstBody: SKPhysicsBody 
     var secondBody: SKPhysicsBody 
     var thirdBody: SKPhysicsBody 

     if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
      firstBody = contact.bodyA 
      secondBody = contact.bodyB 
     } else { 
      firstBody = contact.bodyB 
      secondBody = contact.bodyA 
     } 

     if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
      firstBody = contact.bodyA 
      thirdBody = contact.bodyB 
     } else { 
      firstBody = contact.bodyB 
      thirdBody = contact.bodyA 
     } 


     if firstBody.categoryBitMask == manGroup || secondBody.categoryBitMask == groundGroup { 
      print("contact") 
      condition = 1 
      man.texture = nuetralMan 

     } 

     if firstBody.categoryBitMask == manGroup || thirdBody.categoryBitMask == arrowGroup { 
      print("dead") 
     } 
    } 


func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
} 
} 
} 

我試圖在didBeginContact方法中打印東西,但沒有任何反應。我花了3天試圖弄清楚這一點。據我所知,我所有的categoryBitMasks和CollisionTestBitMask都是正確設置的。任何幫助是非常讚賞。

+0

你的箭頭有虛假的動態,所以除非你的男人移動到箭頭上,否則不會註冊 – Knight0fDragon

+0

是的,男人必須跳過箭頭所以是的,當玩家混亂時他會被箭頭擊中所以箭頭不是動態的並不是問題。 moveByX SKAction使箭頭在屏幕上移動,NSTimer以5秒的速率產生它們。這不是真的問題,認爲問題是我的didBeginContact函數不工作,我不知道爲什麼。 –

回答

1

您的didBeginContact()的定義被隱藏在touchesEnded()函數中。如果你理順你的支撐,它可能會幫助:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch: AnyObject in touches { 
     // for touches loop (skipped) 
    } 
} // Add this closing brace 

    func didBeginContact(contact: SKPhysicsContact) { 
     // contact body (skipped) 
    } 

而且你還需要更新後,刪除多餘的括號():

// ...skipped 

func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
} 
// } // get rid of this one 
} 

希望有所幫助。

+0

哇謝謝這麼多人,你甚至不知道你幫了我多少,我甚至不知道我是否可以感謝你。你是一個真正的英雄。 –