所以我创造了一个移动瓷砖的玩家和围绕地图的墙,以保持玩家在操场上。两者都有物理身体。我的猜测是,我的运动员运动不正确,因此球员会进入墙壁。让我告诉你我的代码:
所以这是玩家的物理实体:
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的碰撞正常工作
答案 0 :(得分:4)
你没有真正描述过你的问题,除了“精灵正在相互移动......”和“所以玩家会闯入墙壁”
要验证您的所有联系人和collsions,请在此处查看我的答案:iOS SpriteKit - collisions and contacts not working as expected并尝试实现checkPhysics()函数并在您认为已设置所有物理机构及其相互作用。
你确定:
SKPhysicsContactDelegate
physicsWorld.contactDelegate = self
SKPhysicsContactDelegate
:
这是否正确:
答案 1 :(得分:3)
此处未在问题中列出,但未发生碰撞联系的原因是因为方法didBeginContact
已更改为didBegin