如何在我的功能中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
的代码,用于删除我的精灵。
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()
}
答案 0 :(得分:6)
试试这个
编辑:意识到它不会删除精灵,这会起作用。
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()
}
}
答案 1 :(得分:0)
//如果我们用函数创建一个SKSpriteNode就像那样!
func gooseWarnningLabelShow () {
var spriteNodelabel = SKSpriteNode(imageNamed: "labelBack2")
spriteNodelabel.position = CGPoint(x:CGRectGetMidX(frame), y: frame.size.height*0.90)
spriteNodelabel.size = CGSizeMake(frame.size.width*0.70, frame.size.height*0.06)
spriteNodelabel.alpha = 0.2
spriteNodelabel.zPosition = 21.0
self.addChild(spriteNodelabel)
spriteNodelabel.runAction(
SKAction.sequence([
SKAction.waitForDuration(5.0),
SKAction.removeFromParent()
])
)
}
使用此功能创建SKSpriteNode!在该函数中,我们有一个SKAction,它在5秒延迟后执行并删除SKSpriteNode!