将物理体添加到 Spritekit 节点
Adding Physic Bodies to Spritekit Nodes
我正在使用 Xcode 7 和 Swift 2 在 iOS 上制作一个基本游戏。我正在尝试将物理体添加到节点。在用户的角色上,物理 body 运行良好(没有崩溃并且运行相同)。然而,当给障碍物添加物理体时,障碍物并没有出现......这是我的障碍物代码:
let random = Int(arc4random_uniform(4))
var objectTexture = SKTexture()
var object = SKSpriteNode()
if random == 0 {
objectTexture = SKTexture(imageNamed: "coin")
object = SKSpriteNode(texture: objectTexture)
object.physicsBody = SKPhysicsBody(rectangleOfSize: objectTexture.size())
object.physicsBody!.categoryBitMask = ColliderType.Coin.rawValue
object.physicsBody!.contactTestBitMask = ColliderType.Coin.rawValue
object.physicsBody!.collisionBitMask = ColliderType.Coin.rawValue
}
if random != 0 {
objectTexture = SKTexture(imageNamed: "fryingPan")
object = SKSpriteNode(texture: objectTexture)
object.physicsBody = SKPhysicsBody(rectangleOfSize: objectTexture.size())
object.physicsBody!.categoryBitMask = ColliderType.Obstacle.rawValue
object.physicsBody!.contactTestBitMask = ColliderType.Obstacle.rawValue
object.physicsBody!.collisionBitMask = ColliderType.Obstacle.rawValue
}
//Setting up object
使用此代码,object 不会出现在屏幕上。但是,当我注释掉物理 body 行时,object 会按指示出现在屏幕上。有谁知道我可能哪里出错了?感谢您的帮助。
设置
object.physicsBody.affectedByGravity = false
我猜重力会将你的障碍物移出屏幕
我正在使用 Xcode 7 和 Swift 2 在 iOS 上制作一个基本游戏。我正在尝试将物理体添加到节点。在用户的角色上,物理 body 运行良好(没有崩溃并且运行相同)。然而,当给障碍物添加物理体时,障碍物并没有出现......这是我的障碍物代码:
let random = Int(arc4random_uniform(4))
var objectTexture = SKTexture()
var object = SKSpriteNode()
if random == 0 {
objectTexture = SKTexture(imageNamed: "coin")
object = SKSpriteNode(texture: objectTexture)
object.physicsBody = SKPhysicsBody(rectangleOfSize: objectTexture.size())
object.physicsBody!.categoryBitMask = ColliderType.Coin.rawValue
object.physicsBody!.contactTestBitMask = ColliderType.Coin.rawValue
object.physicsBody!.collisionBitMask = ColliderType.Coin.rawValue
}
if random != 0 {
objectTexture = SKTexture(imageNamed: "fryingPan")
object = SKSpriteNode(texture: objectTexture)
object.physicsBody = SKPhysicsBody(rectangleOfSize: objectTexture.size())
object.physicsBody!.categoryBitMask = ColliderType.Obstacle.rawValue
object.physicsBody!.contactTestBitMask = ColliderType.Obstacle.rawValue
object.physicsBody!.collisionBitMask = ColliderType.Obstacle.rawValue
}
//Setting up object
使用此代码,object 不会出现在屏幕上。但是,当我注释掉物理 body 行时,object 会按指示出现在屏幕上。有谁知道我可能哪里出错了?感谢您的帮助。
设置
object.physicsBody.affectedByGravity = false
我猜重力会将你的障碍物移出屏幕