gpt4 book ai didi

Swift 2,SpriteKit,我在从父节点删除节点时遇到问题

转载 作者:行者123 更新时间:2023-11-30 13:45:43 25 4
gpt4 key购买 nike

我对 Swift 有点陌生,所以如果这令人困惑,请原谅我。

我试图让我的节点之一“guava”在“ninja”与它碰撞时消失。

目前我的代码在 createScene() 函数中设置如下:

        func spawnGuava(){

//The way the Guava spawn and move

let guavaNode = SKNode()

let guava = SKSpriteNode(texture: guavaNodeTexture)

guava.setScale(0.75)
guava.position = CGPointMake (self.size.width + 160, self.size.height * 0.05)
guava.physicsBody = SKPhysicsBody(rectangleOfSize: guava.size)
guava.physicsBody?.affectedByGravity = false
guava.physicsBody?.dynamic = true
guava.alpha = 0.75
guava.physicsBody?.categoryBitMask = PhysicsCatagory.Guava
guava.physicsBody?.collisionBitMask = PhysicsCatagory.None
guava.physicsBody?.contactTestBitMask = PhysicsCatagory.Ninja

guava.zPosition = 0

guavaNode.addChild(guava)


guava.runAction(GuavaMoveAndRemove)

self.addChild(guavaNode)

func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){

guavaNode.removeFromParent()

}

func didCollideWithGuava(contact: SKPhysicsContact){
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else{
firstBody = contact.bodyB
secondBody = contact.bodyA
}

if ((firstBody.categoryBitMask & PhysicsCatagory.Guava != 0) && (secondBody.categoryBitMask & PhysicsCatagory.Ninja != 0)) {
ninjaAteGuava(firstBody.node as! SKSpriteNode, guava: secondBody.node as! SKSpriteNode)

}
}

这是有关忍者的一些信息

  let ninjaTexture = SKTexture(imageNamed:"ninja")
ninjaTexture.filteringMode = SKTextureFilteringMode.Nearest

ninja = SKSpriteNode(texture: NinjaTexture)
ninja.setScale(0.5)
ninja.position = CGPoint(x: self.frame.size.width * 0.50, y: self.frame.size.height * 0.6)

ninja.physicsBody = SKPhysicsBody(circleOfRadius: ninja.size.height / 2)
ninja.physicsBody?.categoryBitMask = PhysicsCatagory.Ninja
ninja.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall |
ninja.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Guava
ninja.physicsBody!.dynamic = true
ninja.physicsBody!.allowsRotation = false

self.addChild(ninja)

额外信息:

struct PhysicsCatagory {

static let None : UInt32 = 0
static let Boognish : UInt32 = 0x1 << 1
static let Ground : UInt32 = 0x1 << 2
static let Wall : UInt32 = 0x1 << 3
static let Guava : UInt32 = 0x1 << 4
static let Pepper: UInt32 = 0x1 << 5

}

最佳答案

guavaNode 应该是 ninjaAteGuava 函数中的 guava

func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){

guavaNode.removeFromParent()

}

应该是:

func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){

guava.removeFromParent()

}

这是因为你函数中的参数是guava;不是 Guava 节点。我不知道为什么 Xcode 没有给你一个错误,但这应该可以。希望这会有所帮助。

关于Swift 2,SpriteKit,我在从父节点删除节点时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970058/

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