How to access the events and elements inside a SpriteKit GameScene? - swift

I'm new to SpriteKit and starting to collect some experience.
What I currently can not understand is how to mix a GameScene with my own code?
So, there is a way to do all manually like this:
class GameScene: SKScene {
override func didMove(to view: SKView) {
//...
}
override func update(_ currentTime: TimeInterval) {
//...
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//...
}
}
And then pass it to my SpriteView:
SpriteView(scene: GameScene())
This is fine, so I can do everything in Swift by code. To use a .sks GameScene file its also easy by using:
SpriteView(scene: SKScene(fileNamed: "MyScene")!)
BUT what I need to do to override the methods in MyScene and access the Sprites I created via the editor?

I found it in the example. I can use the Custom Class property in the .sks file

Related

How do I make a SKSpritenode move to the touch location in swift SpriteKit

Hi I am making a game where a node should move to the touch location and I have asked before and it is still not working. Here is my code, hopefully you can help me. I have a file called Player.swift with this code inside it:
import SpriteKit
class Player: SKSpriteNode {
let playerTexture = SKTexture(imageNamed: "head")
init() {
super.init(texture: playerTexture, color: .clear, size: playerTexture.size())
}
// Satisfy the NSCoder required init.
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}}
and then I have the gamescene.swift file with this inside of it:
import SpriteKit
class GameScene: SKScene {
let player = Player()
override func didMove(to view: SKView) {
addChild(player)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
player.position = touch.location(in: self)
}
}
}
Are you sure your touch event is registered? Just put a print("moved") in the touches moves function to be sure.

Swift MacOS SpriteKit rightMouseDragged not working

I have a NSViewController/NSView with a SpriteKit Scene filling the whole view. Here are the events I need to handle:
override func mouseDown(with event: NSEvent) {
override func rightMouseDown(with event: NSEvent) {
override func otherMouseDown(with event: NSEvent) {
override func mouseDragged(with event: NSEvent) {
override func rightMouseDragged(with event: NSEvent) {
override func otherMouseDragged(with event: NSEvent) {
But they are very inconsistent, and I'm not able to get rightMouseDragged working at all.
mouseDown works fine in the SKScene
rightMouseDown works fine in the SKScene
otherMouseDown had to be implemented in the viewController and passed on to the scene
mouseDragged worked fine in the SKScene
rightMouseDragged is not working at all
otherMouseDragged had to be implemented in the viewController and passed on to the scene
My main need is to fix rightMouseDragged. It doesn't receive any dragged events either in the view controller or the scene controller.
Ok, I don't understand why, but the rightMouseDragged was being delivered to the SKView. So here's where I needed to implement my event handlers:
mouseDown in the SKScene
rightMouseDown in the SKScene
otherMouseDown in the viewController and passed on to the scene
mouseDragged in the SKScene
rightMouseDragged in the SKView and be passed to the scene
otherMouseDragged in the viewController and passed on to the scene

Swift method doesn't override any method from its superclass

I am relatively new to Swift and I am following some basic tutorials but I seem to be having a problem with some methods which attempt to allow the user to press return to minimise the keyboard or to click off the keyboard and the keyboard will disappear, I understand why I am receiving these errors but have no clue how to go about fixing it, I feel something may have been changed in the newer version of Swift I am using as he is using an older version than me, could anyone possibly explain how to go about fixing these two errors please? Any help would be greatly appreciated here is my source code: (First error, value of type 'viewController' has no member 'text' and secondly, touchesBegan method does not override any method from its superclass)
import UIKit
class ViewController: UIViewController {
#IBAction func buttonPressed(sender: AnyObject) {
label.text = textArea.text
}
#IBOutlet weak var textArea: UITextField!
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.text.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
}
You have 2 problems here, based on the images you posted:
1) The method touhesBegan you are using is not correct:
Correct one:
func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)
Yours:
func touchesBegan(touches: NSSet, withEvent event: UIEvent)
I think you want a delegate for the UITextField, so this one is not corerct: touchesBegan is a method for the UIReponder delegate and not for UITextFieldDelegate.
Here you can find the reference for the UITextFieldDelegate.
2) the variable text doesn't exists in your code. I think you wanted to use textArea instead.
Hope this can help you, happy coding!
In your case change following thing:
instead of :
self.text.delegate = self
change :
self.textArea.delegate = self
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
And for delegate add like this
class ViewController: UIViewController, UITextFieldDelegate {
}

SKSpriteNode Not Loading Onto Screen?

Everything works fine,but then when I add the apple,it only stays on screen for a very short amount of time, and then it goes away/disappears. Why is this? any help would be much appreciated.
import SpriteKit
class GameScene: SKScene {
var bg=SKSpriteNode()
var apple=SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let bg_texture=SKTexture(imageNamed: "wood-texture-pattern.jpg")
bg=SKSpriteNode(texture: bg_texture)
bg.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
bg.size.height=self.frame.height
bg.size.width=self.frame.width
self.addChild(bg)
let applePic=SKTexture(imageNamed:"apple.png")
apple=SKSpriteNode(texture:applePic)
apple.position=CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
// apple.size.height=50
// apple.size.width=50
apple.setScale(0.25)
self.addChild(apple)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Change the zPosition of the skspritenode

tvOS pressesBegan() not updating in GameScene

In my GameScene, when I update variables using 'touchesBegan()' it works fine, but when I pass 'pressesBegan()' from the GameViewController to the GameScene, the code gets excited, but the changes get ignored.
GameViewController:
class GameViewController: UIViewController {
...
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
let scene = GameScene(fileNamed: "GameScene")
scene?.pressesBegan(presses, withEvent: event)
}
GameScene:
class GameScene: SKScene {
var test = true
...
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
test = false
print(test)
}
override func update(currentTime: CFTimeInterval) {
print(test)
}
}
This code will result in "false" then "true" in the console.
Every time you call pressesBegan, you are creating a new Gamescene, you need to do
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
if let skView = self.view as? SKView
{
skView.scene?.pressesBegan(presses, withEvent: event)
}
}