2016-07-14 3 views
0

게임을 만들고 싶습니다. Xcode SpriteKit 샘플러에서 스트라이프 처리했습니다. 매우 간단합니다. 이 중요한 문제를 해결할 때 크게 발전 할 것입니다. 그것은 플레이어, 벽 및 문을 가지고 있습니다. 노드가 할당되고 플레이어가 올바르게 작동합니다. 벽은 자아를위한 아이들을 위해 시도했지만 내 의견이 삭제되면서 충돌합니다. 같은 이름의 노드가 여러 개 있다고 생각하십니까? 그러나 노드가 할당 될 때 어떤 이유로 천천히 떨어지더라도 중력이 가해지지 않고 중력이 코딩되지 않습니다.첫날 충돌 감지 : 신속한 3

더 적은 관심사입니다. 오늘 내 충돌로 인해 내 충돌 인수 기능이 활성화되지 않아서 집에 들어갈 수없는 이유를 선택하러 왔습니다. 예 이벤트에 연락처가 매핑되었음을 알았습니다. 그것은 내 이론에 어울리는 것을 확신합니다.

// 
// GameScene.swift 
// Sandbox 
// 
// Created by M on 7/1/16. 
// Copyright © 2016 M. All rights reserved. 
// 

import SpriteKit 
import GameplayKit 



class GameScene: SKScene, SKPhysicsContactDelegate { 

    var entities = [GKEntity]() 
    var graphs = [GKGraph]() 

    private var lastUpdateTime : TimeInterval = 0 
    private var label : SKLabelNode? 
    var playerNode : SKSpriteNode? 
    var wallNode : SKSpriteNode? 
    var doorNode : SKSpriteNode? 
    private var spinnyNode : SKShapeNode? 
    var furnishing : SKSpriteNode? 
    var playerCategory = 0x1 << 0 
    var wallCategory = 0x1 << 1 
    var doorCategory = 0x1 << 2 
    var pathCategory = 0x1 << 3 


    func nextRoom() { 

     let sceneNode = SKScene(fileNamed: "MyScene") 
     sceneNode?.scaleMode = .aspectFill 

     // Present the scene 
     if let view = self.view { 
      view.presentScene(sceneNode) 
      view.ignoresSiblingOrder = true 
      view.showsFPS = true 
      view.showsNodeCount = true 
     } 
    } 

    func loadRoom() { 
     let furnishing = SKSpriteNode(color: #colorLiteral(red: 0.2464724183, green: 0.05352632701, blue: 0.03394328058, alpha: 1), size:CGSize(width:25, height:25)) 
     doorNode?.addChild(furnishing) 
    } 

    func enterHouse() { 
     let newWindow = CGSize(width: 500, height: 500) 
     doorNode?.scale(to: newWindow) 
     loadRoom() 
    } 

    func exitHouse(){ 
     let oldWindow = CGSize(width: 100, height: 100) 
     doorNode?.scale(to: oldWindow) 
    } 


    override func sceneDidLoad() { 
     self.lastUpdateTime = 0 
     physicsWorld.contactDelegate = self 

     // Get nodes from scene and store for use later 
     self.playerNode = self.childNode(withName: "//player") as? SKSpriteNode 
     playerNode?.physicsBody = SKPhysicsBody(rectangleOf: (playerNode?.frame.size)!) 
     playerNode?.physicsBody?.isDynamic = true 
     playerNode?.physicsBody?.affectedByGravity = false 
     playerNode?.physicsBody?.categoryBitMask = UInt32(playerCategory) 
     playerNode?.physicsBody?.collisionBitMask = UInt32(wallCategory) 
     playerNode?.physicsBody?.contactTestBitMask = UInt32(doorCategory) 

     for child in self.children { 
      /*if child.name == "wall" { 
       if let child = child as? SKSpriteNode { 
        wallNode?.physicsBody = SKPhysicsBody(rectangleOf: (wallNode?.frame.size)!) 
        wallNode?.physicsBody?.isDynamic = false 
     wallNode?.physicsBody?.categoryBitMask = UInt32(wallCategory) 
     wallNode?.physicsBody?.collisionBitMask = UInt32(playerCategory) 
        self.addChild(child) 
       } 
      }*/ 
     } 

     self.doorNode = self.childNode(withName: "door") as? SKSpriteNode 
     doorNode?.physicsBody?.affectedByGravity = false 
     doorNode?.physicsBody?.isDynamic = false 
     doorNode?.physicsBody = SKPhysicsBody(rectangleOf: (doorNode?.frame.size)!) 
     doorNode?.physicsBody?.categoryBitMask = UInt32(doorCategory) 
     doorNode?.physicsBody?.contactTestBitMask = UInt32(playerCategory) 
    } 

    func touchDown(atPoint pos : CGPoint) { 
      let fromX = playerNode?.position.x 
      let fromY = playerNode?.position.y 
      let toX = pos.x 
      let toY = pos.y 
      let resultX = toX - (fromX)! 
      let resultY = toY - (fromY)! 
      let newX = (playerNode?.position.x)! + resultX/10 
      let newY = (playerNode?.position.y)! + resultY/10 
      playerNode?.position.x = newX 
      playerNode?.position.y = newY 
    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     for t in touches { self.touchDown(atPoint: t.location(in: self)) } 
    } 

    func didBeginContact(contact: SKPhysicsContact) { 

     //this gets called automatically when two objects begin contact with each other 
     // 1. Create local variables for two physics bodies 
     var firstBody: SKPhysicsBody 
     var secondBody: SKPhysicsBody 

     // 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody 
     if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
      firstBody = contact.bodyA 
      secondBody = contact.bodyB 
     } else { 
      firstBody = contact.bodyB 
      secondBody = contact.bodyA 
     } 
     if secondBody.categoryBitMask == UInt32(doorCategory){ 
      enterHouse() 
     } 
    } 

    override func update(_ currentTime: TimeInterval) { 
     // Called before each frame is rendered 

     // Initialize _lastUpdateTime if it has not already been 
     if (self.lastUpdateTime == 0) { 
      self.lastUpdateTime = currentTime 
     } 

     // Calculate time since last update 
     let dt = currentTime - self.lastUpdateTime 

     // Update entities 
     for entity in self.entities { 
      entity.update(withDeltaTime: dt) 
     } 

     self.lastUpdateTime = currentTime 
    } 
} 
+0

이 주석 블록의 어느 선이 충돌이 발생하는 데 도움이? – Feldur

+0

다시, 벽 노드 할당에 신경 쓰지 마십시오. 그것이 온라인에서 본 실험에서 나온 것이고 Xcode는 그것을 비트로 chcho했습니다. 그 조각 주위에 아이디어가 있지만 내 관심사는 내 enterHouse 기능을 둘러싼 충돌 처리 이벤트를 낳습니다. –

답변

0

난 당신이 강제로 wallNode 크기 그러나 당신의 코드를 찾고 (!) 풀기 그렇게

wallNode = self.childNode(withName: "wallNode") as? SKSpriteNode 
처럼 아무것도에 할당하지 않을만큼이 라인이 충돌

wallNode?.physicsBody = SKPhysicsBody(rectangleOf: (wallNode?.frame.size)!) 

의 원인이되는 가정

또는 for 루프에 있습니다.

for 루프에서이 코드를 사용하면 충돌을 피하고 벽 노드를 할당해야합니다.

for child in self.children where child.name == "wall" { 
     if let child = child as? SKSpriteNode { 

      wallNode = child // Try this 

      if let wallNode = wallNode { // safely unwrap wall node to avoid crashes    

       wallNode.physicsBody = SKPhysicsBody(rectangleOf: (wallNode.frame.size)) 
       wallNode.physicsBody?.isDynamic = false 
       wallNode.physicsBody?.categoryBitMask = UInt32(wallCategory) 
       wallNode.physicsBody?.collisionBitMask = UInt32(playerCategory) 
       self.addChild(wallNode) // add wall node here instead if you are using your wallNode property 

      } 
     } 
    } 

희망이