gpt4 book ai didi

swift - 5 秒后删除 SKSpriteNode

转载 作者:搜寻专家 更新时间:2023-10-31 22:07:15 26 4
gpt4 key购买 nike

如何在我的函数中像这样在 5 秒后删除我的 SKSpriteNode。我尝试使用一个名为 func 的 NSTimer 删除我的 BonusSprite但 5 秒后我的应用程序崩溃了:

let timerApparitionBonus = NSTimer.scheduledTimerWithTimeInterval(13, target: self, selector: Selector("ApparitionBonus"), userInfo: nil, repeats: true)

}

func ApparitionBonus() {

var BonusApparitionX = UInt32(self.frame.size.width)
var BonusApparitionY = UInt32(self.frame.size.height)

BonusApparitionX = arc4random() % BonusApparitionX
BonusApparitionY = arc4random() % BonusApparitionY
BonusSprite.position = CGPointMake(CGFloat(BonusApparitionX),CGFloat(BonusApparitionY))
BonusSprite.setScale(0.8)
self.addChild(BonusSprite)
}

编辑:

这是我从 DidMoveToView 中删除我的 sprite 的代码。

override func didMoveToView(view: SKView) {

physicsWorld.contactDelegate = self

//BackGround

self.scene?.backgroundColor = UIColor.blackColor()
self.addChild(SKEmitterNode(fileNamed: "MyParticle")!)
self.scene?.size = CGSize(width: 640, height: 1136)

//Placement du Vaisseau :

Vaisseau.setScale(2)
Vaisseau.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
Vaisseau.physicsBody = SKPhysicsBody(rectangleOfSize: Vaisseau.size)
Vaisseau.physicsBody?.affectedByGravity = false
Vaisseau.physicsBody?.categoryBitMask = PhysicsCategories.Vaisseau
Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Meteorites
Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Bonus
Vaisseau.physicsBody?.dynamic = false
self.addChild(Vaisseau)

//Timer créer enemis

CreationEnemisTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("CreationMeteorites"), userInfo: nil, repeats: true)

//Score

timerScore = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector: Selector("ScoreUpper"), userInfo: nil, repeats: true)
ScoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width / 3, height: 20))
ScoreLabel.center = CGPoint(x : self.frame.size.width / 2,y : self.frame.size.height / 4)
ScoreLabel.text = "Score : \(Score)"
ScoreLabel.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 0.3)
ScoreLabel.textColor = UIColor.whiteColor()
self.view?.addSubview(ScoreLabel)

//Aparition des Bonus (timer)

let myFunction = SKAction.runBlock({self.ApparitionBonus()})
let wait = SKAction.waitForDuration(5)
let remove = SKAction.runBlock({self.removeBonus()})

self.runAction(SKAction.sequence([myFunction, wait, remove]))
}

func ApparitionBonus() {

var BonusApparitionX = UInt32(self.frame.size.width)
var BonusApparitionY = UInt32(self.frame.size.height)

BonusApparitionX = arc4random() % BonusApparitionX
BonusApparitionY = arc4random() % BonusApparitionY

BonusSprite.position = CGPointMake(CGFloat(BonusApparitionX),CGFloat(BonusApparitionY))
BonusSprite.setScale(0.8)

BonusSprite.physicsBody?.categoryBitMask = PhysicsCategories.Bonus
BonusSprite.physicsBody?.contactTestBitMask = PhysicsCategories.Vaisseau
}

func removeBonus() {

BonusSprite.removeFromParent()
}

最佳答案

试试这个

编辑:意识到它不会删除 Sprite ,这会起作用。

class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let myFunction = SKAction.runBlock({()in self.ApparitionBonus()})
let wait = SKAction.waitForDuration(5)
let remove = SKAction.runBlock({() in self.removeSprite()})
self.runAction(SKAction.sequence([myFunction, wait, remove]))
}
func ApparitionBonus() {
self.addChild(bonusSprite)
}
func removeSprite() {
bonusSprite.removeFromParent()
}
}

关于swift - 5 秒后删除 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33287829/

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