Created
February 2, 2016 12:47
-
-
Save Relequestual/0a866211ef8d6ea93344 to your computer and use it in GitHub Desktop.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggested improvments from irc: http://imgur.com/S2gzT16