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)
Related
I have two sks files, one with nodes and another with actions both created in Scene Editor. I want to load nodes and add them to another node and then apply actions to them, something like:
override func sceneDidLoad() {
let heroScene = SKScene(fileNamed: "Hero")
let head = heroScene?.childNode(withName: "HeroHead") as! SKSpriteNode
let testAction = SKAction(named: "HeroTestAction") as! SKAction
let loop = SKAction.repeatForever(testAction)
let hero = SKSpriteNode(color: SKColor.white.withAlphaComponent(0) , size: CGSize(width: 50, height: 250))
let body = SKSpriteNode(color: SKColor.blue, size: CGSize(width: 50, height: 50))
head.removeFromParent()
hero.addChild(head)
hero.addChild(body)
self.addChild(hero)
head.run(loop)
body.run(loop)
}
But action does not apply to head. If i apply loop to body or hero it works.
What I do wrong? Thanks!
Update: When I first time run example, action not works, but if close app and run again it is works.
I could want to say there is an interesting "feature" where when you grab a reference sprite like that it comes in as isPaused = true;
Try adding this
head.isPaused = false;
I believe I ran into a similar problem before when grabbing a sprite from another scene.
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
can someone code for me please how to add an overlay start scene in swift?
I want to add a startMenu like in Flappy Bird, and then when you click play it just moves/fades away and the game begins.
Can someone help me achieve this effect?
Looking to achieve this.
I will be more than thankful!
Try to post some code of what you have tried so far when asking a question on stack overflow, people tend to not write code for you.
You can basically do this 2 ways.
1)
Either create a StartScene and transition to GameScene with your preferred SKTransition when the play button is pressed e.g SKTransition.crossFade
2)
If you want to do it in the same scene you can just create a SKNode/SKSpriteNode class to use as a menu.
class Menu: SKSpriteNode {
lazy var playLabel: SKLabelNode = {
let label = SKLabelNode(fontNamed: "HelveticaNeue")
label.text = "Play"
label.fontSize = 22
label.fontColor = .yellow
label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
return label
}()
/// Init with size
init(size: CGSize) {
super.init(texture: nil, color: .red, size: size)
addChild(playLabel)
}
}
}
Than in your GameScene Scene you can add the menu like so with your preferred size, in this example the size of the scene.
class GameScene: SKScene {
lazy var menu: Menu = Menu(size: self.size)
override func didMove(to view: SKView) {
addChild(menu)
}
}
You can add other nodes for UI, including sprites for buttons etc, to that menu class.
Than in GameScene in touches Began you can look for nodes that are touched, e.g the play button and than do your thing e.g to animate out the menu
let action1 = SKAction.fadeAlpha(to: 0, duration: 0.5)
let action2 = SKAction.removeFromParent()
let sequence = SKAction.sequence([action1, action2])
menu.run(sequence)
If you stuck there and want more coding samples you should google how to do this, is basic SpriteKit stuff that you need to know and there is plenty tutorials available. This includes samples of how to make button subclasses using SKSpriteNodes.
Hope this helps
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 created this game in xcode using Sprite Kit and Swift and I am trying to figure out how I can detect when the hero makes contact with a wall. So far this is my code. However I can't seem to find collision when I do SKPhysicsBody.
class GameScene: SKScene {
var movingGround: TFMovingGround!
var hero: TFHero!
var isStarted = false
var wallGenerator: TFWallGenerator!
override func didMoveToView(view: SKView) {
backgroundColor = UIColor(red: 159.0/255.0, green:201.00/255.0, blue:244.0/255.0, alpha:1.0)
movingGround = TFMovingGround(size:CGSizeMake(view.frame.width, kMLGroundHeight))
movingGround.position = CGPointMake(0, view.frame.size.height/2)
addChild(movingGround)
hero = TFHero()
hero.position = CGPointMake(70,movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
addChild(hero)
hero.breath()
//Add WallGenerator
wallGenerator = TFWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
wallGenerator.position = view.center
addChild(wallGenerator)
}
}
This is my code in the GameScene.
Any help would be much appreciated.
A difficult way to go about it would be to get the frame of HeroSprite and get the frame for WallsSprite and see if they overlap, using
CGRectIntersectsRect(rect1, rect2)
and doing this continuously.
A better solution would be to use collision detection. There is a pretty good tutorial here.