Search code examples
swiftxcodesprite-kitcollisionskspritenode

Increasing the size of a spriteNode using spriteKit in update function (Swift)


I'm having troubles with animations, physics and stuff like these.

Anyway, what I'm trying to do is creating a pulsing effect for a sphere where the pulse itself is able to collide with elements on the screen, so I created another spriteNode (child of the sphere) that I want to scale continuously, without depending on touches.

I read that in these cases is better not to use SKActions.

My question is: how do I scale or increase the size of a spriteNode via update function?

The code represents how I worked so far for movement for example.

import SpriteKit
import GameplayKit

class GameScene: SKScene {

 var entities = [GKEntity]()
 var graphs = [String: GKGraph]()

 var movingSquareLv1 = SKSpriteNode()
 var pulsingSphereLv2 = SKSpriteNode()

 var lv2SpherePulse = SKSpriteNode()


 var xVeloicty: CGFloat = 100


 override func sceneDidLoad() {

  movingSquareLv1 = self.childNode(withName: "movingSquare") as!SKSpriteNode
  movingSquareLv1.physicsBody = SKPhysicsBody(rectangleOf: movingSquareLv1.size)
  movingSquareLv1.physicsBody ? .affectedByGravity = false

  pulsingSphereLv2 = self.childNode(withName: "pulsingSphere") as!SKSpriteNode
  pulsingSphereLv2.physicsBody ? .affectedByGravity = false

  lv2SpherePulse = pulsingSphereLv2.childNode(withName: "lv2SpherePulse") as!SKSpriteNode
  lv2SpherePulse.physicsBody ? .affectedByGravity = false


 }


 func touchDown(atPoint pos: CGPoint) {

 }

 func touchMoved(toPoint pos: CGPoint) {

 }

 func touchUp(atPoint pos: CGPoint) {

 }

 override func touchesBegan(_ touches: Set < UITouch > , with event: UIEvent ? ) {

  for t in touches {
   self.touchDown(atPoint: t.location( in: self))
  }
 }

 override func touchesMoved(_ touches: Set < UITouch > , with event: UIEvent ? ) {
  for t in touches {
   self.touchMoved(toPoint: t.location( in: self))
  }
 }

 override func touchesEnded(_ touches: Set < UITouch > , with event: UIEvent ? ) {
  for t in touches {
   self.touchUp(atPoint: t.location( in: self))
  }
 }

 override func touchesCancelled(_ touches: Set < UITouch > , with event: UIEvent ? ) {
  for t in touches {
   self.touchUp(atPoint: t.location( in: self))
  }
 }


 override func update(_ currentTime: TimeInterval) {

  rectMovement()


 }

 func rectMovement() {

  if movingSquareLv1.frame.maxX >= self.size.width / 2 {

   xVeloicty = -100

  } else if movingSquareLv1.frame.minX <= -self.size.width / 2 {
   xVeloicty = 100
  }

  let rate: CGFloat = 0.5
  let relativeVelocity: CGVector = CGVector(dx: xVeloicty - movingSquareLv1.physicsBody!.velocity.dx, dy: 0)
  movingSquareLv1.physicsBody!.velocity = CGVector(dx: movingSquareLv1.physicsBody!.velocity.dx + relativeVelocity.dx * rate, dy: 0)

 }

}

This is the object composed of two sprites, the one I want to work on is the largest one.

1


Solution

  • Whoever told you to not use SKActions is wrong. This is exactly when you want to use an SKAction, and if you have multiple circles pulsing, you use the same action on all circles.

    You really want to keep your update function as small as possible. Trust in apple’s optimizations because they have better access to the API than you do.