I have multiple SpriteNodes loaded in my GameScene at random locations, but it is actually the same SpriteNode added multiple times. I have a function in touchesEnded, that removes a SpriteNode once the touch is released on the same location as the SpriteNode. This only works for the initial SpriteNode (the first SpriteNode that was added) but does not work for all the other SpriteNodes.
I tried to turn the code "if object.contains(location)" into a while loop, so that it would repeat for ever touch. That didn't work either.
var object = SKSpriteNode()
var objectCount = 0
func spawnObject() {
object = SKSpriteNode(imageNamed: "image")
object.position = CGPoint(x: randomX, y: randomY)
objectCount = objectCount + 1
self.addChild(object)
}
while objectCount < 10 {
spawnObject()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
if object.contains(location) {
object.removeFromParent()
}
}
}
I expected that whenever I touch an object it would disappear. But that only happens with one object, and it works perfectly fine and as expected with the first object, but the other nine objects show no reaction.
Ok this is the basics of using an array to track the spawned objects so that you can check them all:
var objectList: [SKSpriteNode] = [] // Create an empty array
func spawnObject() {
let object = SKSpriteNode(imageNamed: "image")
object.position = CGPoint(x: randomX, y: randomY)
self.addChild(object)
objectList.append(object) // Add this object to our object array
}
while objectList.count < 10 {
spawnObject()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
// Check all objects in the array
for object in objectList {
if object.contains(location) {
object.removeFromParent()
}
}
// Now remove those items from our array
objectList.removeAll { (object) -> Bool in
object.contains(location)
}
}
}
Note: that's not the best way to do this for especially from a performance point of view but it's enough to get the idea across.
Related
I am making a game with Sprite Kit where the user has tap balls that pass through the screen. The balls are spawned every 1 second. However, if two balls have spawned and the user taps the first ball only the second (and any that have spawned after that) will be removed/recorded and not the one the user actually tapped.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node = self.nodes(at: location).first
if node?.name == "BALL" {
currentScore += ballValue
player?.removeFromParent()
}
else {
gameOver()
}
}
}
override func didMove(to view: SKView) {
setupTracks()
createHUD()
self.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
self.createBall(forTrack: self.track)
}, SKAction.wait(forDuration: 2)])))
}
func createBall(forTrack track: Int) {
setLevel()
player?.name = "BALL"
player?.size = CGSize(width: 100, height: 100)
ballValue = 1
let ballPosition = trackArray?[track].position
player?.position = CGPoint(x: (ballPosition?.x)!, y: (ballPosition?.y)!)
player?.position.y = (ballPosition?.y)!
player?.zPosition = 1
if ballDirection == "right" {
player?.position.x = 0
moveRight()
}
else {
player?.position.x = (self.view?.frame.size.height)!
moveLeft()
}
}
I’m pretty sure it’s because you do:
player?.removeFromParent()
no matter which sprite is touched, but player is always the last sprite spawned. You’ve already assigned the node that was touched to node, so I think you need to do:
node.removeFromParent()
instead.
I have the following function which spawns squares and adds them to an array of squares. This adds new squares indefinitely until the function is stopped. The array of squares is declared in the SKScene like so: var rsArray = [RedSquare]().
func spawnRedSquares() {
if !self.gameOver {
let rs = RedSquare()
var rsSpawnRange = self.frame.size.width/2
rs.position = CGPointMake(rsSpawnRange, CGRectGetMaxY(self.frame) + rs.sprite.size.height * 2)
rs.zPosition = 3
self.addChild(rs)
self.rsArray.append(rs)
let spawn = SKAction.runBlock(self.spawnRedSquares)
let delay = SKAction.waitForDuration(NSTimeInterval(timeBetweenRedSquares))
let spawnThenDelay = SKAction.sequence([delay, spawn])
self.runAction(spawnThenDelay)
}
}
I'm trying to use the touchesBegan() function to detect when a specific square in the array is tapped and then access the properties of the square. I can't figure out how to determine which square is being touched. How would I go about doing this?
Give each of the squares you spawn a unique name and check that name in touchesBegan. You can use a counter and do
rs.name = "square\(counter++)"
In touchesBegan you can retrieve the name of the touched node and check it against the names of the nodes in the array.
First you have to give the rs node a name. For example
rs.name = "RedSquare"
Then you can use the nodeAtPoint function to find the node at a particular touch point. If the node is a RedSquare, you can modify it.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches {
let touchPoint = touch.locationInNode(self)
let node = self.nodeAtPoint(touchPoint)
if node.name == "RedSquare" {
// Modify node
}
}
}
I was able to answer my own question by experimenting, and decided that I would post the answer in case anyone else had a similar problem. The code that I needed inside the touchesBegan() function was the following:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let rsCurrent = self.nodeAtPoint(location)
for RedSquare in rsArray {
let rsBody = RedSquare.sprite.physicsBody
if rsBody == rsCurrent.physicsBody? {
//Action when RedSquare is touched
}
}
}
}
I'm working a little game for iOS.
I've a SKSpriteNode in my scene - when I remove it with "removeFromParent" and touch the area it was last in, I still get the function.
My code is as following:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if tapToPlayNode.containsPoint(location){
tapToPlayNode.removeFromParent()
startNewGame()
}
}
}
func startNewGame(){
//Starts a new game with resetted values and characters in position
println("Ready.. set.. GO!")
//Shows the ui (value 1)
toggleUiWithValue(1)
}
In other words, I get "Ready.. set.. GO!" output when I touch the area even after it was deleted.
Any clues?
Bests,
Your tapToPlayNode is still retained by self and is removed from it's parent.
You should make it optional var tapToPlayNode:SKSpriteNode?and nil it after remove it from it's parent like this:
if let playNode = self.tapToPlayNode {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if playNode.containsPoint(location) {
playNode.removeFromParent()
startNewGame()
self.tapToPlayNode = nil // il it here!
break
}
}
}
You can also avoid to keep a reference of your tapToPlayNode and give it a name when initializing it like this:
node.name = #"tapToPlayNodeName"
// Add node to scene and do not keep a var to hold it!
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
// Retrieve the ode here
let tapToPlayNode = container.childNodeWithName("tapToPlayNodeName")!
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if tapToPlayNode.containsPoint(location){
tapToPlayNode.removeFromParent()
startNewGame()
}
}
}
I am really really frustrated by this and I have been trying to get it to work for almost two full days now :(
How do I go about adding a click event to a SKSpriteNode in swift
let InstantReplay:SKSpriteNode = SKSpriteNode(imageNamed: "InstantReplay")
InstantReplay.position = CGPoint(x: size.width + InstantReplay.size.width/2, y: size.height/2)
InstantReplay.size = CGSize(width: size.width/1.4, height: size.height/8)
addChild(InstantReplay)
InstantReplay.runAction(SKAction.moveToX(size.width/2, duration: NSTimeInterval(1)))
All I want to happen is when "InstantReplay" is clicked for it to run a function called "InstantReplay_Clicked", How would I go about Implementing this?
Any help much appreciated :)
Give your SKSpriteNode a name so we can identify it in the touchesBegan or touchesEnded method of your SKScene.
Also set userInteractionEnabled of your SKSpriteNode to false, so that it doesn't trap touch events for itself, and not pass them up to the your Scene.
override init() {
super.init()
let instantReplay = SKSpriteNode(imageNamed: "InstantReplay")
instantReplay.position = CGPoint(x: size.width + instantReplay.size.width/2, y: size.height/2)
instantReplay.size = CGSize(width: size.width/1.4, height: size.height/8)
instantReplay.name = "InstantReplay"; // set the name for your sprite
instantReplay.userInteractionEnabled = false; // userInteractionEnabled should be disabled
self.addChild(instantReplay)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let location = touches.anyObject()?.locationInNode(self)
let node = self.nodeAtPoint(location!)
if (node.name == "InstantReplay") {
println("you hit me with your best shot!")
}
}
(oh - i also renamed your instantReplay variable to use lowerCamelCase, in line with Swift best practices.
Swift 4 Update
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let node = self.atPoint(t.location(in :self))
if (node.name == "InstantReplay") {
print("you hit me with your best shot!")
}
}
}
Ok, working with ARKit in Swift here and trying to get a grip on this -
for my game controls I want to be able to control the point (position in 3d space) that the SCNNode is moving towards when the users finger is down on the screen, meaning started by the touchesBegan func.
I want it to be like Apple's fox game with the joystick her but in AR: https://developer.apple.com/library/content/samplecode/Fox/Introduction/Intro.html
My main issue is it doesn't seem that my SCNAction's position to move is being updated correctly on touchesMoved, and moreover I need the SCNNode to move at same speed to the position regardless of how far it is away.
Here's what I have, it is working but not correct:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let results = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitFeature = results.last else { return }
checkIfNodeTapped(touches: touches, node: theDude.node)
theDude.moveToPos(pos: getARPos(hitFeature: hitFeature))
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let results = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitFeature = results.last else { return }
theDude.updateWalkTo(pos: getARPos(hitFeature: hitFeature))
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// if virtualObjectManager.virtualObjects.isEmpty {
//
// return
// }
// virtualObjectManager.reactToTouchesEnded(touches, with: event)
//Remove move actions
theDude.stopMoving()
}
func updateWalkTo(pos: SCNVector3)
{
walkAction = SCNAction.move(to: pos, duration: 1)
}
func moveToPos(pos: SCNVector3)
{
walkAction = SCNAction.move(to: pos, duration: 1)
self.node.runAction(walkAction, forKey: "walk")
}
func stopMoving()
{
self.node.removeAction(forKey: "walk")
}
where the walkAction is just a defined SCNAction. How can I fix this so that the node runs towards wherever the user's finger is on the screen (converted to AR points)?