Swift- dragging a custom made slider - swift

So I'm making a game and was previously using left and right buttons to move. Im thinking about changing it to a custom type of slider similar to this http://gyazo.com/05b6079862874a282ad14220e7267bd1 I'm not sure how to have it update the touches began so register you sliding your finger. What I have so far is this:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.sliderBox{
let move = touch.locationInNode(self)
slider.position.x = move.x
}
}
This gets the red dot to move to where I have touched, but I need to be able to have it like that and be able to slide when you hold it down. Thanks for taking the time to look at this.

fixed by changing override func touchesBegan to func touchesMoved
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.slider{
let move = touch.locationInNode(self)
slider.position.x = move.x
}
}
}

Related

Override function with sprite kit game

I am migrating my game from swift 1 and I can't figure out how to fix this function. I am trying to detect if no touches are taking place, which should keep the ball still. this is the code i have and keep getting error Invalid redeclaration of touchesBegan. Im not sure how to work around this, I've been stuck for a while and can't get the ball to stop moving when no touches present. Please help, thanks
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// do nothing if ad is showing
if Ad.share().showing {
return
}
let touch = touches.first! as UITouch
let touchedLocation = touch.locationInNode(self)
// change the speedXFirst of the ball if it starts
if start {
// start move
let moveRight = touchedLocation.x > CGRectGetMidX(self.frame)
let speedX = moveRight ? ballSpeedX : -ballSpeedX
ball.speedXFirst = speedX
}
// start game if it stops
else {
startGame()
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { //error RIGHT HERE
if start {
// stop move
ball.speedXFirst = 0
}
If you ball must be stopped when the user detaches the finger from the screen, probably you want to use:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
or this if you need to know when the touches were interrupted:
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}

How can i alter my program to allow this object to only react to a single touch?

I am making a basketball game. My problem is when I shoot the ball, i can then touch the ball in mid-air again, which i do not want. Once the user shoots the ball I do not want touch to have an effect. Here is my code:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?
{
touching = false
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if ball.frame.contains(location){
touchingPoint = location
touching = true
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
touchingPoint = location
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if touching{
let dt:CGFloat = 8.25/29.5
let distance = CGVector(dx: touchingPoint.x-ball.position.x, dy: touchingPoint.y-ball.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
ball.physicsBody!.velocity=velocity
}
}
What can i add to my program so that a touch won't have any effect once the ball has been shot?
after you shoot the ball disable userInteraction
ball.userInteractionEnabled = false
then enable it again when you want them to be able to touch it

Moving SKSpriteNode over screen

I want to move an SKSpriteNode over the screen with touch. The issue I have is that if I lock onto one sprite and then my finger drags it over another sprite the first one is let go and my finger starts to drag the second one. But once I touch the first sprite this is the only one I want to move.
There must be a simple way to resolve this?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let touch: UITouch = touches.first as UITouch!
touchLocation = touch.locationInNode(self)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: UITouch = touches.first as UITouch!
let newTouchLocation = touch.locationInNode(self)
let targetNode = self.nodeAtPoint(touchLocation)
if targetNode.physicsBody!.categoryBitMask != PhysicsCategory.Ball {
targetNode.runAction(SKAction.moveBy(CGVector(point: newTouchLocation - touchLocation), duration: 0.0))
touchLocation = newTouchLocation
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//I don't do anything with this yet
}
This is happening because your touchesMoved function is running every time your finger moves and if your finger moves over a new node, that node will be assigned to targetNode. You can fix this by changing this line:
let targetNode = self.nodeAtPoint(touchLocation)
To this:
let targetNode = self.nodeAtPoint(touch)
This way the node at the first touch will be the one that follows your finger and not the one at the last touch.
Thanks to TheCodeComposer, I found the solution:
var targetNode: SKNode!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let touch: UITouch = touches.first as UITouch!
touchLocation = touch.locationInNode(self)
targetNode = self.nodeAtPoint(touchLocation)
objectTouched(touch.locationInNode(self))
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: UITouch = touches.first as UITouch!
let newTouchLocation = touch.locationInNode(self)
// let targetNode = self.nodeAtPoint(touchLocation)
if targetNode.physicsBody!.categoryBitMask != PhysicsCategory.Ball {
targetNode.runAction(SKAction.moveBy(CGVector(point: newTouchLocation - touchLocation), duration: 0.0))
touchLocation = newTouchLocation
}
}
I only define
targetNode
once and then continue to use it instead of redefining it in touchesMoved:

How to create background that follows the character in SWIFT?

I am working on this project in Xcode, where I am making animations. I have perfectly working program where little character walks around the screen. If I press on screen, that is direction the character is going. What I want to do here is to add a background image and make it follow where character is going. Basically, I want character to be able to walk around big map while he does not go out of the image. Something like scrollView, but only in gamescene.
P.S. All the code below is working, I just want to add the background image that moves along.
Here is some code for my main gamescene
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// the hero is initialized with a texture
hero = Player(named: "hero_walk_down_0")
hero.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(hero)
if Settings.sharedInstance.virtualPad {
controller = ControllerNode(position: CGPoint(x:0, y: 0))
self.addChild(controller)
}
backgroundColor = SKColor.blueColor()
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if Settings.sharedInstance.virtualPad == false {
hero.destination = location
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
if Settings.sharedInstance.virtualPad == false {
hero.destination = location
}
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touchesEndedOrCancelled(touches, withEvent: event)
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
touchesEndedOrCancelled(touches, withEvent: event)
}
func touchesEndedOrCancelled(touches: NSSet, withEvent event: UIEvent) {
// when a touch is ended, remove it from the list
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
if Settings.sharedInstance.virtualPad == false {
hero.destination = nil
}
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if Settings.sharedInstance.virtualPad {
hero.walk(self.controller!.pressedDirections())
}
else {
if hero.destination != nil {
hero.walkTowards(hero.destination!)
}
}
}
You create two SKNodes - one for the character and one for the background and add them to each group separately.
Whenever you move the SKNode of the character, you replicate the same action on the background node.

Using Touchesbegan once

I have a touchesbegan and a touchesmoved that move a UIImageView around the screen. But I want to make it so I can't start moving around the screen then go to the opposite side using touchesbegan. I don't want to be able to just tap my way to other side. Is that even possible? Here's my code:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var touch : UITouch! = touches.anyObject() as UITouch
location = touch.locationInView(self.view)
Player.center = location
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
var touch : UITouch! = touches.anyObject() as UITouch
location = touch.locationInView(self.view)
Player.center = location
}
Thanks.