I'm trying to get my "Player" (A circle in the middle) to increase in size once the screen is touched by running a timer.
Once the timer is over 0 seconds, it increases in size. Once the timer is over 3 seconds, it decreases to its original scale size and once the timer is over 7 seconds, it resets and this repeats forever.
What am I doing wrong?
import SpriteKit
class GameScene: SKScene {
var Center = SKSpriteNode()
var Player = SKSpriteNode()
var timer = NSTimer()
var seconds = 0
override func didMoveToView(view: SKView) {
Center = SKSpriteNode(imageNamed: "Center")
Center.size = CGSize(width: 80, height: 80)
Center.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(Center)
Player = SKSpriteNode(imageNamed: "Player")
Player.size = CGSize(width: 80, height: 80)
Player.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
self.addChild(Player)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: #selector(GameScene.playerScaleUp), userInfo: nil, repeats: true)
}
func playerScaleUp(){
if seconds > 0{
Player.runAction(SKAction.scaleBy(4, duration: 2))
}
}
func playerScaleDown(){
if seconds > 3{
Player.runAction(SKAction.scaleBy(-4, duration: 2))
}
}
func resetScale(){
if seconds > 7{
timer.invalidate()
}
}
override func update(currentTime: CFTimeInterval) {
}
}
This can be done in a few different ways:
1) Using SKActions (which my example uses)
2) Using update: method and its passed currentTime parameter
3) Using NSTimer which I wouldn't recommend because it is not affected by scene's, view's or node's paused state, so it can lead you into troubles in the future. Read more in this StackOverflow answer
4) Well, probably some more, but I will stop here.
My example uses SKAction. This means that I don't really check how much time is passed, but rather I organize actions into sequences (where actions are executed sequentially) and into groups (where actions are organized parallel). Means I use SKActions like I am playing with LEGO's :)
Here is the code ...I left debugging code intentionally, because it can help you to learn how you can use SKActions in different situations.
class GameScene: SKScene, SKPhysicsContactDelegate {
let player = SKSpriteNode(color: .blackColor(), size: CGSize(width: 50, height: 50))
var timePassed = 0.0 {
didSet {
self.label.text = String(format: "%.1f",timePassed)
}
}
let label = SKLabelNode(fontNamed: "ArialMT")
override func didMoveToView(view: SKView) {
/* Debugging - Not really needed, I added it just because of a better example */
let wait = SKAction.waitForDuration(0.1)
let updateLabel = SKAction.runBlock({[unowned self] in self.timePassed += wait.duration})
self.label.position = CGPoint(x: frame.midX, y: frame.midY+100.0)
addChild(self.label)
let timerSequence = SKAction.sequence([updateLabel, wait])
self.runAction(SKAction.repeatActionForever(timerSequence), withKey: "counting")
//This is used later, at the end of scaleUpAndDownSequence to reset the timer
let resetTimer = SKAction.runBlock({[unowned self] in self.timePassed = 0.0})
/* End Debugging */
self.player.position = CGPoint(x: frame.midX, y: frame.midY)
addChild(self.player)
let waitBeforeScaleToOriginalSize = SKAction.waitForDuration(3.0)
let waitBeforeRestart = SKAction.waitForDuration(4.0)
let scaleUp = SKAction.scaleTo(2.0, duration: 1)
let scaleDown = SKAction.scaleTo(1.0, duration: 1)
let scaleUpGroup = SKAction.group([waitBeforeScaleToOriginalSize, scaleUp])
let scaleDownGroup = SKAction.group([scaleDown, waitBeforeRestart])
//One cycle, scale up, scale down, reset timer
let scaleUpAndDownSequence = SKAction.sequence([scaleUpGroup, scaleDownGroup, resetTimer])
let loop = SKAction.repeatActionForever(scaleUpAndDownSequence)
self.player.runAction(loop, withKey: "scalingUpAndDown")
}
}
So here, I have two groups of actions:
1) scaleUpGroup
2) scaleDownGroup
scaleUpGroup group of actions has two actions in it: a scaleUp action, and an action which says how much to wait before the scale down action should occur. Because we want scaling up to happen immediately, we run it in parallel with the waitBeforeScaleToOriginalSize.
Same logic goes for scaleDownGroup. When scaleUpGroup is finished (its duration is determined by the longest action in the group) we start scaleDownGroup which scales down the player to its default size, and waits certain amount of time to repeat the whole thing.
Here is the result:
I start an animation on touch ( I've removed that code) and as you can see, scale up animation starts immediately, then after 3 seconds the player get scaled down to its original size, and after 4 seconds the whole animation repeats (player gets scaled up again etc).
Related
I am trying to write a function where I can increase SKShapeNode radius every second, but don't know how:(
var eRadius: CGFloat = 20
var eCircle = SKShapeNode()
override func didMove(to view: SKView) {
super.didMove(to: view)
eCircle = SKShapeNode(circleOfRadius: eRadius)
eCircle.strokeColor = .black
eCircle.glowWidth = 1.0
eCircle.fillColor = .white
eCircle.fillTexture = SKTexture(imageNamed: "e")
addChild(eCircle)
gameTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(addRadius), userInfo: nil, repeats: true)
}
#objc func addRadius(){
eRadius += 1
???? :(
}
Thank you!
Here's a working solution:
import SpriteKit
class GameScene: SKScene {
private var eRadius: CGFloat = 20
private var eCircle = SKShapeNode()
override func didMove(to view: SKView) {
eCircle.strokeColor = .black
eCircle.glowWidth = 1.0
eCircle.fillColor = .white
eCircle.fillTexture = SKTexture(imageNamed: "e")
addChild(eCircle)
run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
self.updateCirclePath()
self.eRadius += 1
}, SKAction.wait(forDuration: 1.0)])))
}
private func updateCirclePath() {
eCircle.path = UIBezierPath(ovalIn: CGRect(x: -eRadius * 0.5, y: -eRadius * 0.5, width: eRadius, height: eRadius)).cgPath
}
}
Basically, you create the circle using a UIBezierPath and then update it as your radius changes. For the updating you should use a forever-repeating SKAction sequence that both sets the path, then changes the radius. I chose to do the path first, then update the radius for the next upcoming turn, for clarity.
You can also do it the opposite way, so run the update function first to set the initial path, then wait one second, after which you change the radius, then update the path based on that radius.
In SpriteKit, you should use SKActions for timers instead of scheduling or dispatch queues. With SKActions, everything conforms to the same game time, which you can then control by pausing and whatever.
Also, there are multiple ways to make a circle using UIBezierPath, including ovals, rounded rectangles and arcs: https://developer.apple.com/documentation/uikit/uibezierpath.
I am struggling with one issue. Global declaration of my sprite so that I can interact with it. In this game, I have created a local sprite called enemy featured below:
func spawnEnemy() {
let enemy = SKSpriteNode(imageNamed: "as")
let yPosition = CGFloat(frame.maxY - enemy.size.height)
let getXvalue = GKRandomDistribution(lowestValue: Int(frame.minX + enemy.size.width), highestValue: Int(frame.maxX - enemy.size.width))
let xPosition = CGFloat(getXvalue.nextInt())
enemy.position = CGPoint(x: xPosition, y: yPosition)
enemy.name = "asteroid"
enemy.zPosition = 100
addChild(enemy)
let animationDuration:TimeInterval = 6
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: xPosition, y: 0), duration: animationDuration))
actionArray.append(SKAction.self.removeFromParent())
enemy.run(SKAction.sequence(actionArray))
}
I want to tap the enemy to make it disappear from the screen. The variable is declared locally and not globally so the touchesBegan function does not "see" enemy. However, when I move the statement:
let enemy = SKSpriteNode(imageNamed: "as")
outside of local declaration and into global. It works until the code tries to spawn in another enemy and i get an error of "Tried to add an SKNode who already has a parent" This is the code I have running in my view did load:
run(SKAction.repeatForever(SKAction.sequence([SKAction.run{self.spawnEnemy()
}, SKAction.wait(forDuration: 1.0)])))
Every time it spawns a new enemy it crashes and says that the SKNode already has a parent which i understand. However, for my game to function I need the player to be able to touch the individual instance of that enemy and remove it. Hence my code for
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in:self) {
let nodesArray = self.nodes(at:location)
if nodesArray.first?.name == "asteroid" {
print("Test")
enemy.removeFromParent()
print("Test Completed")
}
}
}
Now the error says unresolved use of "enemy" because the enemy is not global. I have been going back and forth on this issue for quite some time. If anyone has any potential solution or work around I would be very grateful, and thank you for your help.
Move your enemies to their own class and handle the touch for each of those enemies in their own class. This cleans up your GameScene and keeps your code more organized. You can now add as many instances of enemy as you want.
FYI not related to this question but somethings to consider after you get this working
when game over or level change or win make sure you have a clean up function to remove all enemies
you should strongly consider recycling your objects vs creating them on the fly...better performance
try to separate as much code to your objects class as possible
class enemy: SKSpriteNode {
init() {
super.init(texture: nil, color: .clear, size: CGSize.zero)
setup()
}
func setup() {
isUserInteractionEnabled = true
name = "asteroid"
zPosition = 100
let image = SKSpriteNode(imageNamed: "as")
imagine.zPosition = 1
addChild(image)
self.size = image.size
animate()
}
func animate() {
let animationDuration: TimeInterval = 6
let move = SKAction.move(to: CGPoint(x: xPosition, y: 0), duration: animationDuration)
let remover = SKAction.self.removeFromParent()
run(SKAction.sequence(move, remover))
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
removeFromParent()
}
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
let sequence = SKAction.sequence([SKAction.run{ self.spawnEnemy()
}, SKAction.wait(forDuration: 1.0)])
run(SKAction.repeatForever(sequence))
}
func spawnEnemy() {
let enemy = Enemy()
let yPosition = CGFloat(frame.maxY - enemy.size.height)
let getXvalue = GKRandomDistribution(lowestValue: Int(frame.minX + enemy.size.width), highestValue: Int(frame.maxX - enemy.size.width))
let xPosition = CGFloat(getXvalue.nextInt())
enemy.position = CGPoint(x: xPosition, y: yPosition)
addChild(enemy)
}
}
so I'm working with a game in SpriteKit and I am using this code to pause the game.
self.pauseButton.alpha = 0
self.playButton.alpha = 1
self.settingsBackground.alpha = 0.85
self.isPaused = true
self.pauseButton.alpha = 1
self.playButton.alpha = 0
self.settingsBackground.alpha = 0
The code which is run before pausing is to change the appearance of the pause and the code afterward reverts it. The problem is that the code before pausing is not getting run and instead the game is just pausing before changing the visuals around. I've tried adding delays, making the steps SKActions, and testing just the code before the pause. When I run just the first 3 lines the visuals change properly but obviously, the game doesn't pause. When I run the whole thing the game pauses but the visuals don't change. Help!
the problem is that even when a scene isPaused the code still get run. Yes you are pausing the scene, yes the visuals do get run...but the visuals after the isPaused line also get run and reset the visuals that you just changed.
here is a really simple example of how this working. there are 3 boxes on the scene; the bottom box has an action that repeatedly scales up and down, and will stop when the scene is paused. the top box will pause the game when pressed and will reveal the middle box which will unpause the game.
class GameScene: SKScene {
private var test: SKSpriteNode!
private var test2: SKSpriteNode!
private var test3: SKSpriteNode!
override func didMove(to view: SKView) {
backgroundColor = .clear
test = SKSpriteNode(color: .blue, size: CGSize(width: 200, height: 200))
test.position = CGPoint(x: 0, y: 350)
addChild(test)
test2 = SKSpriteNode(color: .red, size: CGSize(width: 200, height: 200))
test2.position = CGPoint(x: 0, y: 0)
test2.alpha = 0
addChild(test2)
test3 = SKSpriteNode(color: .yellow, size: CGSize(width: 200, height: 200))
test3.position = CGPoint(x: 0, y: -350)
addChild(test3)
let scaleDown = SKAction.scale(to: 0.1, duration: 1.0)
let scaleUp = SKAction.scale(to: 1.0, duration: 1.0)
let sequence = SKAction.sequence([scaleDown, scaleUp])
let repeater = SKAction.repeatForever(sequence)
test3.run(repeater)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch! {
let touchLocation = touch.location(in: self)
if test.contains(touchLocation) {
print("pausing")
self.test2.alpha = 1
self.test.alpha = 0
self.isPaused = true
}
if test2.contains(touchLocation) {
print("unpausing")
self.test2.alpha = 0
self.test.alpha = 1
self.isPaused = false
}
}
}
}
Running into some trouble with my code. I'm trying to make zombies follow my player around with the following code:
class GameScene: SKScene, SKPhysicsContactDelegate {
func Enemies() {
Enemy = SKSpriteNode(imageNamed: "Spaceship")
Enemy.size = CGSize(width: 50, height: 50)
Enemy.color = UIColor(red: 0.9, green: 0.1, blue: 0.1, alpha: 1.0)
Enemy.colorBlendFactor = 1.0
//physics
Enemy.physicsBody = SKPhysicsBody(rectangleOf: Enemy.size)
Enemy.physicsBody?.isDynamic = true
Enemy.physicsBody?.affectedByGravity = false
Enemy.name = "Enemy"
Enemy.position.y = -frame.size.height/2
let positionX = arc4random_uniform(UInt32(frame.size.width))
Enemy.position.x = CGFloat(positionX)
addChild(Enemy)
}
override func didMove(to view: SKView) {
enemyTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(GameScene.Enemies), userInfo: nil, repeats: true)
override func update(_ currentTime: TimeInterval) {
Enemy.run(SKAction.move(to: ship.position, duration: 3))
}
If I run this code, I can spawn the zombies, but they will not follow my main player, they will just go to the position that he was at when they were spawned (i.e. zombie spawned at time = 0 will go to the ship position at time = 0, zombie spawned at time = 1 will go to the ship position at time = 1, and so on). However, if I run this code while only spawning one zombie like so:
override func didMove(to view: SKView) {
Enemies()
}
The lone zombie will follow my player around. Any idea why the code works for one zombie, but not for multiple zombies?
I would not recommend constantly adding actions on your update cycle, you are going to suffer from performance loss due to all the things that happen behind the scenes. Instead, use an SKAction.customAction that you would only add to your sprite once.
Here is an example of a custom action that will do what you want, remember only assign it once. (Code is not tested, so may need edits)
let customActionBlock =
{
(node,elapsedTime) in
let dx = ship.x - node.position.x
let dy = ship.y - node.position.y
let angle = atan2(dx,dy)
node.position.x += sin(angle) * speedPerFrame
node.position.y += cos(angle) * speedPerFrame
}
let duration = TimeInterval(Int.max) //want the action to run infinitely
let followPlayer = SKAction.customAction(withDuration:duration,actionBlock:customActionBlock)
Enemy.run(action:followPlayer,withKey:"follow")
Maybe you should removeAllActions() on Enemy before readding an action. It seems that you have actions that take 3 seconds, but you add an action every frame, so it has at most 180 actions for a node at once.
Here is an illustration of what I am trying to do:
Here is my code so far:
import SpriteKit
class GameScene: SKScene {
let mySquare1 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))
let mySquare2 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))
override func didMoveToView(view: SKView) {
mySquare1.position = CGPoint(x: 100, y:100)
mySquare2.position = CGPoint(x: 300, y:100)
mySquare1.fillColor = SKColor.blueColor()
mySquare2.fillColor = SKColor.blueColor()
self.addChild(mySquare1)
self.addChild(mySquare2)
let moveAction1 = SKAction.moveTo(CGPoint(x:250, y:100), duration: 1)
mySquare1.runAction(moveAction1)
let moveAction2 = SKAction.moveTo(CGPoint(x:300, y:350), duration: 1)
mySquare2.runAction(moveAction2)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func update(currentTime: CFTimeInterval) {
}
}
My problem is, I am trying to move the rectangles synchronously (not asynchronously). That is, I want my first rectangle start moving, finish its movement, stop. And then, start my second rectangle moving, finish its movement and stop.
Currently what happens is that, when I run my program, they both start moving at the same time.
I also found SKAction.sequence for actions to play in order, however, I only can use this for actions on the same object. Not in different objects like in my example.
If you want to move the two rectangles sequentially (not in parallel), you could use the completion property of the first action like this (apple docs):
import SpriteKit
class GameScene: SKScene {
let mySquare1 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))
let mySquare2 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))
override func didMoveToView(view: SKView) {
mySquare1.position = CGPoint(x: 100, y:100)
mySquare2.position = CGPoint(x: 300, y:100)
mySquare1.fillColor = SKColor.blueColor()
mySquare2.fillColor = SKColor.blueColor()
self.addChild(mySquare1)
self.addChild(mySquare2)
let move1 = SKAction.moveToX(250, duration: 1.0)
let move2 = SKAction.moveToY(250, duration: 1.0)
self.mySquare1.runAction(move1,completion: {
self.mySquare2.runAction(move2)
})
}
}
You could use the SKAction for running blocks. Then sequence them from the scene.
For example:
func example() {
let firstRectMove = SKAction.runBlock({
let move1 = SKAction.moveToX(20, duration: 1.0)
let move2 = SKAction.moveToY(20, duration: 1.0)
rec1.runAction(SKAction.sequence([move1,move2]))
})
let actionWait = SKAction.waitForDuration(2.0)
let secondRectMove = SKAction.runBlock({
let move1 = SKAction.moveToX(20, duration: 1.0)
let move2 = SKAction.moveToY(20, duration: 1.0)
rec2.runAction(SKAction.sequence([move1,move2]))
})
//self is an SKScene or any other node really...
self.runAction(SKAction.sequence([firstRectMove,actionWait,secondRectMove]))
}
As Alessandro Ornano suggests, a way to accomplish this is run your first action with a completion block, where a block of code runs after the completion of the original action. A downside to this approach is that it can create a pyramid of doom when you need to chain together more than two actions at a time.
An alternative way to avoid to the "pyramid" is to define a method that calls itself recursively for each subsequent action in an array:
func runInSequence(actions:[(node:SKNode,action:SKAction)], index:Int) {
if index < actions.count {
let node = actions[index].node
let action = actions[index].action
node.runAction(action) {
// Avoid retain cycle
[weak self] in
self?.runInSequence(actions, index: index+1)
}
}
}
To use this, define/create an array that stores the nodes and actions you want to run in sequence, and call the method with the starting index:
let nodesAndActions:[(node:SKNode,action:SKAction)] = [
(mySquare1,moveAction1),
(mySquare2,moveAction2)
]
runInSequence(nodesAndActions, index: 0)