gpt4 book ai didi

ios - SpriteKit SKNode 不与边缘碰撞

转载 作者:行者123 更新时间:2023-11-30 14:02:40 24 4
gpt4 key购买 nike

我正在使用 Swift 在 SpriteKit 中制作游戏。我一生都无法弄清楚为什么我的节点不会碰撞并从屏幕上弹开,这是 SKSpriteNode 的默认行为。

这是我来自三个不同文件的所有代码:

游戏 View Controller :

override func viewDidLoad() {
super.viewDidLoad()

let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true

let battleScene = CSLevel()
battleScene.size = view.frame.size
battleScene.scaleMode = .AspectFill

skView.presentScene(battleScene)
}

CS级别:

//sorry here's the real code     
class CSLevel: SKScene, SKPhysicsContactDelegate {

var myWorld: SKNode!
var leader: CSCharacter?

override init(size: CGSize) {
super.init(size: size)

setUpScene()
setUpCharacters()
}

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

override func didMoveToView(view: SKView) {
super.didMoveToView(view)


}

func setUpScene() {
//Take care of setting up the world and bring in property list

self.anchorPoint = CGPoint(x: 0.5, y: 0.5) //0,0 to 1,1

myWorld = SKNode()
self.addChild(myWorld)

let map = SKSpriteNode(imageNamed: "map")
map.position = CGPoint(x: 0, y: 0)

myWorld.addChild(map)

myWorld.xScale = 1
myWorld.yScale = 1

//Setup physics
self.physicsWorld.gravity = CGVector(dx: 0, dy: -1)
self.physicsWorld.contactDelegate = self

myWorld.physicsBody = SKPhysicsBody(edgeLoopFromRect: screenFrame)
myWorld.physicsBody?.categoryBitMask = wallCategory
}

func setUpCharacters() {
leader = CSCharacter()
myWorld.addChild(leader!)
}

override func update(currentTime: NSTimeInterval) {

}

func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB

if firstBody.categoryBitMask == wallCategory || secondBody.categoryBitMask == wallCategory {

}
}
}

CS角色:

class CSCharacter: SKNode {

var character: SKSpriteNode! //this will be the actual image you see of the character

override init() {
super.init()

character = SKSpriteNode(imageNamed: "character")
self.addChild(character)

self.physicsBody = SKPhysicsBody(circleOfRadius: character.frame.size.width / 2)
self.physicsBody?.dynamic = true
self.physicsBody?.restitution = 5
self.physicsBody?.allowsRotation = true

self.xScale = 2
self.yScale = 2

self.physicsBody?.categoryBitMask = playerCategory
self.physicsBody?.collisionBitMask = wallCategory
self.physicsBody?.contactTestBitMask = wallCategory
}

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

}

这是一个错误还是什么?运行 Xcode beta 7.1。我实际上是在遵循教程,但它的行为方式与该人的行为方式不同,即使我为他正在键入的函数键入 function 。任何帮助或澄清表示赞赏!

最佳答案

这里#(希望有帮助)

尝试在“skview.showsNodeCount = true”下方添加“skView.showsPhysics = true”这将自动追踪物理体周围的线。当您运行应用程序时,您应该会在边缘循环所在的 skscene 周围看到一条细蓝线。如果没有,您可能需要向“let BattleScene = CSLevel()”添加一个参数。因此“let BattleScene = CSLevel()”现在是“let BattleScene = CSLevel(size: skView.frame.size)”。

换句话说,你的节点可能会掉入边缘循环,因为边缘循环的大小未指定

关于ios - SpriteKit SKNode 不与边缘碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793932/

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