试图添加一个已经有父节点的 SKNode swift
Attemped to add a SKNode which already has a parent swift
我正在创建一个游戏,但我的与操纵杆相关联的子弹生成方法一直出现此错误。我想在操纵杆处于活动状态时重复生成节点这是我创建触发方法的方式
override func didMoveToView(view: SKView) {
if fireWeapon == true {
NSTimer.scheduledTimerWithTimeInterval(0.25, target: self,
selector: Selector ("spawnBullet1"), userInfo: nil, repeats: true)
}
}
func spawnBullet1(){
self.addChild(bullet1)
bullet1.position = CGPoint (x: hero.position.x , y:hero.position.y)
bullet1.xScale = 0.5
bullet1.yScale = 0.5
bullet1.physicsBody = SKPhysicsBody(rectangleOfSize: bullet1.size)
bullet1.physicsBody?.categoryBitMask = PhysicsCategory.bullet1
bullet1.physicsBody?.contactTestBitMask = PhysicsCategory.enemy1
bullet1.physicsBody?.affectedByGravity = false
bullet1.physicsBody?.dynamic = false
}
override func touchesBegan(touches: Set<UITouch>, withEvent
event:UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if (CGRectContainsPoint(joystick.frame, location)) {
stickActive = true
if stickActive == true {
fireWeapon = true
}
第一个项目符号按计划启动并且运行良好,但是每次第二个项目符号启动时应用程序崩溃并且我收到错误。谁能告诉我另一种产生射速的方法
而不是 self.addChild(bullet1)
你需要 self.addChild(SKSpriteNode(imageNamed: "bullet.png"))
这是因为每次发射都需要创建一颗新子弹,你不想每次都移动同一颗子弹。
我正在创建一个游戏,但我的与操纵杆相关联的子弹生成方法一直出现此错误。我想在操纵杆处于活动状态时重复生成节点这是我创建触发方法的方式
override func didMoveToView(view: SKView) {
if fireWeapon == true {
NSTimer.scheduledTimerWithTimeInterval(0.25, target: self,
selector: Selector ("spawnBullet1"), userInfo: nil, repeats: true)
}
}
func spawnBullet1(){
self.addChild(bullet1)
bullet1.position = CGPoint (x: hero.position.x , y:hero.position.y)
bullet1.xScale = 0.5
bullet1.yScale = 0.5
bullet1.physicsBody = SKPhysicsBody(rectangleOfSize: bullet1.size)
bullet1.physicsBody?.categoryBitMask = PhysicsCategory.bullet1
bullet1.physicsBody?.contactTestBitMask = PhysicsCategory.enemy1
bullet1.physicsBody?.affectedByGravity = false
bullet1.physicsBody?.dynamic = false
}
override func touchesBegan(touches: Set<UITouch>, withEvent
event:UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if (CGRectContainsPoint(joystick.frame, location)) {
stickActive = true
if stickActive == true {
fireWeapon = true
}
第一个项目符号按计划启动并且运行良好,但是每次第二个项目符号启动时应用程序崩溃并且我收到错误。谁能告诉我另一种产生射速的方法
而不是 self.addChild(bullet1)
你需要 self.addChild(SKSpriteNode(imageNamed: "bullet.png"))
这是因为每次发射都需要创建一颗新子弹,你不想每次都移动同一颗子弹。