2016-06-23 28 views
0

私は2つの宇宙船が画面の周りに浮かんでいますが、画面の上とモットーを壊すことはできません。しかし、彼らは私の側を飛ぶことができます。私は無限のサイトを見てきましたが、いくつかの種類のskphysicsbodysを行っていますが、どれも役に立たないものです。 コード例。Swift 2 PhysicsBody not working

class GameScene: SKScene, SKPhysicsContactDelegate { 

var player = SKSpriteNode(imageNamed: "spaceship.png") 
var player2 = SKSpriteNode(imageNamed: "spaceship.png") 
var timer = NSTimer() 
var tapsValid:Bool? 
var playerRight:Bool? 
var playerChange:Bool? 

override func didMoveToView(view: SKView) { 

    let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 
    sceneBody.friction = 0 
    self.physicsBody = sceneBody 

    physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0) 


    tapsValid = true 
    playerRight = true 

    self.scene?.backgroundColor = UIColor.whiteColor() 

    player.position = CGPointMake(self.size.width/2, self.size.height/1.8 + 280) 
    player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size) 
    player.physicsBody?.dynamic = false 
    player.physicsBody?.affectedByGravity = false 
    player.physicsBody?.restitution = 1 
    player.physicsBody?.friction = 0 
    player.physicsBody?.linearDamping = 0 
    player.physicsBody?.angularDamping = 0 

    player2.position = CGPointMake(self.size.width/2, self.size.height/14) 
    player2.physicsBody = SKPhysicsBody(rectangleOfSize: player2.size) 
    player2.physicsBody?.affectedByGravity = false 
    player2.physicsBody?.dynamic = false 
    player2.physicsBody?.restitution = 1 
    player2.physicsBody?.friction = 0 
    player.physicsBody?.linearDamping = 0 
    player.physicsBody?.angularDamping = 0 

    self.addChild(player) 

    self.addChild(player2) 

誰でも私をここで助けてください。私はちょうど画面の端に物理学を追加したい。ありがとう、

答えて

0

ノードのcolliderTypesとカテゴリを定義する必要があります。あなたは何を見てチェックすることができますこの方法は、どのような

enum ColliderType: UInt32 { 
    case Spaceship = 0 
    case CornerCategory = 1 
} 

self.physicsBody!.categoryBitMask = ColliderType.CornerCategory.rawValue 


player.physicsBody?.contactTestBitMask = ColliderType.CornerCategory.rawValue 
player2.physicsBody?.contactTestBitMask = ColliderType.CornerCategory.rawValue 

func didBeginContact(contact: SKPhysicsContact) { 
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
     //If it is true. Means that bodyA is the spaceship. Because it's 0 
     firstBody = contact.bodyA 
     secondBody = contact.bodyB 
    }else{ 
     //Either way. firstBody will always be the spaceship 
     firstBody = contact.bodyB 
     secondBody = contact.bodyA 
    } 

    if firstBody.categoryBitMask == ColliderType. Spaceship.rawValue && secondBody.categoryBitMask == ColliderType.CornerCategory.rawValue{ 
     //means that Spaceship and wall touches 
     print("Spaceship hits wall") 

     } 
} 
を打ちます