I am trying to create a SKSpriteNode from a class (Planets), to my GameScene, when a button is pressed. My problem is that the border doesn't appear on screen.
class Planets: SKSpriteNode, EventListenerNode, InteractiveNode {
var gameScene: GameScene!
func didMoveToScene(){
isUserInteractionEnabled = true
gameScene = (SKScene(fileNamed: "GameScene") as! GameScene)
}
func interact() {
print("hi \(self.name ?? "Planet X")")
let border = SKSpriteNode(color: SKColor.gray, size: CGSize(width: 200, height: 200))
border.name = "border"
border.zPosition = 100
border.position = CGPoint(x: gameScene.frame.size.width/2, y: gameScene.frame.size.height/2)
gameScene.addChild(border)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
interact()
}
}
However, if I use self.addChild,(self meaning the Planets class) it works, so it works if I add the border to a SKSpriteNode, but I don't want that because the size differ, the position differ too. I just want to add it to my GameScene.
Thank you in advance!
I think the problem here is that the Planets class is creating its own copy of GameScene with this line here:
gameScene = (SKScene(fileNamed: "GameScene") as! GameScene)
That copy is never presented and so it's never visible and neither is the border that was added to it.
If Planets is a child of GameScene then I would try referencing the parent of the Planets node when you add the border like this:
func interact() {
print("hi \(self.name ?? "Planet X")")
guard let gameScene = self.parent as? GameScene else {
print("Error: could not get the parent scene")
return
}
let border = SKSpriteNode(color: SKColor.gray, size: CGSize(width: 200, height: 200))
border.name = "border"
border.zPosition = 100
border.position = CGPoint(x: gameScene.frame.size.width/2, y: gameScene.frame.size.height/2)
gameScene.addChild(border)
}
Related
Im attempting to use some of the code from a solution found on this page for creating a button Create Button in SpriteKit: Swift
class GameScene: SKScene {
let button = SKSpriteNode(imageNamed: "yourImgName")
override func didMoveToView(view: SKView) {
button.name = "btn"
button.size.height = 100
button.size.width = 100
button.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 50)
self.addChild(button)
//Adjust button properties (above) as needed
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name {
if name == "btn" {
let yourNextScene = YourNextScene(fileNamed: "YourNextScene")
self.view?.presentScene(yourNextScene!)
}
}
}
}
and the current code that I have is supposed to make the player jump when the button is pressed, but nothing is currently happening when its pressed
import SwiftUI
import SpriteKit
import UIKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let button = SKSpriteNode(imageNamed: "playerOBJ")
let player = SKSpriteNode(imageNamed: "playerOBJ")
let playerRadius = player.frame.width / 2.0
player.position = CGPoint(x: 200, y: 500)
player.name = "Jimmy"
addChild(player)
player.physicsBody = SKPhysicsBody(circleOfRadius: playerRadius)
player.physicsBody?.allowsRotation = false
player.physicsBody?.friction = 0
player.physicsBody?.restitution = 0
player.zPosition = 100
// Button
button.name = "btn"
button.size.height = 100
button.size.width = 100
button.position = CGPoint(x: 100, y: 100)
self.addChild(button)
// Physics
physicsBody = SKPhysicsBody(edgeLoopFrom: frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)))
Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { _ in
player.physicsBody?.applyForce(CGVector(dx: 100, dy: 1000))
}
func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let positionInScene = touch!.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name {
if name == "btn" {
player.physicsBody?.applyForce(CGVector(dx: 0, dy: 10000))
}
}
}
}
override func update(_ currentTime: TimeInterval) { }
}
I'm thinking that maybe this is an issue with the press not being resitered at all but I'm not fully sure
Your main problem is you put all of your code inside the didMove function. You put the touchesBegan function inside the didMove function. When the didMove function finishes, touchesBegan goes away so none of your touches are going to be handled in the game.
You also declared the button and the player as local variables inside the didMove function.
override func didMove(to view: SKView) {
let button = SKSpriteNode(imageNamed: "playerOBJ")
let player = SKSpriteNode(imageNamed: "playerOBJ")
// Rest of function omitted
}
When the didMove function finishes, the button and player go away too. You also have the same image name for the player and button.
The fix is to make the button and player variables properties of the GameScene class. Move touchesBegan out of the didMove function too.
class GameScene: SKScene {
// Declare button and player here.
var button = SKSpriteNode()
var player = SKSpriteNode()
override func didMove(to view: SKView) {
// Initialize button, player, and everything else here.
}
func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Code in touchesBegan func goes here.
}
}
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)
}
}
I'm developing a game where you have a character. The character is a subclass of an SKSpriteNode:
class Character: SKSpriteNode {
init() {
let texture = SKTexture(imageNamed: "character")
super.init(texture: texture, color: UIColor.white, size: texture.size())
self.isUserInteractionEnabled = true
self.zPosition = 10
self.position = CGPoint(x: 0, y: 0)
self.name = "character"
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("Touch!")
// Create small +1 sprite
let plusNode = SKSpriteNode(imageNamed: "plus1Node")
plusNode.zPosition = 10000 //To make sure its always in front
plusNode.position = self.position
self.addChild(plusNode)
}
The character is added to the game via GameScene.swift:
func spawnCharacter() {
//Random postion for the pigs
let randomX = Functions().randomBetweenNumbers(firstNum: -140, secondNum: 140)
let randomY = Functions().randomBetweenNumbers(firstNum: -240, secondNum: 240)
let newCharacter = Character()
newCharacter = CGPoint(x: randomX, y: randomY)
gameArea!.addChild(newCharacter)
}
When the player taps the character a little "+1" node is spawned to show the player that he has touched the character. I want that +1 node to be spawned on top of the character, but it's placed way off - like multiple points away.
If I move the +1 code to the GameScene (just after the character is created and added to the scene the position is spot on).
What am I missing here?
PulzeNode.position = CGpoint.zero
As you add it to the character, it should be at the origin of parent node
This is from a simple game in SpriteKit with a Ball() class that has a function shieldOn() which, for the moment, simply replaces the texture of a single ball to that of a ball surrounded by a shield.
The ball is created like this in GameScene:
func getBall() {
let ball = Ball()
ball.createBall(parentNode: self)
}
Here is the Ball class
class Ball: SKSpriteNode {
func createBall(parentNode: SKNode) {
let ball = SKSpriteNode(texture: SKTexture(imageNamed: "ball2"))
ball.physicsBody = SKPhysicsBody(circleOfRadius: 25)
ball.name = "ball"
parentNode.addChild(ball)
ball.size = CGSize(width: 50, height: 50)
ball.position = CGPoint(x: 20, y: 200)
launch(spriteNode: ball, parentNode: parentNode)
}
private func launch(spriteNode: SKSpriteNode, parentNode: SKNode) {
spriteNode.physicsBody?.applyImpulse(CGVector(dx: 5, dy: 0))
}
func shieldOn() {
self.texture = SKTexture(imageNamed: "ballShield")
}
func shieldOff() {
self.texture = SKTexture(imageNamed: "ball2")
}
}
In the main section of my code (GameScene.swift) I don't have a reference to the ball. So I cycle through all of the nodes on the screen and try to cast the matching one as shown below. I crash with an error saying that it could not cast value of type SKSpriteNode to Ball.
for node in self.children {
if node.name == "ball" {
let ball = node as! Ball
ball.shieldOn()
}
}
I've tried a few variations with no luck. Am I at least working in the right direction? Thanks!
With the new information I think you want something like this:
Ball Class:
class Ball: SKSpriteNode{
init() {
let texture = SKTexture(imageNamed: "ball2")
let size = CGSize(width: 50, height: 50)
super.init(texture: texture, color: UIColor.clear, size: size)
self.name = "ball"
self.physicsBody = SKPhysicsBody(circleOfRadius: size.height/2)
self.physicsBody?.applyImpulse(CGVector(dx: 5, dy: 0))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func shieldOn() {
self.texture = SKTexture(imageNamed: "ballShield")
}
func shieldOff() {
self.texture = SKTexture(imageNamed: "ball2")
}
}
Then use this to create the ball:
func getBall() {
let ball = Ball()
ball.position = CGPoint(x: 20, y: 200)
scene?.addChild(ball)
}
Perhaps a better way to do this would be to keep an array of all Balls created and added to the scene. Then you could just iterate through your array and update their texture. You would not need to enumerate them on the screen, which can decrease performance if there are many moving sprites.
As far as your code goes, it looks like you might be affected by this bug:
https://forums.developer.apple.com/thread/26362
Ok so I'm trying to make so when Button1 is clicked, it gets removed and replaced by Button2. When Button2 gets clicked, I want it to remove button2 and replace it with button1, etc.. Here's my code I have so far (The buttons are functional so that is not the problem.)
//Sound Button1
soundButton.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height * 0.42)
soundButton.zPosition = 15
self.addChild(soundButton)
Here's the function that runs when Button1 is clicked:
func sound() {
soundButton.removeFromParent()
soundButton2.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height * 0.42)
soundButton2.zPosition = 15
self.addChild(soundButton2)
}
Here's the function that runs when Button2 is clicked:
func sound1() {
soundButton2.removeFromParent()
self.addChild(soundButton)
}
Lastly, here is my code for the buttons getting clicked in the touchesEnded function:
//Sound1 Button Pressed
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if soundButton.containsPoint(location) {
sound()
for touch: AnyObject in touches {
_ = touch.locationInNode(self)
}
}
}
//Sound1 Button Pressed
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if soundButton2.containsPoint(location) {
sound1()
for touch: AnyObject in touches {
_ = touch.locationInNode(self)
}
}
}
I'd just use the hidden property to toggle the visibility of both buttons. Set the one you want displayed to hidden = false and the one you want to hide as hidden = true
You are overcomplicating this. What you need is just to remove the button which is tapped, and add an appropriate one:
import SpriteKit
class GameScene: SKScene{
let soundButton = SKSpriteNode(color: UIColor.purpleColor(), size:CGSize(width: 200, height: 200))
let soundButton2 = SKSpriteNode(color: UIColor.orangeColor(), size:CGSize(width: 200, height: 200))
override func didMoveToView(view: SKView) {
soundButton.name = "button1"
soundButton2.name = "button2"
soundButton.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
soundButton.zPosition = 15
self.addChild(soundButton)
soundButton2.position = soundButton.position
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
if let location = touch?.locationInNode(self){
let node = nodeAtPoint(location)
if node.name == "button1" {
node.removeFromParent()
//Not really needed for this example, but a good habit
if soundButton2.parent == nil {
addChild(soundButton2)
}
}else if node.name == "button2"{
node.removeFromParent()
//Not really needed for this example, but a good habit
if soundButton.parent == nil {
addChild(soundButton)
}
}
}
}
}
The other way way would be to subclass a SKSpriteNode and make your Button class (Button:SKSpriteNode), set its userInteractionEnabled property to true, and implement its touchesBegan/touchesEnded methods.
Or you can use SKAButton class which has a lot of different functionalities (mimics the usefulness of UIButton).