I'm new to Xcode 7, how do I add a black node in SpriteKit?
Can anyone point me in the right direction or link me up with any tutorials please? Thank you
You want to create a new SKSpriteNode object, and add it to the view
class GameScene: SKScene {
var node: SKSpriteNode?
override func didMoveToView(view: SKView) {
let node = SKSpriteNode(imageNamed: "BlackNode.png")
node.position.x = self.size.width / 2
node.position.y = self.size.height / 2
addChild(node)
}
}
So in your GameScene class (or whatever SKScene class you are using), this creates a SKSpriteNode that is the image of whatever "BlackNode.png" is, and sets its position to the center of the screen, and adds it to scene
Assuming your question is asking how to add an all black node to the scene, try this:
override func didMoveTo(view: SKView) {
let node = SKSpriteNode(color: UIColor.black, size: CGSize(width: 100, height: 100)) // Declare and initialize node
addChild(node) // Function that adds node to scene
}
This uses the SKSpriteNode initializer init(color: UIColor, size: CGSize), which you would pass a color for the node and size. This is then used to create a rectangle, while the nodes texture property remains nil.
Note that in this example, no position is explicitly chosen, so the default (0,0) is chosen (centre of scene if scene anchorPoint is 0.5,0.5)
To get a further understanding of scenes vs views and SpriteKit vs UIKit (which I think is where you're having trouble) check out this answer: SpriteKit vs UIKit
Related
All right, so let's say I have two SKShapeNodes (or any SKNode related object), one is a circle, and the other is a square. I'm just trying to put the square inside the circle so it looks like this:
However, if I simply add the Square Shape Node inside the Circle Shape Node, as it's child, it just ends up looking like this:
How do I limit the Circle's children to the circle's bounds? For those images, I basically created a new project with the default GameKit template, and it's GameScene.swift code is basically this:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var circleNode = SKShapeNode()
var squareNode = SKShapeNode()
override func didMove(to view: SKView) {
// Setup Shape Nodes:
circleNode = SKShapeNode(circleOfRadius: 100)
circleNode.fillColor = .blue
squareNode = SKShapeNode(rectOf: CGSize(width: 200, height: 200))
squareNode.fillColor = UIColor.gray.withAlphaComponent(0.5)
// Positioning Shape Nodes:
circleNode.position = CGPoint(x: 0, y: 0)
squareNode.position = CGPoint(x: 0, y: -circleNode.frame.height/2) // At the middle of Circle Node!
// Inserting Circle Node in the Game Scene:
self.addChild(circleNode)
// Inserting Square Shape Node in the Circle Node:
circleNode.addChild(squareNode)
}
}
How can I accomplish this behavior? Any help would be greatly appreciated!
I'm trying to create a hud that sits at the top of the screen and doesn't move with the rest of the scene. I was able to get a static view up there with this:
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let hud = SKView()
hud.alpha = 0.5
hud.backgroundColor = SKColor.greenColor()
hud.frame = CGRect(x: 0.5, y: 0.5, width: view.bounds.width, height: 50)
self.view?.addSubview(hud)
So with that, my player can walk around the scene, the camera follows him, but there is always a transparent view across the top which is what I think I want.
So next I tried adding things to the hud view at the top but I can't seem to figure it out. For example, if I create an SKLabel node I can't add it with hud.addChild because SKView doesn't have addChild. I tried doing hud.addSubview but a SKLabel isn't a UIView, which is what addSubview expects.
Does anyone know how I add things to the hud like labels?
I am creating a Terraria-style game in Swift. I want to have it so the player node is always in the center of the screen, and when you move right the blocks go left like in Terraria.
I am currently trying to figure out how to keep the view centered on the character. Does anyone know of a good way of accomplishing this?
Since iOS 9 / OS X 10.11 / tvOS, SpriteKit includes SKCameraNode, which makes a lot of this easier:
positioning the camera node automatically adjusts the viewport
you can easily rotate/zoom the camera by transform in the camera node
you can fix HUD elements relative to the screen by making them children of the camera node
the scene's position stays fixed, so things like physics joints don't break the way they do when you emulate a camera by moving the world
It gets even better when you combine camera nodes with another new feature, SKConstraint. You can use a constraint to specify that the camera's position is always centered on a character... or add extra constraints to say, for example, that the camera's position must stay within some margin of the edge of the world.
The below will center the camera on a specific node. It can also smoothly transition to the new position over a set time frame.
class CameraScene : SKScene {
// Flag indicating whether we've setup the camera system yet.
var isCreated: Bool = false
// The root node of your game world. Attach game entities
// (player, enemies, &c.) to here.
var world: SKNode?
// The root node of our UI. Attach control buttons & state
// indicators here.
var overlay: SKNode?
// The camera. Move this node to change what parts of the world are visible.
var camera: SKNode?
override func didMoveToView(view: SKView) {
if !isCreated {
isCreated = true
// Camera setup
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.world = SKNode()
self.world?.name = "world"
addChild(self.world)
self.camera = SKNode()
self.camera?.name = "camera"
self.world?.addChild(self.camera)
// UI setup
self.overlay = SKNode()
self.overlay?.zPosition = 10
self.overlay?.name = "overlay"
addChild(self.overlay)
}
}
override func didSimulatePhysics() {
if self.camera != nil {
self.centerOnNode(self.camera!)
}
}
func centerOnNode(node: SKNode) {
let cameraPositionInScene: CGPoint = node.scene.convertPoint(node.position, fromNode: node.parent)
node.parent.position = CGPoint(x:node.parent.position.x - cameraPositionInScene.x, y:node.parent.position.y - cameraPositionInScene.y)
}
}
Change what’s visible in the world by moving the camera:
// Lerp the camera to 100, 50 over the next half-second.
self.camera?.runAction(SKAction.moveTo(CGPointMake(100, 50), duration: 0.5))
Source: swiftalicio - 2D Camera in SpriteKit
For additional information, look at Apple's SpriteKit Programming Guide (Example: Centering the Scene on a Node).
You have to create World node that contains nodes. And you should put anchorPoint for example (0.5,0.5). Center on your player. And then you should move your player.
func centerOnNode(node:SKNode){
let cameraPositionInScene:CGPoint = self.convertPoint(node.position, fromNode: world!)
world!.position = CGPoint(x:world!.position.x - cameraPositionInScene.x, y: world!.position.y - cameraPositionInScene.y)
}
override func didSimulatePhysics() {
self.centerOnNode(player!)
}
I'm unable to add a SKSpriteNode to a scene in Spritekit with the folling code:
func addScoreNode(){
scoreNode = SKSpriteNode(color: SKColor.blueColor(), size: CGSizeMake(100,100))
scoreNode!.position = CGPointMake(50, 450)
addChild(scoreNode!)
}
When I create the node with a texture, it gets added to the screen.
Any help would be greatly appreciated.
Im not sure if you are just trying to display text which would be a SKLabelNode or if you are trying to apply a picture to the node But try this at the top of your scene under the class Gamescene : SKScene add
var scoreNode = SKSpriteNode()
And then instead of the func addScoreNode() put this in the didMoveToView
scoreNode.color = UIColor.blueColor()
scoreNode.size = CGSizeMake(100,100)
scoreNode.position = CGPointMake(50, 450)
scoreNode.zPosition = 5 //this brings it to the front of the screen if there are multiple things at this position
and either in the didMoveToView or where ever you want it to be called and added to screen
self.addChild(scoreNode)
I would like to know how to remove my SKNodes when they are off screen to help my game run more smoothly.
How To Do This On Sprite Kit
Thanks So Much
Here is an easy solution in Swift 4:
class GameScene: SKScene {
let s = SKLabelNode(fontNamed: "Chalkduster")
override func didMove(to view: SKView) {
s.text = "test"
s.fontSize = 50
addChild(s)
let moveRight = SKAction.moveBy(x: 40, y: 0, duration: 0.5)
s.run(SKAction.repeatForever(moveRight))
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if ((s.parent != nil) && !intersects(s)) {
s.removeFromParent()
print("Sprite removed.")
}
}
}
You have a sprite (in this case a SKLabelNode but any sprite node will do) that is moving horizontally and you want to delete this sprite once is out of the frame bounds.
You can use the intersects function to check this and then remove that sprite from its parent. I have also checked that the sprite has a parent before removing it (by checking if s.parent is not nil) as we wish to remove the sprite only once.
https://stackoverflow.com/a/24195006/2494064
Here is a link to an answer that removes nodes that go off the top of the screen. You would just have to replicate this to cover the entire border and set all of the walls to have the same contactBitMask values.
Basically the logic is to remove the SKSpriteNodes when they contact physicsbodies that you have resting just outside the visible screen.