Hi everybody,
I am trying to write a game in swift with spritesnode. I used the “touchesBegan” to shoot enemies and “touchesMoved” to move my ship. After a short minutes, the fps fall from 60 to almost 0 while the memory remains low (30 MB) and CPU is 100% almost all the time. I have about 30 nodes, 15 Draws.
Someone would have an idea where the probleme come from ?
Thank for your help
Sincerely
here the code of “fire” func and “touchesmoved and began” func
func fire() {
laser = SKSpriteNode(imageNamed: "Laser")
laser.name = "Laser"
laser.size = CGSize(width: 20, height: 2)
laser.position = ship.position
if vitesse < 0 {
let moveX = SKAction.moveBy (x: WidthScreen-ship.position.x, y: 0, duration: 0.5)
let moveXDone = SKAction.removeFromParent()
laser.run(SKAction.sequence([moveX, moveXDone]))
} else {
let moveX = SKAction.moveBy(x: -WidthScreen+ship.position.x, y: 0, duration: 0.5)
let moveXDone = SKAction.removeFromParent()
laser.run(SKAction.sequence([moveX, moveXDone]))
}
laser.physicsBody = SKPhysicsBody(rectangleOf: (laser.size))
laser.physicsBody?.categoryBitMask = PhysicsCategory.Laser
laser.physicsBody?.collisionBitMask = PhysicsCategory.Invaders | PhysicsCategory.SmartInvaders | PhysicsCategory.UfoToSmart
laser.physicsBody?.isDynamic = false
laser.zPosition = 3
let soundAction = SKAction.playSoundFileNamed("Laser.wav", waitForCompletion: true);
self.run(soundAction)
self.addChild(laser)
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
let touch : UITouch = touches.first!
var touchLocation = touch.location(in: self)
if touchLocation.y > 88HeightScreen/100 {
touchLocation.y = 88HeightScreen/100
}
if touchLocation.y < HeightScreen/15 {
touchLocation.y = HeightScreen/15
}
elevation = touchLocation.y
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
/* Called when a touch begins */
if lifeArray.count>0{
fire()
}
}