Я создаю игру, которая порождает случайные платформы перед игроком.во время касания начался метод, который я создал SKAction, который вызывает функцию создания стен после задержки.используя инструменты XCode, я сузил, что падение choppiness / framerate каждый раз, когда генерируется платформа, коренится в createWalls () и полагает, что это происходит от попытки рисовать спрайты из случайного числа.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if isGameStarted == false{
//1
isGameStarted = true
player.physicsBody?.affectedByGravity = true
createPauseBtn()
//2
logoImg.run(SKAction.scale(to: 0.5, duration: 0.3), completion: {
self.logoImg.removeFromParent()
})
taptoplayLbl.removeFromParent()
//3
self.player.run(repeatActionBird)
//TODO: add pillars here
let spawn = SKAction.run({
() in
self.wallPair = self.createWalls()
self.addChild(self.wallPair)
})
//2
let delay = SKAction.wait(forDuration: 1.5
)
let SpawnDelay = SKAction.sequence([spawn, delay])
let spawnDelayForever = SKAction.repeatForever(SpawnDelay)
self.run(spawnDelayForever)
//3
let distance = CGFloat(self.frame.width + wallPair.frame.width)
let movePillars = SKAction.moveBy(x: -distance - 50, y: 0, duration: TimeInterval(0.008 * distance))
let removePillars = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePillars, removePillars])
player.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))
} else {
//4
if isDied == false {
player.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))
}
}
for touch in touches{
let location = touch.location(in: self)
//1
if isDied == true{
if restartBtn.contains(location){
if UserDefaults.standard.object(forKey: "highestScore") != nil {
let hscore = UserDefaults.standard.integer(forKey: "highestScore")
if hscore < Int(scoreLbl.text!)!{
UserDefaults.standard.set(scoreLbl.text, forKey: "highestScore")
}
} else {
UserDefaults.standard.set(0, forKey: "highestScore")
}
restartScene()
}
} else {
//2
if pauseBtn.contains(location){
if self.isPaused == false{
self.isPaused = true
pauseBtn.texture = SKTexture(imageNamed: "play")
} else {
self.isPaused = false
pauseBtn.texture = SKTexture(imageNamed: "pause")
}
}
}
}
}
иэто моя функция createWalls ()
func createWalls() -> SKNode {
// 1
let coinPouch = SKSpriteNode()
coinPouch.size = CGSize(width: 40, height: 40)
coinPouch.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2)
coinPouch.physicsBody = SKPhysicsBody(rectangleOf: coinPouch.size)
coinPouch.physicsBody?.affectedByGravity = false
coinPouch.physicsBody?.isDynamic = false
coinPouch.physicsBody?.categoryBitMask = CollisionBitMask.flowerCategory
coinPouch.physicsBody?.collisionBitMask = 0
coinPouch.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
coinPouch.color = SKColor.blue
// 2
wallPair = SKNode()
wallPair.name = "wallPair"
let randomPlatform1 = Int(arc4random_uniform(4)) + 1
let randomPlatform2 = Int(arc4random_uniform(4)) + 1
let platformAtlas = SKTextureAtlas(named: "CoinPlatforms")
let texture1 = platformAtlas.textureNamed("\(randomPlatform1)CoinPlatform")
let texture2 = platformAtlas.textureNamed("\(randomPlatform2)CoinPlatform")
let topWall = SKSpriteNode(texture: texture1)
topWall.name = "\(randomPlatform1)CoinPlatform"
let btmWall = SKSpriteNode(texture: texture2)
btmWall.name = "\(randomPlatform2)CoinPlatform"
topWall.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2 + 475)
btmWall.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2 - 475)
topWall.setScale(0.5)
btmWall.setScale(0.5)
if randomPlatform1 < 4 {
topWall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "\(randomPlatform1)CoinPlatform"), size: topWall.size)
topWall.physicsBody?.categoryBitMask = CollisionBitMask.pillarCategory
topWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
topWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
} else {
topWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: topWall.size.width - 2, height: topWall.size.height - 2))
topWall.physicsBody?.categoryBitMask = CollisionBitMask.groundCategory
topWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
topWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
}
topWall.physicsBody?.isDynamic = false
topWall.physicsBody?.affectedByGravity = false
if randomPlatform2 < 4 {
btmWall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "\(randomPlatform2)CoinPlatform"), size: btmWall.size)
btmWall.physicsBody?.categoryBitMask = CollisionBitMask.pillarCategory
btmWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
btmWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
} else {
btmWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: btmWall.size.width - 2, height: btmWall.size.height - 2))
btmWall.physicsBody?.categoryBitMask = CollisionBitMask.groundCategory
btmWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
btmWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
}
btmWall.physicsBody?.isDynamic = false
btmWall.physicsBody?.affectedByGravity = false
topWall.zRotation = CGFloat(Double.pi)
wallPair.addChild(topWall)
wallPair.addChild(btmWall)
wallPair.zPosition = 1
let randomPosition = random(min: -200, max: 200)
wallPair.position.y = wallPair.position.y + randomPosition
let randomSuperCoinSpawn = arc4random_uniform(5)
if randomSuperCoinSpawn == 1 {
wallPair.addChild(coinPouch)
}
wallPair.run(moveAndRemove)
return wallPair
}
func random() -> CGFloat{
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(min : CGFloat, max : CGFloat) -> CGFloat{
return random() * (max - min) + min
}
, игра работает так же хорошо, как и порождающие мне узлы, но заморозка действительно убивает игру.Любая помощь очень ценится, я был на этом в течение нескольких дней и хотел бы двигаться дальше.Спасибо.