因此,我创建了一个基于图块移动的玩家,并在地图周围创建了一堵墙,以将玩家保持在操场上。两者都有一个物理主体。我的猜测是,我的玩家移动不正确,因此玩家会撞到墙上。让我向你展示我的代码:
这就是玩家的物理主体:
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
}
Edit:
的价值观PhysicsCategory.Player
and PhysicsCategory.Wall
struct PhysicsCategory{
static let Wall:UInt32 = 0x1 << 0
static let Player:UInt32 = 0x1 << 1
static let House:UInt32 = 0x1 << 2
}
Edit 2:
主要问题是如何与 SKAction 正常工作发生碰撞