How to subclass a SKSpriteNode and implement touchesBegan? - ios

I'm trying to create a node that has the ability to be swiped offscreen. I tried adding a UIGestureRecognizer the the view but the swipe only works for the first node. I heard there is a way to subclass a spriteNode and in the touchesBegan add the ability to swipe off screen. I can setup a base for a custom sprite node but am unsure of how to give it the ability to be swiped. Here is my code for a custom sprite node:
class CustomNode: SKSpriteNode {
init() {
let texture = SKTexture(imageNamed: customNodeImage)
super.init(texture: texture, color: SKColor.clear, size: texture.size())
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I do have a touchesBegan in my scene but when I touch a node I get a "nil" response when I try and print out the node's name. Here is my scene code:
import SpriteKit
let plankName = "woodPlank"
class PlankScene: SKScene {
var plankWood : Plank?
var plankArray : [SKSpriteNode] = []
override func didMove(to view: SKView) {
plankWood = childNode(withName: "woodPlank") as? Plank
plankWood?.isUserInteractionEnabled = false
let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
}
func swipedRight(sender: UISwipeGestureRecognizer) {
if sender.direction == .right {
let moveOffScreenRight = SKAction.moveTo(x: 400, duration: 0.5)
let nodeFinishedMoving = SKAction.removeFromParent()
let waitForNode = SKAction.wait(forDuration: 0.5)
plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]),
completion:{
} )
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
print("\(plankWood?.name)")
if plankWood?.name == plankName {
print("Plank touched")
}
}
override func enumerateChildNodes(withName name: String, using block: #escaping (SKNode, UnsafeMutablePointer<ObjCBool>) -> Void) {
let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))
swipeRight.direction = .right
view?.addGestureRecognizer(swipeRight)
}
}

Related

SKScene nodes not detecting touch

I am trying to make an ARKit app for ios and the nodes in the scene are not responding to touch. The scene is properly displayed but I haven't been able to detect any touch.
fileNamed: "TestScene" refers to a TestScene.sks file in my project which is empty and I add the node in the code as shown below.
let detailPlane = SCNPlane(width: xOffset, height: xOffset * 1.4)
let testScene = SKScene(fileNamed: "TestScene")
testScene?.isUserInteractionEnabled = true
let winner = TouchableNode(fontNamed: "Chalkduster")
winner.text = "You Win!"
winner.fontSize = 65
winner.fontColor = SKColor.green
winner.position = CGPoint(x: 0, y: 0)
testScene?.addChild(winner)
let material = SCNMaterial()
material.diffuse.contents = testScene
material.diffuse.contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0)
detailPlane.materials = [material]
let node = SCNNode(geometry: detailPlane)
rootNode.addChildNode(node)
For TouchableNode I have the following class
class TouchableNode : SKLabelNode {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("Touch detected")
}
}
I've achieved this affect using gesture recognize
private func registerGestureRecognizers() -> Void {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
sceneView.addGestureRecognizer(tapGestureRecognizer)
}
then have a function to handle the tap gesture
#objc private func handleTap(sender: UITapGestureRecognizer) -> Void {
let sceneViewTappedOn = sender.view as! SCNView
let touchCoordinates = sender.location(in: sceneViewTappedOn)
let hitTest = sceneViewTappedOn.hitTest(touchCoordinates)
if !hitTest.isEmpty {
let hitResults = hitTest.first!
var hitNode = hitResults.node
// do something with the node that has been tapped
}
}
}
You need to do isUserInteractionEnabled = true first.
So, something like:
class TouchableNode : SKLabelNode {
override init() {
super.init()
isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
isUserInteractionEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("Touch detected")
}
}

Can't touch UIImageView that is moving in Swift

I made drop tile game that tiles fall from the top of screen to one's bottom.
This game system is when you touch a tile, the tile will be hidden.
The tiles are custom class (GameTile class), but Touches Began in GameViewController didn't work.
How can I solve it?
GameTile.swift
class GameTile: UIImageView {
init(named: String, frame: CGRect) {
super.init(frame: frame)
super.image = (UIImage(named: named))
super.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameTileNormal: GameTile {
let namedDefault: String
var frameDefault: CGRect
let isHiddenDefault: Bool
var isUserInteractionEnabledDefault: Bool
let colorName: UIColor
init(
named: String,
frame: CGRect,
isHidden: Bool = false,
isUserInteractionEnabled: Bool = true,
color: UIColor = UIColor.blue) {
namedDefault = named
isHiddenDefault = isHidden
frameDefault = frame
isUserInteractionEnabledDefault = isUserInteractionEnabled
colorName = color
super.init(named: named, frame: frame)
super.isHidden = isHiddenDefault
super.isUserInteractionEnabled = isUserInteractionEnabledDefault
super.backgroundColor = colorName
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
GameView.swift
class GameView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true
self.backgroundColor = (UIColor.white)
self.frame = CGRect(x:0, y:0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
//make tiles
let tileNormal = GameTileNormal.init(named: "clear",
frame: CGRect(x:0), y:-60, width:60, height:60),isUserInteractionEnabled: true)
self.addSubview(tileNormal)
//move tiles
moveTile(tile: tileNormal, lane: 1)
}
}
func moveTile(tile: GameTile, lane: Int) {
UIImageView.animate(withDuration: TimeInterval(2.0),
delay: 0.0,
options: .curveLinear,
animations: {
tile.frame.origin.y = UIScreen.main.bounds.size.height
}, completion: {finished in
tile.removeFromSuperview()
//make new tile
self.makeTiles(lane: lane)
})
}
GameViewController.swift
class GameViewController: UIViewController {
var gameView: GameView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.isUserInteractionEnabled = true
gameView = GameView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
self.view.addSubview(trapView)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchEvent = touches.first!
if let gameView = self.gameView {
// touchEvent.view is "gameView", not the view whose kind of class is GameTileNormal...
if let touchedGameTile = touchEvent.view as? GameTileNormal {
print("Touched normal tile")
touchEvent.view?.isHidden = true
touchEvent.view?.isUserInteractionEnabled = false
}else{
// other view
}
}
}
UPDATE
I changed how to move tiles from UIImageView.animation to Timer.
Then If I touched tiles, it didn't through after if (tile.layer.presentation()?.hitTest(location)) != nil { in touchesBegan, GameViewController.....
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchEvent = touches.first!
let location = touchEvent.location(in: touchEvent.view)
if let standardView = self.standardView {
for tile in standardView.tiles {
//breakpoint stops here
if (tile.layer.presentation()?.hitTest(location)) != nil {
//breakpoint doesn't through here
if tile is GameTileNormal {
//normal tile touched
}else{
}
break
}
}
}
}
moveTiles
makeTileTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateTilesPositionY(timer:)), userInfo: sendArray, repeats: true)
update tile position (drop tiles)
#objc func updateTilesPositionY(timer: Timer){
//tile info
let timerInfo:[Any] = timer.userInfo as! [Any]
let tile:GameTile = timerInfo[0] as! GameTile
let lane: Int = timerInfo[1] as! Int
//drop tile
tile.frame.origin.y = tile.frame.origin.y+1
//if tile reached on the bottom
if tile.frame.origin.y >= UIScreen.main.bounds.size.height {
if tile is GameTileNormal {
self.showGameOverView()
}
}else{
//drop tile
}
In UIImageView.animate add option .allowUserInteraction:
UIImageView.animate(withDuration: TimeInterval(2.0),
delay: 0.0,
options: [.curveLinear, .allowUserInteraction],
animations: {
tile.frame.origin.y = UIScreen.main.bounds.size.height
}, completion: {finished in
...
By default the user interaction is disallowed during animations.
UPDATE
However, to test whether the user hit a moving object, you will have a bit harder time. See for example this SO question. Basically, the UIView object does not really move, you can easily test that after firing the animation, the frame of the animated object is set straight to the end position. Just the presentation layer draws the moving view.
You will have to always go over all your moving tiles in the game and test each one if any of them has been touched (here I assume you have a reference to all the tiles in the game):
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchEvent = touches.first!
let location = touchEvent.location(in: touchEvent.view)
if let gameView = self.gameView {
for tile in tiles {
// go over all the moving objects in your scene and hit test all of them
if let touchedLayer = tile.layer.presentation()?.hitTest(location) {
// if a hittest returns a layer, it means that this tile was touched, we can handle it and break out of the loop
tile.isHidden = true
tile.isUserInteractionEnabled = false
tile.removeFromSuperview()
break
}
}
}
}
Use layer.presentationLayer to run a hitTest if that hitTest return a CALayer then you are touching that titleView, in fact this will only work if your titles are userInteractionEnabled = false
Full Code
import UIKit
class GameTile: UIImageView {
init(named: String, frame: CGRect) {
super.init(frame: frame)
super.image = (UIImage(named: named))
super.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameTileNormal: GameTile {
let namedDefault: String
var frameDefault: CGRect
let isHiddenDefault: Bool
var isUserInteractionEnabledDefault: Bool
let colorName: UIColor
init(
named: String,
frame: CGRect,
isHidden: Bool = false,
isUserInteractionEnabled: Bool = false,
color: UIColor = UIColor.blue) {
namedDefault = named
isHiddenDefault = isHidden
frameDefault = frame
isUserInteractionEnabledDefault = isUserInteractionEnabled
colorName = color
super.init(named: named, frame: frame)
super.isHidden = isHiddenDefault
super.isUserInteractionEnabled = isUserInteractionEnabledDefault
super.backgroundColor = colorName
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true
self.backgroundColor = (UIColor.white)
self.frame = CGRect(x:0, y:0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
//make tiles
let tileNormal = GameTileNormal.init(named: "clear",
frame: CGRect(x:0, y:-60, width:60, height:60),isUserInteractionEnabled: false)
self.addSubview(tileNormal)
//move tiles
moveTile(tile: tileNormal, lane: 1)
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.blue.cgColor
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func moveTile(tile: GameTile, lane: Int) {
UIImageView.animate(withDuration: TimeInterval(10),
delay: 0.0,
options: .curveLinear,
animations: {
tile.frame.origin.y = UIScreen.main.bounds.size.height
}, completion: {finished in
tile.removeFromSuperview()
//make new tile
//self.makeTiles(lane: lane)
})
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchEvent = touches.first!
let location = touchEvent.location(in: touchEvent.view)
for tile in self.subviews {
// go over all the moving objects in your scene and hit test all of them
if tile.layer.presentation()?.hitTest(location) != nil {
// if a hittest returns a layer, it means that this tile was touched, we can handle it and break out of the loop
tile.isHidden = true
break
}
}
}
}
class ViewController: UIViewController {
var gameView: GameView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.isUserInteractionEnabled = true
gameView = GameView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
self.view.addSubview(gameView)
}
}

How can I detect swipes on Spritekit Nodes?

So I am currently working on a 2D endless runner written in Swift3 and using Spritekit for my nodes and scenes. I recently implemented some code to detect swipes in general, they will be below. So my question is: how can I detect a swipe action and check the direction of that swipe on a Spritekit Node? I realize that this has been asked before, but I cannot find a working solution since everything I come across seems to be for Swift and Swift2, not Swift3.
Here's my swipe detection code:
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeUp.direction = UISwipeGestureRecognizerDirection.up
self.view.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
}
I have tested these with a function:
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
}
The swipes seem to work all right, as the print statements have been displaying the correct outputs in response to my swipes.
I have also added a delegate to avoid possible SIGABRT errors:
class GameViewController: UIViewController, UIGestureRecognizerDelegate
Please let me know if you'd like more information!
Edit:
Added code where I initialized my puzzle pieces
//The class
class ChoosePiecesClass: SKSpriteNode{
func moveWithCamera() {
self.position.x += 5;
}
//initialize a piece
private var RandomPiece1: ChoosePiecesClass?;
override func didMove(to view: SKView) {
RandomPiece1 = childNode(withName: "RandomPiece1") as? ChoosePiecesClass;
RandomPiece1?.position.x = 195;
RandomPiece1?.position.y = -251;
}
override func update(_ currentTime: TimeInterval) {
RandomPiece1?.moveWithCamera();
}
Edited to provide the error statement from console, upon clicking Start Game and triggering the fatalError code, causing the game to crash.
fatal error: init(coder:) has not been implemented: file /Users/ardemis/Documents/PuzzleRunner/PuzzleRunner 3 V3/PuzzleRunner/ChoosePieces.swift, line 33
It also displays the following
EXEC_BAT_INSTRUCTION(code = EXEC_1386_INVOP, subcode = 0x0)
on the same line as the fatalError declaration.
I wouldn't use the UIGestures if I was trying to track specific SpriteNodes. You can easily track your nodes in TouchesBegan and figure out which way a swipe direction occurs. This example has three sprites on the screen on will print/log whatever direction one of them gets swiped and will ignore all other swipes.
EDIT > I just created a subclass for my Box object (sorry I didn't use your naming, but it has the same functions). There are probably many ways of doing this, I chose to use create a protocol on the subclass and make the scene conform to the protocol. I moved all of the touch/swipe functionality into the sub class, and when it is done detecting the swipe it just calls the delegate and says which object has been swiped.
protocol BoxDelegate: NSObjectProtocol {
func boxSwiped(box: Box)
}
class Box: SKSpriteNode {
weak var boxDelegate: BoxDelegate!
private var moveAmtX: CGFloat = 0
private var moveAmtY: CGFloat = 0
private let minimum_detect_distance: CGFloat = 100
private var initialPosition: CGPoint = CGPoint.zero
private var initialTouch: CGPoint = CGPoint.zero
private var resettingSlider = false
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func moveWithCamera() {
self.position.x += 5
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch! {
initialTouch = touch.location(in: self.scene!.view)
moveAmtY = 0
moveAmtX = 0
initialPosition = self.position
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch! {
let movingPoint: CGPoint = touch.location(in: self.scene!.view)
moveAmtX = movingPoint.x - initialTouch.x
moveAmtY = movingPoint.y - initialTouch.y
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
var direction = ""
if fabs(moveAmtX) > minimum_detect_distance {
//must be moving side to side
if moveAmtX < 0 {
direction = "left"
}
else {
direction = "right"
}
}
else if fabs(moveAmtY) > minimum_detect_distance {
//must be moving up and down
if moveAmtY < 0 {
direction = "up"
}
else {
direction = "down"
}
}
print("object \(self.name!) swiped " + direction)
self.boxDelegate.boxSwiped(box: self)
}
}
In GameScene.sks
Make sure to make GameScene conform to BoxDelegate by adding the protocol after the declaration
class GameScene: SKScene, BoxDelegate {
var box = Box()
var box2 = Box()
var box3 = Box()
override func didMove(to view: SKView) {
box = Box(color: .white, size: CGSize(width: 200, height: 200))
box.zPosition = 1
box.position = CGPoint(x: 0 - 900, y: 0)
box.name = "white box"
box.boxDelegate = self
addChild(box)
box2 = Box(color: .blue, size: CGSize(width: 200, height: 200))
box2.zPosition = 1
box2.name = "blue box"
box2.position = CGPoint(x: 0 - 600, y: 0)
box2.boxDelegate = self
addChild(box2)
box3 = Box(color: .red, size: CGSize(width: 200, height: 200))
box3.zPosition = 1
box3.name = "red box"
box3.position = CGPoint(x: -300, y: 0)
box3.boxDelegate = self
addChild(box3)
}
func boxSwiped(box: Box) {
currentObject = box
print("currentObject \(currentObject)")
}
override func update(_ currentTime: TimeInterval) {
box.moveWithCamera()
box2.moveWithCamera()
box3.moveWithCamera()
}
}

How to call a function that is defined in a different view controller?

I'm trying to call the function called "kick". Inside it is a CAAnimation that I am playing on this man I made. So usually I would just call the function in the same view controller as it was made in. But because I have a HUD/overlay scene the displays over my 3D game. And the HUD/overlayscene is made in a different view controller specifically just to set up the HUD I can't get the HUD view controller to Recognize any functions from the Main View Controller.
MainViewController:
Code:
class GameViewController: UIViewController, ADBannerViewDelegate, SKPhysicsContactDelegate, SKSceneDelegate, SCNSceneRendererDelegate, SCNPhysicsContactDelegate{
var HUDView: HUD!
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func viewDidLoad() {
super.viewDidLoad()
//-----Scene-Setup----------
let scnView = self.view as! SCNView
scnView.scene = FieldScene
scnView.playing = true
scnView.loops = true
self.HUDView = HUD(size: CGSizeMake(100, 100))
scnView.overlaySKScene = self.HUDView
scnView.delegate = self
scnView.overlaySKScene!.delegate = self
scnView.overlaySKScene!.userInteractionEnabled = true
scnView.backgroundColor = UIColor.whiteColor()
scnView.allowsCameraControl = true
scnView.showsStatistics = false
let GuyScene = SCNScene(named: "art.scnassets/The1.dae")
let Guy: SCNNode = GuyScene!.rootNode.childNodeWithName("Armature", recursively: true)!
let GuyBody: SCNNode = GuyScene!.rootNode.childNodeWithName("Cube", recursively: true)!
//----Giveing it a physics---------
let collisionCapsuleRadius2 = CGFloat(0.1)
let collisionCapsuleHeight2 = CGFloat(0.1)
Guy.position = SCNVector3(x: -30.0, y: 30.0, z: 0.0)
Guy.scale = SCNVector3Make(50, 50, 50)
Guy.rotation = SCNVector4Make(0, 1, 0, 1 )
Guy.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius2, height: collisionCapsuleHeight2), options:nil))
Guy.physicsBody?.affectedByGravity = true
Guy.physicsBody?.friction = 0 //
Guy.physicsBody?.restitution = 1 //bounceness of the object
Guy.physicsBody?.angularDamping = 1 // rotationess
Guy.physicsBody?.mass = 1
Guy.physicsBody?.rollingFriction = 0
scnView.scene!.rootNode.addChildNode(Guy)
scnView.scene!.rootNode.addChildNode(GuyBody)
}
//Ok Kick function gets declared right here
func Kick() {
//-----Animate-Guy-----Working-Perfectly-----------
let KickAnimation = CAAnimation.animationWithSceneNamed("art.scnassets/The1Anima.dae")!
Guy.addAnimation(KickAnimation, forKey: "Go")
Guy.removeAnimationForKey("Go", fadeOutDuration: 3.0)
}
}
extension CAAnimation {
class func animationWithSceneNamed(name: String) -> CAAnimation? {
var animation: CAAnimation?
if let scene = SCNScene(named: name) {
scene.rootNode.enumerateChildNodesUsingBlock({ (child, stop) in
if child.animationKeys.count > 0 {
animation = child.animationForKey(child.animationKeys.first!)
stop.initialize(true)
}
})
}
return animation
}
}
//Ok Below is my HUD/Overlayscene which holds the button that i would like call the function in.
extension GameViewController {
// was never used
}
class HUD: SKScene {
//-----------------Controller-Buttons----------------------
var ButtonA = SKSpriteNode(imageNamed:"EnhancedAButton")
override init(size: CGSize) {
super.init(size: size)
//----A-Button--Creation -------------------
ButtonA.size = CGSize(width: 7, height: 11)
ButtonA.anchorPoint = CGPointMake(-11.35, -0.6)
ButtonA.zPosition = 0
ButtonA.alpha = 0.45
self.addChild(ButtonA)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
//Ok here is the function I need to put "Kick()" inside it but when I do it doenst recognize it because the function wasnt declared in this view controller
func AButtonPressed() {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
for touch: AnyObject in touches {
let location1 = touch.locationInNode(self)
if self.nodeAtPoint(location1) == self.ButtonA {
AButtonPressed()
print("AButtonPressed")
}
}
}
}
enter image description here
enter image description here
This is practically the best way to handle this. Other methods are messy while this is the most decoupled natural way to handle it. If you have any questions, let me know.
extension GameViewController: GameDelegate {
func Kick() {
//-----Animate-Guy-----Working-Perfectly-----------
let KickAnimation = CAAnimation.animationWithSceneNamed("art.scnassets/The1Anima.dae")!
Guy.addAnimation(KickAnimation, forKey: "Go")
Guy.removeAnimationForKey("Go", fadeOutDuration: 3.0)
}
}
protocol GameDelegate {
func Kick()
}
class HUD: SKScene {
var delegate: GameDelegate?
override init(size: CGSize) {
super.init(size: size)
//this calls Kick anywhere in the HUD class
delegate?.Kick()
//----A-Button--Creation -------------------
ButtonA.size = CGSize(width: 7, height: 11)
ButtonA.anchorPoint = CGPointMake(-11.35, -0.6)
ButtonA.zPosition = 0
ButtonA.alpha = 0.45
self.addChild(ButtonA)
}
}
after the line that is inside of GameViewController's viewDidLoad()
//below this line
self.HUDView = HUD(size: CGSizeMake(100, 100))
//add in
self.HUDView.delegate = self
After all of this, is added in, you can delete the copy of Kick that is not in the extension. Both controllers can access the function.

Create Button in SpriteKit: Swift

I want to create a button in SpriteKit or in an SKScene that sends the view to another view controller.
I tried using the "performSegue with identifier ", however apparently an SKScene doesn't support this.
How would I create a button that sends the view to another view with SpriteKit?
This is the code that I've tried using to perform this action.
The line with "HomeButton.prepareForSegueWithIdentifier()" is just an example. It won't actually let me add the "prepareForSegue" part, it doesn't support it <--- What I mean by that is when I go to add it, it is unrecognized.
class GameOverScene: SKScene {
var HomeButton: SKNode! = nil
init(size: CGSize, won: Bool) {
super.init(size: size)
backgroundColor = SKColor.whiteColor()
HomeButton = SKSpriteNode(color: SKColor.blueColor(), size: CGSize(width: 100, height: 100))
HomeButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
HomeButton.userInteractionEnabled = true
self.addChild(HomeButton)
let message = won ? "You Won!" : "You Lose!"
let label = SKLabelNode(fontNamed: "Title 1")
label.text = message
label.fontSize = 40
label.fontColor = SKColor.blackColor()
label.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(label)
runAction(SKAction.sequence([SKAction.waitForDuration(3.0), SKAction.runBlock() {
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameScene(size: size)
self.view?.presentScene(scene, transition: reveal)
}
]))
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if HomeButton.containsPoint(location) {
HomeButton.prepareForSegueWithIdentifier()
}
}
}
Note: I've tried using a button, but they don't work in and SKScene.
I'll be on to respond if there is any confusion.
I have translated Alessandro Ornano’s answer to Swift 3.1:
import SpriteKit
class FTButtonNode: SKSpriteNode {
enum FTButtonActionType: Int {
case TouchUpInside = 1,
TouchDown, TouchUp
}
var isEnabled: Bool = true {
didSet {
if (disabledTexture != nil) {
texture = isEnabled ? defaultTexture : disabledTexture
}
}
}
var isSelected: Bool = false {
didSet {
texture = isSelected ? selectedTexture : defaultTexture
}
}
var defaultTexture: SKTexture
var selectedTexture: SKTexture
var label: SKLabelNode
required init(coder: NSCoder) {
fatalError("NSCoding not supported")
}
init(normalTexture defaultTexture: SKTexture!, selectedTexture:SKTexture!, disabledTexture: SKTexture?) {
self.defaultTexture = defaultTexture
self.selectedTexture = selectedTexture
self.disabledTexture = disabledTexture
self.label = SKLabelNode(fontNamed: "Helvetica");
super.init(texture: defaultTexture, color: UIColor.white, size: defaultTexture.size())
isUserInteractionEnabled = true
//Creating and adding a blank label, centered on the button
self.label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center;
self.label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center;
addChild(self.label)
// Adding this node as an empty layer. Without it the touch functions are not being called
// The reason for this is unknown when this was implemented...?
let bugFixLayerNode = SKSpriteNode(texture: nil, color: UIColor.clear, size: defaultTexture.size())
bugFixLayerNode.position = self.position
addChild(bugFixLayerNode)
}
/**
* Taking a target object and adding an action that is triggered by a button event.
*/
func setButtonAction(target: AnyObject, triggerEvent event:FTButtonActionType, action:Selector) {
switch (event) {
case .TouchUpInside:
targetTouchUpInside = target
actionTouchUpInside = action
case .TouchDown:
targetTouchDown = target
actionTouchDown = action
case .TouchUp:
targetTouchUp = target
actionTouchUp = action
}
}
/*
New function for setting text. Calling function multiple times does
not create a ton of new labels, just updates existing label.
You can set the title, font type and font size with this function
*/
func setButtonLabel(title: NSString, font: String, fontSize: CGFloat) {
self.label.text = title as String
self.label.fontSize = fontSize
self.label.fontName = font
}
var disabledTexture: SKTexture?
var actionTouchUpInside: Selector?
var actionTouchUp: Selector?
var actionTouchDown: Selector?
weak var targetTouchUpInside: AnyObject?
weak var targetTouchUp: AnyObject?
weak var targetTouchDown: AnyObject?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if (!isEnabled) {
return
}
isSelected = true
if (targetTouchDown != nil && targetTouchDown!.responds(to: actionTouchDown)) {
UIApplication.shared.sendAction(actionTouchDown!, to: targetTouchDown, from: self, for: nil)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if (!isEnabled) {
return
}
let touch: AnyObject! = touches.first
let touchLocation = touch.location(in: parent!)
if (frame.contains(touchLocation)) {
isSelected = true
} else {
isSelected = false
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if (!isEnabled) {
return
}
isSelected = false
if (targetTouchUpInside != nil && targetTouchUpInside!.responds(to: actionTouchUpInside!)) {
let touch: AnyObject! = touches.first
let touchLocation = touch.location(in: parent!)
if (frame.contains(touchLocation) ) {
UIApplication.shared.sendAction(actionTouchUpInside!, to: targetTouchUpInside, from: self, for: nil)
}
}
if (targetTouchUp != nil && targetTouchUp!.responds(to: actionTouchUp!)) {
UIApplication.shared.sendAction(actionTouchUp!, to: targetTouchUp, from: self, for: nil)
}
}
}
Usage:
#objc func buttonTap() {
print("Button pressed")
}
override func didMove(to view: SKView)
{
backgroundColor = SKColor.white
let buttonTexture: SKTexture! = SKTexture(imageNamed: "button")
let buttonTextureSelected: SKTexture! = SKTexture(imageNamed: "buttonSelected.png")
let button = FTButtonNode(normalTexture: buttonTexture, selectedTexture: buttonTextureSelected, disabledTexture: buttonTexture)
button.setButtonAction(target: self, triggerEvent: .TouchUpInside, action: #selector(GameScene.buttonTap))
button.setButtonLabel(title: "Button", font: "Arial", fontSize: 12)
button.position = CGPoint(x: self.frame.midX,y: self.frame.midY)
button.zPosition = 1
button.name = "Button"
self.addChild(button)
}
I have created two .png:
If you need to create a button in SpriteKit, I think this button must have all or some of the available actions to do whatever you want (exactly as UIButton did)
Here you can find a simple class that build a SpriteKit button, called FTButtonNode:
class FTButtonNode: SKSpriteNode {
enum FTButtonActionType: Int {
case TouchUpInside = 1,
TouchDown, TouchUp
}
var isEnabled: Bool = true {
didSet {
if (disabledTexture != nil) {
texture = isEnabled ? defaultTexture : disabledTexture
}
}
}
var isSelected: Bool = false {
didSet {
texture = isSelected ? selectedTexture : defaultTexture
}
}
var defaultTexture: SKTexture
var selectedTexture: SKTexture
var label: SKLabelNode
required init(coder: NSCoder) {
fatalError("NSCoding not supported")
}
init(normalTexture defaultTexture: SKTexture!, selectedTexture:SKTexture!, disabledTexture: SKTexture?) {
self.defaultTexture = defaultTexture
self.selectedTexture = selectedTexture
self.disabledTexture = disabledTexture
self.label = SKLabelNode(fontNamed: "Helvetica");
super.init(texture: defaultTexture, color: UIColor.whiteColor(), size: defaultTexture.size())
userInteractionEnabled = true
//Creating and adding a blank label, centered on the button
self.label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center;
self.label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center;
addChild(self.label)
// Adding this node as an empty layer. Without it the touch functions are not being called
// The reason for this is unknown when this was implemented...?
let bugFixLayerNode = SKSpriteNode(texture: nil, color: UIColor.clearColor(), size: defaultTexture.size())
bugFixLayerNode.position = self.position
addChild(bugFixLayerNode)
}
/**
* Taking a target object and adding an action that is triggered by a button event.
*/
func setButtonAction(target: AnyObject, triggerEvent event:FTButtonActionType, action:Selector) {
switch (event) {
case .TouchUpInside:
targetTouchUpInside = target
actionTouchUpInside = action
case .TouchDown:
targetTouchDown = target
actionTouchDown = action
case .TouchUp:
targetTouchUp = target
actionTouchUp = action
}
}
/*
New function for setting text. Calling function multiple times does
not create a ton of new labels, just updates existing label.
You can set the title, font type and font size with this function
*/
func setButtonLabel(title: NSString, font: String, fontSize: CGFloat) {
self.label.text = title as String
self.label.fontSize = fontSize
self.label.fontName = font
}
var disabledTexture: SKTexture?
var actionTouchUpInside: Selector?
var actionTouchUp: Selector?
var actionTouchDown: Selector?
weak var targetTouchUpInside: AnyObject?
weak var targetTouchUp: AnyObject?
weak var targetTouchDown: AnyObject?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!isEnabled) {
return
}
isSelected = true
if (targetTouchDown != nil && targetTouchDown!.respondsToSelector(actionTouchDown!)) {
UIApplication.sharedApplication().sendAction(actionTouchDown!, to: targetTouchDown, from: self, forEvent: nil)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!isEnabled) {
return
}
let touch: AnyObject! = touches.first
let touchLocation = touch.locationInNode(parent!)
if (CGRectContainsPoint(frame, touchLocation)) {
isSelected = true
} else {
isSelected = false
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!isEnabled) {
return
}
isSelected = false
if (targetTouchUpInside != nil && targetTouchUpInside!.respondsToSelector(actionTouchUpInside!)) {
let touch: AnyObject! = touches.first
let touchLocation = touch.locationInNode(parent!)
if (CGRectContainsPoint(frame, touchLocation) ) {
UIApplication.sharedApplication().sendAction(actionTouchUpInside!, to: targetTouchUpInside, from: self, forEvent: nil)
}
}
if (targetTouchUp != nil && targetTouchUp!.respondsToSelector(actionTouchUp!)) {
UIApplication.sharedApplication().sendAction(actionTouchUp!, to: targetTouchUp, from: self, forEvent: nil)
}
}
}
The source is available in this Gist
Usage:
let backTexture: SKTexture! = SKTexture(image:"backBtn.png")
let backTextureSelected: SKTexture! = SKTexture(image:"backSelBtn.png")
let backBtn = FTButtonNode(normalTexture: backTexture, selectedTexture: backTextureSelected, disabledTexture: backTexture,size:backTexture.size())
backBtn.setButtonAction(self, triggerEvent: .TouchUpInside, action: #selector(GameScene.backBtnTap))
backBtn.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
backBtn.zPosition = 1
backBtn.name = "backBtn"
self.addChild(backBtn)
func backBtnTap() {
print("backBtnTap tapped")
// Here for example you can do:
let transition = SKTransition.fadeWithDuration(0.5)
let nextScene = MenuScene(size: self.scene!.size)
nextScene.scaleMode = .ResizeFill
self.scene?.view?.presentScene(nextScene, transition: transition)
}
The simplest solution, but possibly not of the greatest quality, is to use a SpriteNode containing an image and name it. Later, using that scene you can easily program it to transfer the user to the next scene when tapped:
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!)
}
}
}
}
Don't forget to replace "YourNextScene" with the actual name of your next scene.

Resources