【发布时间】:2017-02-11 07:41:22
【问题描述】:
因此,我创建了一个玩家,该玩家基于图块移动,并在地图周围设置一堵墙,以使玩家保持在操场上。两者都有一个physicsBody。我的猜测是,我的玩家动作不正确,所以玩家会撞到墙上。让我向您展示我的代码:
这就是玩家的physicsBody:
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Player
self.physicsBody!.contactTestBitMask = PhysicsCategory.House
self.physicsBody!.collisionBitMask = PhysicsCategory.Wall
这就是墙体的物理:
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Wall
self.physicsBody!.contactTestBitMask = 0
self.physicsBody!.collisionBitMask = PhysicsCategory.Player
self.physicsBody!.isDynamic = false
它们都继承自 Objects 类:
self.physicsBody = SKPhysicsBody(rectangleOf: size)
self.physicsBody!.affectedByGravity = false
self.name = name
所以,如果这是原因,让我给你我的玩家移动代码:
func move(direction: String, width: CGFloat){
//Choosing the direction based of the users touch
switch direction{
case "South":
//Decreasing the position of the player by 1 Tile
pos.y -= width
//Flipping the picture the right way to make the player look in the right direction
yScale = 1.0
//Rotates the picture
let rotateAction = SKAction.rotate(toAngle: -1.57, duration: 0.0)
run(rotateAction)
//All the other directions just change the x and y value of pos corresponding to the direction
}
//Moves the player to the calculated position
let moveAction = SKAction.move(to: pos, duration: 0.0)
run(moveAction){
//Sound
}
编辑:
PhysicsCategory.Player 和 PhysicsCategory.Wall 的值
struct PhysicsCategory{
static let Wall:UInt32 = 0x1 << 0
static let Player:UInt32 = 0x1 << 1
static let House:UInt32 = 0x1 << 2
}
编辑 2:
主要问题是与 SKAction 正常工作的碰撞
【问题讨论】:
-
PhysicsCategory.Player、PhysicsCategory.House 和 PhysicsCategory.Wall 的值是什么?
-
已编辑。房子是玩家必须去的目标。同样的错误也发生在那里。
-
我们需要比我更强大的头脑。我的很困惑。
-
您需要什么信息?
标签: swift sprite-kit collision skphysicsbody