Very High CPU Usage in SpriteKit - ios

I'm making a simple tile-based game in SpriteKit and I'm having some difficulty with high CPU usage in my game. I have a map made of 60 tiles, and each tile is a subclass of SKSpriteNode. Just displaying these 60 sprites in the scene is using up to 80% of CPU in the iPhone 6s simulator. There is no motion, user interaction, or physics going on. When I made the same game in UIKit and not SpriteKit my CPU usage was 0. What could be using so much CPU?
my tile class:
import SpriteKit
import UIKit
class Tile: SKSpriteNode {
var tileType = "grass", tileX = 0, tileY = 0
init (tileType: String, tileX: Int, tileY: Int) {
self.tileType = tileType
self.tileX = tileX
self.tileY = tileY
let texture = SKTexture(imageNamed: tileType)
super.init(texture: texture, color: UIColor(), size: texture.size())
self.userInteractionEnabled = true
self.position = CGPoint(x: CGFloat(45+64*(tileX-1)), y: CGFloat(47+56*(tileY-1)))
self.zPosition = -1
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and my Sprite Kit scene code:
import SpriteKit
var map: [[String]] = [["grass","water","grass","rocky","rocky","grass","grass","grass","grass","water"],["grass","water","grass","grass","rocky","rocky","grass","grass","water","water"],["grass","water","water","grass","rocky","grass","grass","water","water","water"],["grass","grass","water","rocky","rocky","grass","grass","water","water","water"],["grass","grass","water","rocky","rocky","grass","water","water","water","water"],["grass","grass","water","rocky","rocky","grass","water","water","water","water"] ]
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
for (rowNumber, row) in map.enumerate() {
for (columnNumber, type) in row.enumerate() {
let theTile = Tile(tileType: type, tileX: columnNumber+1, tileY: rowNumber+1)
self.addChild(theTile)
}
}
self.backgroundColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

Iphone Simulator on Xcode have a much greater CPU Usage than in a real device.
Test in a real device to have a real metric about CPU Usage.

Tyshka, you need to reuse resources. Your code creates a new SKTexture for the exact same input image data. Just create one SKTexture and then set that as the texture for the new node. Add a map to your code so that the SKTexture can be checked and reused given the texture string name.

Related

Swift SKNode position help - sprite position always 0,0 as it moves

Problem: I’m working in swift playground on an iPad Pro.
I’d like to get the current position coordinates of an SKSpriteNode with attached physicsBody as they move around the screen. No matter what it try, the sprite position returns 0,0, despite seeing it move across the screen.
I’ve attempted various implementations of convert(_:to:) and convert(_:from:) but the sprite position always ends up 0,0. My understanding is that this isn’t really necessary in the specific case here since the sprite is the direct child of the scene so the sprite’s position property should already be what it is in the scene coordinate system.
I’ve spent a lot of googling time attempting to figure this out without much success which makes me think I’m asking the wrong question. If so, please help me figure out what the right question to ask is!
The touch position print returns the correct position relative to the initial screen size. That’s what I’d like to get for the sprite.
Disclaimer - i don’t do this professionally so please forgive what I’m sure is ugly coding!
import SwiftUI
import SpriteKit
import PlaygroundSupport
var screenW = CGFloat(2732)
var screenH = CGFloat(2048)
struct ContentView: View {
var scene: SKScene {
let scene = GameScene()
scene.size = CGSize(width: 2732, height: 2048)
scene.scaleMode = .aspectFit
scene.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
return scene
}
class GameScene: SKScene {
var ship1 = SKSpriteNode()
public override func didMove(to view: SKView) {
// gravity edge around screen for convenience
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
//turn off default 1g gravity
physicsWorld.gravity = .zero
//Add a ship sprite
let ship1 = SKSpriteNode(imageNamed: "ship.PNG")
ship1.setScale(0.2)
ship1.position = CGPoint(x: screenW * 0.2, y: screenH * 0.2)
ship1.physicsBody = SKPhysicsBody(rectangleOf: ship1.size)
ship1.physicsBody?.velocity = CGVector(dx: 0, dy: 100)
addChild(ship1)
}
//touch handling begin
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
//
let location = touch.location(in: self)
print(location)
print(ship1.position)
}
}
var body: some View {
SpriteView(scene: scene)
.ignoresSafeArea()
}
}
//star the view
let currentView = ContentView()
//Playground view setup
PlaygroundPage.current.setLiveView(currentView)
PlaygroundPage.current.wantsFullScreenLiveView = true
PlaygroundPage.current.needsIndefiniteExecution = true

Swift - Increase speed of a moving Node using SpriteKit

I am working on a game similar to pong, while starting the game I apply impulse to the ball in random direction, which is at the centre of the screen e.g.
func impulse(){
let randomNum:UInt32 = arc4random_uniform(200)
let someInt:Int = Int(randomNum)
//Ball Impulse
if someInt<49{
ball.physicsBody?.applyImpulse(CGVector(dx: ballSpeed+3, dy: ballSpeed-5))
}else if someInt<99{
ball.physicsBody?.applyImpulse(CGVector(dx: ballSpeed+5, dy: -ballSpeed+5))
}else if someInt<149{
ball.physicsBody?.applyImpulse(CGVector(dx: -ballSpeed-5, dy: -ballSpeed+5))
}else if someInt<200{
ball.physicsBody?.applyImpulse(CGVector(dx: -ballSpeed-3, dy: ballSpeed-5))
}
}
Where ballSpeed is the preset speed of ball.
So is there any way I can gradually increase the velocity of ball with duration while it is in motion? As ball will keep on bouncing around the screen so it is difficult to apply force to it using dx and dy.
EDIT -
I am using above method just to assign a random quadrant/direction to ball at start of the game.
Start, when impulse method is implemented e.g.
Game start image
And I want to increase speed of the ball by a predefined unit over time during gameplay e.g.
Gameplay
Ok, here you go! The ball CONSTANTLY gets faster, no matter what!!! Adjust amount to determine how much the ball gains speed each frame.
class GameScene: SKScene, SKPhysicsContactDelegate {
let ball = SKShapeNode(circleOfRadius: 20)
var initialDY = CGFloat(0)
var initialDX = CGFloat(0)
override func didMove(to view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
self.physicsWorld.gravity = CGVector.zero
// Configure ball pb:
let pb = SKPhysicsBody(circleOfRadius: 20)
ball.physicsBody = pb
addChild(ball)
pb.applyImpulse(CGVector(dx: 10, dy: 10))
}
override func update(_ currentTime: TimeInterval) {
initialDX = abs(ball.physicsBody!.velocity.dx)
initialDY = abs(ball.physicsBody!.velocity.dy)
}
override func didSimulatePhysics() {
guard let pb = ball.physicsBody else { fatalError() }
// decrease this to adjust the amount of speed gained each frame :)
let amount = CGFloat(5)
// When bouncing off a wall, speed decreases... this corrects that to _increase speed_ off bounces.
if abs(pb.velocity.dx) < abs(initialDX) {
if pb.velocity.dx < 0 { pb.velocity.dx = -initialDX - amount }
else if pb.velocity.dx > 0 { pb.velocity.dx = initialDX + amount }
}
if abs(pb.velocity.dy) < abs(initialDY) {
if pb.velocity.dy < 0 { pb.velocity.dy = -initialDY - amount }
else if pb.velocity.dy > 0 { pb.velocity.dy = initialDY + amount }
}
}
}
you can modify this to only increase speed every 5 seconds with an SKAction.repeatForever(.sequence([.wait(forDuration: TimeInterval, .run( { code } ))) but IMO having it constantly gain speed is a bit more awesome and easier to implement.

I made a hitbox sprite as a child to my player, in my player Class. how do I access that hitbox sprite in gamescene's didBegin method?

The purpose of this "hitbox" is so the player will only be able to jump while walking on top of ground/platforms.The hitbox is a little less wide then the player and is on the players feet. Here's my player class:
class Player: SKSpriteNode {
let maxPlayerSpeed:CGFloat = 300
static var isPlayerOnGround = false
init() {
//players texture
let texture = SKTexture(imageNamed: "playerMove1")
super.init(texture: texture, color: SKColor.clear, size: texture.size())
//hitbox that sits underneath the player and follows him
let jumpHitBox = SKSpriteNode(color: .red, size: CGSize(width: texture.size().width - (texture.size().width / 8), height: texture.size().height / 5))
jumpHitBox.position.y = (-texture.size().height) + (texture.size().height / 2)
jumpHitBox.alpha = 0.5
jumpHitBox.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: jumpHitBox.size.width,
height: jumpHitBox.size.height))
jumpHitBox.zPosition = 3
jumpHitBox.physicsBody?.pinned = true
jumpHitBox.physicsBody?.allowsRotation = false
jumpHitBox.physicsBody?.categoryBitMask = CollisionTypes.playerJump.rawValue
jumpHitBox.physicsBody?.collisionBitMask = 0
jumpHitBox.physicsBody?.contactTestBitMask = CollisionTypes.ground.rawValue
addChild(jumpHitBox)
physicsBody = SKPhysicsBody(rectangleOf: size)
physicsBody?.categoryBitMask = CollisionTypes.player.rawValue
physicsBody?.contactTestBitMask = CollisionTypes.star.rawValue | CollisionTypes.saw.rawValue | CollisionTypes.finish.rawValue
physicsBody?.collisionBitMask = CollisionTypes.ground.rawValue
physicsBody?.affectedByGravity = true
physicsBody?.restitution = 0.2
physicsBody?.isDynamic = true
physicsBody?.allowsRotation = false
setScale(0.6)
zPosition = 5
physicsBody?.linearDamping = 0.0 }
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) //error here
}
I want to use the jumpHitBox in this method as an additional contact.bodyA/B node in GameScene.swift:
func didBegin(_ contact: SKPhysicsContact) {
if contact.bodyA.node == player {
playerCollided(with: contact.bodyB.node!)
} else if contact.bodyB.node == player {
playerCollided(with: contact.bodyA.node!)
}
}
I don't know how to reference the the jumpHitBox child node from my player class in the didBegin in GameScene.swift.
Any advice is greatly appreciated.
EDIT: I'm getting an error at the
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) //error here
}
I dont put anything in here in my player class but since moving the jumpHitBox sprite to a global declaration I get an error at the super.init(coder: aDecoder) line in the required init saying: Property 'self.jumpHitBox' not initialized at super.init call
Although I would try the much simpler velocity.dy approach as mentioned in comments, your problem is that your hitbox is declared inside the scope of an initializer, so you can only access it there. If you give the hitbox a higher scope, such as a class property, then you can access it most anywhere:
class Player: SKSpriteNode {
let maxPlayerSpeed:CGFloat = 300
// This is probably going to cause you bugs later btw.. it should probably be
// just a regular property:
static var isPlayerOnGround = false
// Now you can just call playerInstance.jumpHitBox :
private(set) var jumpHitBox = SKSpriteNode()
init() {
//players texture
let texture = SKTexture(imageNamed: "playerMove1")
super.init(texture: texture, color: SKColor.clear, size: texture.size())
jumpHitBox = SKSpriteNode(color: .red, size: CGSize(width: texture.size().width - (texture.size().width / 8), height: texture.size().height / 5))
//hitbox that sits underneath the player and follows him
}
}
UPDATE:
class Player: SKSpriteNode {
let maxPlayerSpeed:CGFloat = 300
// This is probably going to cause you bugs later btw.. it should probably be
// just a regular property:
static var isPlayerOnGround = false
// Now you can just call playerInstance.jumpHitBox :
var jumpHitBox = SKSpriteNode()
private func makeHitBox() -> SKSpriteNode {
let texture = SKTexture(imageNamed: "playerMove1")
//hitbox that sits underneath the player and follows him
let localJumpHitBox = SKSpriteNode(color: .red, size: CGSize(width: texture.size().width - (texture.size().width / 8), height: texture.size().height / 5))
localJumpHitBox.position.y = (-texture.size().height) + (texture.size().height / 2)
localJumpHitBox.alpha = 0.5
localJumpHitBox.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: localJumpHitBox.size.width,
height: localJumpHitBox.size.height))
localJumpHitBox.zPosition = 3
localJumpHitBox.physicsBody?.pinned = true
localJumpHitBox.physicsBody?.allowsRotation = false
localJumpHitBox.physicsBody?.categoryBitMask = CollisionTypes.playerJump.rawValue
localJumpHitBox.physicsBody?.collisionBitMask = 0
localJumpHitBox.physicsBody?.contactTestBitMask = CollisionTypes.ground.rawValue
return localJumpHitBox
}
init() {
//players texture
let texture = SKTexture(imageNamed: "playerMove1")
super.init(texture: texture, color: SKColor.clear, size: texture.size())
physicsBody = SKPhysicsBody(rectangleOf: size)
physicsBody?.categoryBitMask = CollisionTypes.player.rawValue
physicsBody?.contactTestBitMask = CollisionTypes.star.rawValue | CollisionTypes.saw.rawValue | CollisionTypes.finish.rawValue
physicsBody?.collisionBitMask = CollisionTypes.ground.rawValue
physicsBody?.affectedByGravity = true
physicsBody?.restitution = 0.2
physicsBody?.isDynamic = true
physicsBody?.allowsRotation = false
setScale(0.6)
zPosition = 5
physicsBody?.linearDamping = 0.0
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) //error here
jumpHitBox = makeHitBox()
addChild(jumpHitBox)
}
}
One way to reference jumpHitBox from outside its class is by making it a property of the Player class as in let jumpHitBox: SKSpriteNode, in the same way as you have declared maxPlayerSpeed to be a property of the Player class.
If you make this change, then remember to remove the let in this line let jumpHitBox = SKSpriteNode(color: .red .... since you now just need to assign a value to it, instead of declaring it. You should also now move the call to super.init to be after this line. Otherwise the compiler will complain, i.e. all properties of a class must be assigned a value before calling super.init.
The jumpHitBox should be now be accessible in didBegin by using player.hitBox
Hope this helps!
Here is a secondary answer showing a different approach to your problem. Note, the player only jumps when on the ground. This uses a "delayed frame" or "frame skip" or however you want to put it, because the jump command (pb.applyImpulse) is only called inside of didSimulatePhysics which means the character won't actually go any higher until the next frame.
// NOTE: This is a very simple example. The player bounces a bit on landing,
// which is essentially "landing lag" before you can jump again. In other words,
// the less the player bounces when contact the ground, the faster the player
// can jump again. There are ways to make this bouncing effect minimal / 0, but would
// require more work to implement, and I don't have any brilliantly simple ideas at the moment.
class GameScene : SKScene {
var player = SKSpriteNode(color: .blue, size: CGSize(width: 50, height: 50))
var initialY = CGFloat(0)
var flag_wantsToJump = false
override func didMove(to view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
let pb = SKPhysicsBody(rectangleOf: player.size)
pb.restitution = 0
player.physicsBody = pb
addChild(player)
}
override func mouseDown(with event: NSEvent) {
// Tells game that we want to jump next frame:
flag_wantsToJump = true
}
override func update(_ currentTime: TimeInterval) {
// Give us new, initial frame data to compare against whatever our position.y will be
// later in the frame:
let curY = player.position.y
initialY = curY
}
override func didSimulatePhysics() {
// Determine whether or not we want to jump next frame:
guard flag_wantsToJump else { return }
// Determine whether or not we are allowed to jump:
let curY = player.position.y
print(curY, initialY)
if curY == initialY {
// Even though we are calling .applyImpulse this frame, it won't be processed
// until next frame--because we are calling this from inside `didSimulatePhysics`!
player.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 75))
}
}
override func didFinishUpdate() {
// Clear our flags for new mouse / touch input for next frame:
flag_wantsToJump = false
}
}

Sprites show up in simulator, not on device

This simple project inserts a square sprite in the center of the field when you press the button, which calls the add() function below. In the simulator, when you add multiple sprites, it pushes the others out of the way, so when you have pressed it a bunch of times you get...
screen shot from simulator, iphone 6, iOS 9.2, and this is the behavior I want.
But the same code running on my iphone, after adding the same number of sprites yields this... screen show from physical iphone 6, iOS 9.2
Here is the code from GameScene.swift:
import SpriteKit
class GameScene: SKScene {
override init(size: CGSize) {
super.init(size:size)
self.physicsWorld.gravity = CGVectorMake(0, -1.0)
let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = worldBorder
self.physicsBody?.friction = 0.5
}
func add()
{
let sprite = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 10, height: 10))
sprite.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
sprite.physicsBody = SKPhysicsBody(circleOfRadius: 8)
sprite.physicsBody?.friction = 0.0
sprite.physicsBody?.affectedByGravity = false
sprite.physicsBody?.restitution = 0.5
sprite.physicsBody?.linearDamping = 0.5
addChild(sprite)
}
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
}
Where am I going wrong? And how to I get the behavior I want on the real iPhone?
I'm not sure if which one is the desired behavior, but it does make sense that they wouldn't slide if you are stacking them literally right on top of each other. That being said, there is a way to get around this.
Keep in mind that nodes use floating point positioning, but of course they can only actually visibly be positioned by the pixel.
1 pixel would be 0.5 points on a 2x screen, and 0.33 points on 3x screen. With that in mind, you can use an offset of < 0.33 to get the physics bodies offset without it being visible to the user. Here's a little example:
class GameScene: SKScene {
var xOffset: CGFloat = 0.05
var yOffset: CGFloat = 0.3
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
override init(size: CGSize) {
super.init(size:size)
self.physicsWorld.gravity = CGVectorMake(0, -1.0)
let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = worldBorder
self.physicsBody?.friction = 0.5
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
add()
updateOffsets()
}
func add()
{
let sprite = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 10, height: 10))
sprite.position = CGPointMake((self.frame.size.width / 2) + xOffset, (self.frame.size.height / 2) + yOffset)
sprite.physicsBody = SKPhysicsBody(circleOfRadius: 8)
sprite.physicsBody?.friction = 0.0
sprite.physicsBody?.affectedByGravity = false
sprite.physicsBody?.restitution = 0.5
sprite.physicsBody?.linearDamping = 0.5
addChild(sprite)
}
private func updateOffsets() {
xOffset = -xOffset
yOffset = -yOffset
}
}
Switching the offsets is the important part. If you don't do that, you'll have the same problem of stacking exactly on top of each other. The offsets I used get pretty close to the simulator behavior, but a few taps in you'll notice it's a little different. Hopefully you don't care about matching the exact same pattern you're getting in simulator. It's a very similar one with this code. If you do care, you'll notice changing the offsets will change the pattern.

Prevent two SKSpriteNodes from overlapping

I want to reproduce Super-Mario for iOS using Swift and SpriteKit. I use SKPhysics-bodies to simulate collisions between the player and the environment. The player and all objects have their own SKPhysicsBody (rectangle of their size). But when the player jumps against a brick (from top left or top right) like this, the player stucks in the air.
My assumption is that when the two SKSpriteNodes collide, they overlap a tiny bit. And the SKPhysics-Engine thinks that the player is on top of the middle brick because the player is a tiny bit inside the brick above and falls down on the middle brick.
So my question is: How can i prevent SKSPriteNodes from overlapping? Or how can i fix this?
P.S. If you need more information to answer, please tell me!
EDIT: Some Code of the Brick Class:
import SpriteKit
class Brick: SKSpriteNode {
let imgBrick = SKTexture(imageNamed: "Brick")
init(let xCoor: Float, let yCoor: Float) {
// position is a global variable wich is 1334/3840
super.init(texture: imgBrick, color: UIColor.clearColor(), size: CGSize(width: 80*proportion, height: 80*proportion))
position = CGPoint(x:Int(xCoor*667)+Int(40*proportion), y:Int(375-(yCoor*375))-Int(40*proportion))
name = "Brick"
physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
physicsBody?.affectedByGravity = false
physicsBody?.dynamic = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and my Player Class
import SpriteKit
class Mario: SKSpriteNode {
let atlas = SKTextureAtlas(named: "Mario")
var imgMario = [SKTexture]()
var action = SKAction()
init(let xCoor: Float, let yCoor: Float) {
for(var i = 1; i < 24; i++) {
imgMario.append(atlas.textureNamed("X\(i)"))
}
super.init(texture: imgMario[0], color: UIColor.clearColor(), size: CGSize(width: 120*proportion, height: 160*proportion))
position = CGPoint(x:Int(xCoor)+Int(60*proportion), y:Int(yCoor)-Int(90*proportion))
name = "Mario"
physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
physicsBody?.allowsRotation = false
action = SKAction.repeatActionForever(SKAction.animateWithTextures(imgMario, timePerFrame: 0.03, resize: false, restore: true))
runAction(action, withKey: "walking")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and SOME PART of my Level Class
class Level: SKScene, SKPhysicsContactDelegate {
var motionManager = CMMotionManager()
var mario = Mario(xCoor: 100, yCoor: 90)
var world = SKSpriteNode()
let MarioCategory: UInt32 = 0x1 << 0
let BrickCategory: UInt32 = 0x1 << 1
/*
Some more code...
*/
func setUpPhysics() {
physicsWorld.contactDelegate = self
mario.physicsBody!.categoryBitMask = marioCategory
mario.physicsBody?.collisionBitMask = brickCategory
mario.physicsBody?.contactTestBitMask = brickCategory
for(var i = 0; i < world.children.count; i++) {
if(world.children[i].name == "Brick") {
world.children[i].physicsBody?.categoryBitMask = brickCategory
world.children[i].physicsBody?.collisionBitMask = marioCategory
world.children[i].physicsBody?.contactTestBitMask = marioCategory
world.children[i].physicsBody?.friction = 0
}
}
}
}
Try setting UsePreciseCollisionDetection to true on your physicsbody.
mario.physicsBody?.usesPreciseCollisionDetection = true
https://developer.apple.com/reference/spritekit/skphysicsbody has some examples.
By setting this property to true could cause it to detect collisions more accurately so only when you mean to. I'd also rethink giving each block its own physics body. You only really care about the edge of a group of blocks rather than the edges of each individual block.
Another suggestion is that you can create a physics body based on a texture. So if your SKSpriteNode has a complex texture with transparent bits you can create another texture that is just a simple black block or similar this will help the engine detect more accurate collisions.

Resources