Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to delegate a touch event to an entity when the entity has a component where the SKSpriteNode was defined, and the spritenode is underneeth another node based on z-index
// BaseScene.swift
// Hospital Demo
//
// Created by Ben Hutton on 17/11/2015.
// Copyright © 2015 Ben Hutton. All rights reserved.
//
import Foundation
import SpriteKit
import GameplayKit
import HLSpriteKit
class BaseScene: HLScene {
...
...
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touched!")
let touch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNodes = self.nodesAtPoint(positionInScene)
for node in touchedNodes {
print(node.userData)
if((node.userData?["entity"]) != nil){
let entity = node.userData!["entity"] as! GKEntity
entity.componentForClass(TouchableSpriteComponent)?.callFunction()
}
}
}
}
//
// SpriteComponent.swift
// Hospital Demo
//
// Created by Ben Hutton on 19/12/2015.
// Copyright © 2015 Ben Hutton. All rights reserved.
//
import GameplayKit
import SpriteKit
class SpriteComponent: GKComponent {
let node: SKSpriteNode
init(texture: SKTexture) {
node = SKSpriteNode(texture: texture, color: SKColor.whiteColor(), size: texture.size())
}
func addToNodeKey() {
self.node.userData = NSMutableDictionary()
self.node.userData?.setObject(self.entity!, forKey: "entity")
}
}
//
// Tile.swift
// Hospital Demo
//
// Created by Ben Hutton on 19/12/2015.
// Copyright © 2015 Ben Hutton. All rights reserved.
//
import SpriteKit
import GameplayKit
class Tile: GKEntity {
init(imageName: String, x: Int, y: Int) {
super.init()
let spriteComponent = SpriteComponent(texture: SKTexture(imageNamed: imageName))
addComponent(spriteComponent)
spriteComponent.addToNodeKey()
let width = Int((spriteComponent.node.texture?.size().width)!)
let x = width * x + width / 2
let y = width * y + width / 2
let positionComponent = PositionComponent(x: x, y: y)
addComponent(positionComponent)
spriteComponent.node.position = CGPoint(x: x, y: y)
let touchableComponent = TouchableSpriteComponent(){
print("function of spritecomponent?")
self.handleTouch()
}
addComponent(touchableComponent)
let spriteDebugComponent = SpriteDebugComponent(node: spriteComponent.node)
addComponent(spriteDebugComponent)
}
func handleTouch() {
print("I am a TILE!")
print(self.componentForClass(PositionComponent)?.x)
print(self.componentForClass(PositionComponent)?.y)
// Do some more things like change state of another component
}
}
//
// TouchableSpriteComponent.swift
// Hospital Demo
//
// Created by Ben Hutton on 19/12/2015.
// Copyright © 2015 Ben Hutton. All rights reserved.
//
import GameplayKit
import SpriteKit
class TouchableSpriteComponent: GKComponent {
var entityTouched: ()->Void;
init(f:() -> Void) {
self.entityTouched = f
}
func callFunction() {
self.entityTouched()
}
}
@Relequestual

This comment has been minimized.

Copy link
Owner Author

Relequestual commented Feb 2, 2016

Suggested improvments from irc: http://imgur.com/S2gzT16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.