gpt4 book ai didi

Swift 3 Physicsbody 碰撞不起作用

转载 作者:可可西里 更新时间:2023-11-01 02:05:00 25 4
gpt4 key购买 nike

这是我的玩家类

我的播放器不会与 Gamefield 发生碰撞,它们只是相互穿过。我不希望他们彼此重叠。我试着用谷歌搜索解决方案,但对我来说,我似乎基本上做得很好。

请帮助:

class Player: SKShapeNode {
static public let length: CGFloat = 50
static public let rect = CGRect(x: -length/2, y: -length/2, width: length, height: length)

var life = 100

override init() {
super.init()
self.fillColor = SKColor.blue
self.strokeColor = SKColor.black
self.position = CGPoint(x: 50, y: 50)

self.physicsBody = SKPhysicsBody(edgeLoopFrom: Player.rect)
self.physicsBody?.isDynamic = true
self.physicsBody?.allowsRotation = false
self.physicsBody?.categoryBitMask = PhysicsCategory.Robot
self.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile
self.physicsBody?.collisionBitMask = PhysicsCategory.Gamefield
self.physicsBody?.usesPreciseCollisionDetection = true
}; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

func moveBy(vect: CGVector) {
self.position.x += vect.dx
self.position.y += vect.dy
//print(vect.dx, vect.dy)
}
}

这是我的游戏场景类

struct PhysicsCategory {
static let None : UInt32 = UInt32.min
static let All : UInt32 = UInt32.max
static let Robot : UInt32 = 0b0001 // 1
static let Gamefield : UInt32 = 0b0010 // 2
static let Monster : UInt32 = 0b0011 // 3
static let Projectile : UInt32 = 0b0100 // 4
}

class GameScene: SKScene, SKPhysicsContactDelegate {
var player = Player(rect: Player.rect)
var controlL: Control
var controlR: Control

var cam = SKCameraNode()

override init(size: CGSize) {
controlL = Control(posX: 0, size: size, direction: 1)
controlR = Control(posX: size.width, size: size, direction: -1)

super.init(size: size)

}; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

override func didMove(to view: SKView) {
self.backgroundColor = SKColor.white

physicsWorld.gravity = CGVector.zero
physicsWorld.contactDelegate = self

cam.xScale = 1
cam.yScale = 1
self.camera = cam
player.addChild(cam)

let gameFieldRect = CGRect(x: 0, y: 0, width: 1000, height: 600)
let gameField = SKShapeNode(rect: gameFieldRect)
gameField.fillColor = SKColor.clear
gameField.strokeColor = SKColor.black
gameField.position = CGPoint(x: 0, y: 0)

gameField.physicsBody = SKPhysicsBody(edgeLoopFrom: gameFieldRect)
gameField.physicsBody?.isDynamic = true
gameField.physicsBody?.allowsRotation = false
gameField.physicsBody?.categoryBitMask = PhysicsCategory.Gamefield
gameField.physicsBody?.contactTestBitMask = PhysicsCategory.None
gameField.physicsBody?.collisionBitMask = PhysicsCategory.Robot
self.addChild(gameField)

self.addChild(player)

cam.addChild(controlL.add())
cam.addChild(controlR.add())

我希望有人看到错误。我已经花了很长时间。

最佳答案

您需要将您的机器人更改为不同的物理体类型 (rectangleOf)?:

SpriteKit supports two kinds of physics bodies, volume-based bodies and edge-based bodies. When you create a physics body, its kind, size, and shape are determined by the constructor method you call. An edge-based body does not have mass or volume and is unaffected by forces or impulses in the system. Edge-based bodies are used to represent volume-less boundaries or hollow spaces in your physics simulation. In contrast, volume-based bodies are used to represent objects with mass and volume.

此外,您是否使用脉冲或力来移动您的机器人?不是 .move(to:) 或 .position =?移动和位置会破坏你的物理世界(在某些情况下它会经历)

此外,您似乎需要将类别掩码更改为适当的系列(对于 didBegin(contact:))

它应该是 1、2、4、8、16,依此类推...这就是您获得 独特 接触命中的方式。现在您可以获得“4”的命中从 0+4 或 1+3... 因此,不是唯一的碰撞。

虽然你的 collisionBitMask 看起来不错..他们应该互相碰撞。

关于Swift 3 Physicsbody 碰撞不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43808297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com