Keep track of first node in line of nodes - ios

I've set up my game to have the same node repeatedly added from the bottom with different texture to move up to the top of the screen. I need a method to keep track of the node always leading this line of nodes. So if the original first node gets removed, the one behind it takes its place as the first node and I'm able to keep track of it. This is the code I've used to create my line of nodes. Any help would be appreciated.
EDIT: I tried making an SKNode and placing it above the ball to check its name but it just returns nil.
class GameScene: SKScene {
var aboveNode = SKNode()
override func didMoveToView(view: SKView) {
var create = SKAction.runBlock({() in self.createBottomTargets()})
var wait = SKAction.waitForDuration(2)
var createAndWait = SKAction.repeatActionForever(SKAction.sequence([create, wait]))
self.runAction(createAndWait, withKey: "firstBottom")
}
override func update(currentTime: NSTimeInterval) {
println("\(aboveNode.name)")
}
func createBottomTargets() {
ball = SKSpriteNode(imageNamed: "blue1")
ball.position = CGPoint(x: frame.size.width / 2, y: -50)
ball.texture = textures[random]
self.addChild(ball)
let move = SKAction.moveByX(0, y: 1400, duration: 18)
let remove = SKAction.removeFromParent()
ball.runAction(SKAction.sequence([move, remove]))
aboveNode.position = CGPoint(x: ball.position.x, y: ball.position.y + 163)
}
}

Depending on how other parts of your game works, you could set it up so that each subsequent node is a child node of the node before it. That way the child nodes will move with their parent nodes. Imagine a vertical stack of nodes set up this way, each of which is 50px tall. Each node has a local position of [0, 50] to place it atop its parent. If you remove the bottom node and add it's child nodes back to the scene, all of the descendent nodes will appear to have moved down because their position is relative to their parent node.

Related

Is it acceptable to create an SKShapeNode in update delegate?

I create an SKShapeNode in SpriteKit's update delegate and it works exactly as I want it to. My only concern is that update is called every tenth of a second. Does that mean that my code is also run that often? That doesn't seem very performance friendly. Is there a way around it? Or is it fine having the code on update?
Why is the code in update in the first place you might ask. Well if I put it in didMove or sceneDidLoad then when I rotate the device the node doesn't follow along instantly, it remains in its old place for half a second before relocating to its new position, making it look like it jumps. Bad. Update solves this. Here's the code:
override func update(_ currentTime: TimeInterval) {
let mySquare = SKShapeNode(rectOf: CGSize(width: 50, height: 50))
mySquare.fillColor = SKColor.blue
mySquare.lineWidth = 1
mySquare.position = CGPoint(x: 0, y: 0)
self.addChild(mySquare)
}
}
Yes you can create. the only issue you have is it's creating a new node each time update is called. At the moment you can not see multiple nodes because you're positioning them on top of each other if you try to change the position of mySquare on each update call you will see that you have multiple nodes. created which is not effective soon you will run out of frame rates. as you don't have any animations on going you don't see the difference now.
override func update(_ currentTime: TimeInterval) {
let mySquare = SKShapeNode(rectOf: CGSize(width: 50, height: 50))
mySquare.fillColor = SKColor.blue
mySquare.lineWidth = 1
mySquare.position = CGPoint(x: randomX, y: randomY)
self.addChild(mySquare)
}
changing the position of the X and Y will give you the chance to see the nodes being added to different position on the screen.
if for any reason you want to create your mySquare node in the update then you can keep a reference for the node or search the node in the parent node if it's available then don't add any
var mySquare: SKShapeNode
Then in the update check if your Square node is not nil or has the same
override func update(_ currentTime: TimeInterval) {
if mySquire == nil {
mySquare = SKShapeNode(rectOf: CGSize(width: 50, height: 50))
mySquire.name = "your square name"
mySquare.fillColor = SKColor.blue
mySquare.lineWidth = 1
mySquare.position = CGPoint(x: randomX, y: randomY)
self.addChild(mySquare)
}
}
Note: you can even check for the name like
if mySquare.name == "your square name"
then don't add it any more
Doing anything on the update func is a bad idea
Apple Documentation:
Do not call this method directly; it is called by the system exactly once per frame, so long as the scene is presented in a view and is not paused. This is the first method called when animating the scene, before any actions are evaluated and before any physics are simulated.
If you want to create objects and control them, you should use didMove func for example. And you can create an action like this for example:
self.run(
SKAction.repeatForever(
SKAction.customAction(withDuration: 0.0, actionBlock: { (_, _) in
// Create and add new node...
})
)
)
But probably create a several nodes infinitely is a bad idea.

How can I get the position of a moving SKSpriteNode?

I have an SKSpriteNode that moves back and forth.
I need to get it's position as it moves (so it changes all the time) and get different positions as it moves across the screen.
When I try and receive the position it comes as the original position.
Object movement:
let right = SKAction.moveBy(x: self.frame.width, y: 0, duration: 3)
let left = SKAction.moveBy(x: -self.frame.width, y: 0, duration: 3)
And this is what I'm trying to do to get it's position printed as it goes.
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
pos = String(describing: blockposition.x)
print(pos)
}
Is it possible to get one that updates as it moves?
in your "class"
1_ type var sprite(the name of your sprite) = SKSpriteNode()
2_ then add your synod to the scene
3_ then in the update func call print(sprite(the name of your sprite).position.x or y) as you want hope it worked for you
You can't do it this way.
From https://developer.apple.com/documentation/spritekit/skaction?changes=_1
Observing Changes to Node Properties
Generally, actions do not call public methods on nodes. For example, if you wanted to subclass SKNode to respond to a move(to:duration:) action, you may consider overriding its position property to add a didSet observer (see Overriding Property Observers).
class MovingNode: SKSpriteNode {
override var position: CGPoint {
didSet {
// code to react to position change
}
}
}
However, because a move action running on an instance of MovingNode doesn't set its position, the observer isn't invoked and your code to react to a position change is never executed.
In this example, the solution is to use SKSceneDelegate. By comparing a node's position in the delegate's update(_:for:) method - which is called at the beginning of each frame - to its position in the delegate's didEvaluateActions(for:) method - which is called after any actions have been evaluated - you can check if it has moved and react accordingly.
Listing 7 shows an example of how you can implement this solution.
Listing 7
Responding to a position change during a running action
let node = SKNode()
var nodePosition = CGPoint()
func update(_ currentTime: TimeInterval, for scene: SKScene) {
nodePosition = node.position
}
func didEvaluateActions(for scene: SKScene) {
let distance = hypot(node.position.x - nodePosition.x,
node.position.y - nodePosition.y)
if distance > 0 {
// code to react to position change
}
}

How to make enemy have independent lives?

PLEASE HELP!!! I have been trying to figure this out for along time. I have searched the internet and i cannot find anything that will help me.
I am currently making a game in which you are a space ship in the middle and enemy ships are moving towards you and you have to shoot them. some enemies have different lives. for example: a red ship takes one shot to explode, the blue ship takes 3, etc. I have everything to work only the lives. for example: whenever a blue ship is called on to the screen i shoot it once so its life goes down to 2. but whenever a another blue ship is called the first blue ship has its life reset back to 3 again. Is there anyway I can make it so that whenever a ship looses lives it remains that way even if other ships are called ?
this is my ship function that gets called and adds enemy space ships onto the screen:
func VillainRight(){
let TooMuch = self.size.width
let point = UInt32(TooMuch)
life = 3
let VillainR = SKSpriteNode(imageNamed: "BlueVillain")
VillainR.zPosition = 2
VillainR.position = CGPoint(x: self.frame.minX,y: CGFloat(arc4random_uniform(point)))
//This code makes the villain's Zposition point towards the SpaceShip
let angle = atan2(SpaceShip.position.y - VillainR.position.y, SpaceShip.position.x - VillainR.position.x)
VillainR.zRotation = angle - CGFloat(M_PI_2)
let MoveToCenter = SKAction.move(to: CGPoint(x: self.frame.midX, y: self.frame.midY), duration: 15)
//Physics World
VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
VillainR.physicsBody?.categoryBitMask = NumberingPhysics.RightV
VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
VillainR.physicsBody?.affectedByGravity = false
VillainR.physicsBody?.isDynamic = true
VillainR.run(MoveToCenter)
addChild(VillainR)
}
This is the code that calls this function:
_ = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(Level1.VillainRight), userInfo: nil, repeats: true)
I am using the spritekit in Swift.
Thank You Very Much in advance!
That is happening because life variable is declared as a property of a scene and it is not local to a specific node (enemy ship). You can solve this in a few ways... First way would be using node's userData property:
import SpriteKit
let kEnergyKey = "kEnergyKey"
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMove(to view: SKView) {
let blueShip = getShip(energy: 3)
let greenShip = getShip(energy: 2)
let redShip = getShip(energy: 1)
if let blueShipEnergy = blueShip.userData?.value(forKey: kEnergyKey) as? Int {
print("Blue ship has \(blueShipEnergy) lives left")
//hit the ship
blueShip.userData?.setValue(blueShipEnergy-1, forKey: kEnergyKey)
if let energyAfterBeingHit = blueShip.userData?.value(forKey: kEnergyKey) as? Int {
print("Blue ship has \(energyAfterBeingHit) lives left")
}
}
}
func getShip(energy:Int)->SKSpriteNode{
//determine which texture to load here based on energy value
let ship = SKSpriteNode(color: .purple, size: CGSize(width: 50, height: 50))
ship.userData = [kEnergyKey:energy]
return ship
}
}
This is what the docs say about userData property:
You use this property to store your own data in a node. For example,
you might store game-specific data about each node to use inside your
game logic. This can be a useful alternative to creating your own node
subclasses to hold game data.
As you can see, an alternative to this is subclassing of a node (SKSpriteNode):
class Enemy:SKSpriteNode {
private var energy:Int
//do initialization here
}

SKSpriteNode doesn't maintain current position after moveToParent function

The SKNode method moveToParent does not maintain its absolute position after moving to a new parent in the same scene. The node moves to another position.
The documentation for moveToParent says
Moves the node to a new parent node in the scene. The node maintains its current position in scene coordinates.
Did I miss something?
update_01:
I realize something new for me.
The moving node maintains its position only if its initial position is zero points.If it has a initial position, after moveToParent function, it moves, which makes me confuse as compared to apple documentation.
As I understand from "maintain", it does not move on the screen, just its position values are changed.
and please do not consider scene!.scaleMode, because it was tested without this(and ofcourse proper anchorPoint) and nothing changed.
This is my code and screenshots:
import SpriteKit
class GameScene: SKScene , UIGestureRecognizerDelegate{
var counter:Int = 0
var touchTracker : [UITouch : SKNode] = [:]
let yellowParentNode = SKSpriteNode()
let blueParentNode = SKSpriteNode()
override func didMoveToView(view: SKView)
{
scene!.scaleMode = SKSceneScaleMode.ResizeFill
let yellowPosition = CGPointMake(100, 100)
let bluePosition = CGPointMake(200[![enter image description here][1]][1], 100)
//yellow parent
yellowParentNode.name = "yellow"
yellowParentNode.color = UIColor.yellowColor()
yellowParentNode.size.height = 50
yellowParentNode.size.width = 50
yellowParentNode.anchorPoint = CGPoint(x: 0 , y: 0)
yellowParentNode.position = yellowPosition
self.addChild(yellowParentNode)
//blue parent
blueParentNode.name = "blue"
blueParentNode.color = UIColor.blueColor()
blueParentNode.size.height = 50
blueParentNode.size.width = 50
blueParentNode.position = bluePosition
blueParentNode.anchorPoint = CGPoint(x: 0 , y: 0)
self.addChild(blueParentNode)
//child
let childNode = SKSpriteNode()
childNode.color = UIColor.redColor()
childNode.size.width = 20
childNode.size.height = 20
childNode.name = "child"
childNode.position = CGPointMake(40, 40)
childNode.anchorPoint = CGPoint(x: 0 , y: 0)
yellowParentNode.addChild(childNode)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let touch = touches.first as UITouch!
let touchLocation = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(touchLocation)
touchTracker[touch as UITouch] = touchedNode
if touchedNode.name == "child"
{
yellowParentNode.childNodeWithName("child")!.moveToParent(blueParentNode)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touch = touches.first as UITouch!
let touchLocation = touch.locationInNode(self)
let touchedNode = touchTracker[touch as UITouch]
touchedNode?.position = touchLocation
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
{
// let touch = touches.first as UITouch!
touchTracker.removeAll()
}
override func update(currentTime: CFTimeInterval)
{
/* Called before each frame is rendered */
}
}
this is the initial screen:
and this is next screen,which is after touching the child node:
You may be misinformed here, and Alessandro's answer does not explain why things are happening.
The position property is always relative to the parent. This is not what scene coordinates are. If you want to get the scene coordinates, you need to convert the position to the scene.
Now when you use moveToParent, you obviously are moving the node's parent property to the new parent. This means that the nodes position has to change, since the parent is changing. Now the node position has to change in order to adjust for the new parent, so that is still stays on the same spot on the scene.
E.G.
Node 1 lies on 10, 10 to the scene, Node 2 lies on 20,10 to the scene.
Node A is a child of node 1, and is positioned at 0,0 (scene coordinate (10,10))
We then move Node A from node 1 to node 2.
If position never changes (stays at (0,0)), this means that when viewed on screen, the scene coordinate would be 20,10, which is wrong
The position would have to change to (-10,0) because you take the parent (20,10) and need to adjust it by (-10,0) to get the scene coordinate of Node A back to (10,10)
I think there might be a misunderstanding in the interpretation of the information provided by Apple. In fact "moves the node to a new parent node" it could understand that your node is already a child of a node in the same scene.
If you make for example:
let pos1 = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
let pos2 = CGPointMake(CGRectGetMidX(self.frame)/2,CGRectGetMidY(self.frame)/2)
// make a big yellow node
let bigNode1 = SKSpriteNode.init(color: UIColor.yellowColor(), size: CGSizeMake(400,400))
addChild(bigNode1)
bigNode1.position = pos1
// make a big blue node
let bigNode2 = SKSpriteNode.init(color: UIColor.blueColor(), size: CGSizeMake(400,400))
addChild(bigNode2)
bigNode2.position = pos2
// make a little red node as a child of bigNode1
let littleNode = SKSpriteNode.init(color: UIColor.redColor(), size: CGSizeMake(100,100))
bigNode1.addChild(littleNode)
littleNode.position = CGPointZero
after you have this output:
Now if you do :
littleNode.moveToParent(bigNode2)
the output not change, exactly as explained by Apple.
Update (after 0x141E comment):
#0x141E As you can see, the position remain always 10,10 so it's correct:

Gradually increasing speed of scrolling background in SpriteKit

I am making a simple game in SpriteKit, and I have a scrolling background. What simply happens is that a few background images are placed adjacent to each other when the game scene is loaded, and then the image is moved horizontally when it scrolls out of the screen. Here is the code for that, from my game scene's didMoveToView method.
// self.gameSpeed is 1.0 and gradually increases during the game
let backgroundTexture = SKTexture(imageNamed: "Background")
var moveBackground = SKAction.moveByX(-self.frame.size.width, y: 0, duration: (20 / self.gameSpeed))
var replaceBackground = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
var moveBackgroundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackground, replaceBackground]))
for var i:CGFloat = 0; i < 2; i++ {
var background = SKSpriteNode(texture: backgroundTexture)
background.position = CGPoint(x: self.frame.size.width / 2 + self.frame.size.width * i, y: CGRectGetMidY(self.frame))
background.size = self.frame.size
background.zPosition = -100
background.runAction(moveBackgroundForever)
self.addChild(background)
}
Now I want to increase the speed of the scrolling background at certain points of the game. You can see that the duration of the background's horizontal scroll is set to (20 / self.gameSpeed). Obviously this does not work, because this code is only run once, and therefore the movement speed is never updated to account for a new value of the self.gameSpeed variable.
So, my question is simply: how do I increase the speed (reduce the duration) of my background images' movements according to the self.gameSpeed variable?
Thanks!
You could use the gameSpeed variable to set the velocity of the background. For this to work, firstly, you need to have a reference to your two background pieces (or more if you so wanted):
class GameScene: SKScene {
lazy var backgroundPieces: [SKSpriteNode] = [SKSpriteNode(imageNamed: "Background"),
SKSpriteNode(imageNamed: "Background")]
// ...
}
Now you need your gameSpeed variable:
var gameSpeed: CGFloat = 0.0 {
// Using a property observer means you can easily update the speed of the
// background just by setting gameSpeed.
didSet {
for background in backgroundPieces {
// Minus, because the background is moving from left to right.
background.physicsBody!.velocity.dx = -gameSpeed
}
}
}
Then position each piece correctly in didMoveToView. Also, for this method to work each background piece needs a physics body so you can easily change its velocity.
override func didMoveToView(view: SKView) {
for (index, background) in enumerate(backgroundPieces) {
// Setup the position, zPosition, size, etc...
background.physicsBody = SKPhysicsBody(rectangleOfSize: background.size)
background.physicsBody!.affectedByGravity = false
background.physicsBody!.linearDamping = 0
background.physicsBody!.friction = 0
self.addChild(background)
}
// If you wanted to give the background and initial speed,
// here's the place to do it.
gameSpeed = 1.0
}
You could update gameSpeed in update for example with gameSpeed += 0.5.
Finally, in update you need to check if a background piece has gone offscreen (to the left). If it has it needs to be moved to the end of the chain of background pieces:
override func update(currentTime: CFTimeInterval) {
for background in backgroundPieces {
if background.frame.maxX <= 0 {
let maxX = maxElement(backgroundPieces.map { $0.frame.maxX })
// I'm assuming the anchor of the background is (0.5, 0.5)
background.position.x = maxX + background.size.width / 2
}
}
}
You could make use of something like this
SKAction.waitforDuration(a certain amount of period to check for the updated values)
SKAction.repeatActionForever(the action above)
runAction(your action)
{ // this is the completion block, do whatever you want here, check the values and adjust them accordly
}

Resources