SpriteKit:Sprites在已经设置了physicsBody的情况下彼此移动 [英] SpriteKit: Sprites are moving through each other with a physicsBody already set
问题描述
因此,我创建了一个玩家,该玩家可以基于图块移动并围绕地图移动墙以将其保持在操场上。两者都有physicalBody。我的猜测是,我的播放器移动不正确,因此播放器会撞到墙壁上。让我向您展示我的代码:
So I have created a player who moves tile based and a wall surrounding the map to keep the player on the playground. Both have a physicsBody in them. My guess is, that my movement of the player is not properly and so the player bugs into the walls. Let me just show you my code:
所以这是玩家的身体:
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Player
self.physicsBody!.contactTestBitMask = PhysicsCategory.House
self.physicsBody!.collisionBitMask = PhysicsCategory.Wall
这是PhysicalBody of Wall:
And this is the physicsBody of the Wall:
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Wall
self.physicsBody!.contactTestBitMask = 0
self.physicsBody!.collisionBitMask = PhysicsCategory.Player
self.physicsBody!.isDynamic = false
它们都从对象类继承:
self.physicsBody = SKPhysicsBody(rectangleOf: size)
self.physicsBody!.affectedByGravity = false
self.name = name
一种如果这是原因,请让我给我我播放器移动的代码:
So and just let me give you my code for the movement of the player if thats the cause:
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正常工作
The main problem is to get collision with SKAction properly working
推荐答案
除了 Sprite正在彼此之间移动……和因此玩家将虫子打入了墙壁
You haven't actually described your problem other than "Sprites are moving through each other..." and "so the player bugs into the walls"
要验证您所有的联系和同谋,请查看我的答案此处: iOS SpriteKit-碰撞和联系人无法正常工作,并尝试实现checkPhysics()函数,并在您认为已设置所有物理物体及其相互作用后调用它。
To validate all of your contact and collsions, have a look at my answer here : iOS SpriteKit - collisions and contacts not working as expected and try implementing the checkPhysics() function and calling it once you think you have set up all of your physics bodies and their interactions.
您确定吗?
- 玩家和Wall实际上具有物理物体吗?
- 场景是
SKPhysicsContactDelegate
- 您有设置
physicsWorld.contactDelegate = self
- 您已经实现了
SKPhysicsContactDelegate的一种可选方法
:
- didBeginContact
- didEndcontact
- player and wall actually have physics bodies?
- the scene is an
SKPhysicsContactDelegate
- You have set
physicsWorld.contactDelegate = self
- You have implemented one of the optional methods of
SKPhysicsContactDelegate
:- didBeginContact
- didEndcontact
这是否正确:
- 您想让玩家与墙碰撞吗?
- 您想让玩家与墙碰撞吗?
- 您想得到通知(didBegincontact称为),当玩家
和房屋碰触时? - 您不想在墙壁碰到任何东西时收到通知吗?
- You want the player to collide with the wall?
- You want the wall to collide with the player?
- You want to be notified (didBegincontact called) when the player and the house touch?
- You don't want to be notified if the wall touches anything?
这篇关于SpriteKit:Sprites在已经设置了physicsBody的情况下彼此移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!