Sprite kit touch and hold with swift - ios

i'm try to make my first game in sprite kit using swift
everything work fine for now but i couldn't know how could handle touch and hold in the screen
i'm try to make a jump power when the player hold the touch but i can't find event for this
thanks ;)

you can try this :
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// action when the user touches the screen
// you can know where did he touch the screen like this
let touchLocation = touches?.anyObject().locationInView(self/* or your view */)
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
// your code here
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
// your code here
}
those method will help cary touches events in your screen

As I understand you need to handle 2 situations
1 - player taps on the screen/node - e.g. to jump
2 - player taps and holds - e.g. to change jump power
I think you need to handle both "touchesBegan" and "touchesEnded".
In "touchesBegan" start special timer and after some delay (e.g. 0.5 secs) start playing special animation for it (jump power indicator that shows current jump power)
on "touchesEnded" - stop timer, stop animation and calculate result jump power (based on timer's value).
If you also need to handle direction (angle) of jump - in this case you should also handle "touchesMoved" and calculate current angle based on touch position.

Related

How to count the number of times the screen has been touched? SWIFT

How to get the number of times the screen has been touched in Swift?
iOS wkwebview application.
This code doesn't work:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
counter++
print(counter)
}
I think that wkwebview handles the touches of it's inside view, you can't "reach" it outside of it. Your best shot might be to inject some javascript code which can send a message if the user touches the document
You can read about javascript messages and the wkwebviews here

Defining my spaceship movement in spritekit game in Swift

Hi I am new to Xcode and Swift, right now I am trying to design a game that involves a spaceship as player with alien spaceships.
I ran into a little problem where I am trying to differentiate the movement of the spaceship from the firing of the spaceship.
Basically I used touchesBegan() function to run the function which my spaceship fires, and touchesMoved() function to move the spaceship's x-position.
These are the code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
pShoot()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
spaceship.run(SKAction.moveTo(x: location.x, duration: 0.5))
}
}
What I am trying to do is to differentiate the clicking or touching indicator and pressed and move indicator, in other words I dont want the spaceship to fire when I am pressed and move on the screen, and I do not want the spaceship to move when I am clicking constantly but at different position. ( touchesMoved() detects changes in touch positions so if I am clicking at different positions spaceship will move which I dont want)
I would like to know what would be the best way of implementing this, thank you.
You could try checking the area that the user pressed and deciding whether or not to move the spaceship depending on the origin of the touch.

Detect separate touches on different SpriteKit nodes?

I am making a SpriteKit game where in order to begin the game, you need to hold two separate spots (SKShapeNodes) for 3 seconds. If you let go either finger or move either finger off a node, the game will not start. I have it working fine with 1 spot, but when I try to do 2 spots, I'm stuck. What is the simplest way to detect the 2 correct touches on the correct nodes?
This doesn't seem like a very uncommon situation, so if anyone knows the best way to handle this, I would appreciate the help.
Swift preferred, also.
Set multipleTouchEnabled to YES and use the touchesForView: method.
You can get more specific information on multi touch from the Apple Docs Multitouch Events.
The main idea is to have all touches when users provides any actions and operate them at the same time.
So, add 3 handlers to your Scene class:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
checkTouches((event?.allTouches())!)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
checkTouches((event?.allTouches())!)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
checkTouches((event?.allTouches())!)
}
In the checkTouches function you will see all touches with updated properties (like positions etc).
Simple example:
func checkTouches(touches: Set<UITouch>) {
// iterate over all touches
for t in touches {
let touch = t as UITouch
let touchLocation = touch.locationInNode(self)
if... <-- YOUR CODE HERE TO CHECK NODE NAME AND TOUCHED TIME
}
}
Using this approach you will be able to handle any changes simultaniously.
E.g. user may touch on your node, then move finger outside it and then move bach to this node.
Enjoy!

recognizing long press gesture in spriteKit

Hi I am currently moving a sprite by checking if the button was pressed in touchesBegan and then updating the position in update. I am doing this so that the user does not need to keep pressing the move up/down/left/right etc button over and over. The problem is that sometimes the sprite does not stop moving which im sure is due to this. Does anyone know a better solution to this? For brevity I will show you the way I am taking care of the up button.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
// Get the location of the touch in this scene
let location = touch.locationInNode(self)
// Check if the location of the touch is within the button's bounds
if upButton.containsPoint(location) {
upButtonPressed = true
}
override func update(currentTime: CFTimeInterval) {
if upButtonPressed == true {
ball.position.y += 3
}
I kept it simple here but I do have all the conditions to stop movement in my code. I am simply wondering if there is an easier way to do this with maybe a long press gesture recognizer?

Make UIImageView Go Up Then Down Swift

I need to make a UIImageView animate up when the screen is tapped then back down when it hits the top.
This is the code I have got at the moment:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
if (CGRectIntersectsRect(self.playerImagePhone.frame, self.floorImagePhone.frame)) {
UIView.animateWithDuration(0.4) {
self.playerImagePhone.center = CGPointMake(self.playerImagePhone.center.x, self.playerImagePhone.center.y - 50)
println("player jumped")
}
}
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
UIView.animateWithDuration(0.4) {
self.playerImagePhone.center = CGPointMake(self.playerImagePhone.center.x, self.playerImagePhone.center.y + 50)
println("player jumped")
}
}
This code is working to make it go up then back down but the problem is that it allows the user to hold their finger on the screen and the image will stay up. How can I make it go down as soon as the image is finished going up?
Thanks
touchesBegan can be called often... it probably isn't a good idea to perform your animations in there. You'll probably want to update a seperate object maintaining the animation, then once the animation is done (once the duration is over) you run another animation to bring the image back down.
As an alternative you can use also use animateWithDuration:animations:completion: and calling the down animation within the completion block.

Resources