How can I move sprites around with my finger? - ios

I want to be able to move a sprite with my finger. I first have a variable for whether the finger is on the sprite or not:
var isFingerOnGlow = false
Then I have the touchesBegan function where I change the value of the above variable if the user touches the sprite:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var touch = touches.anyObject() as UITouch!
var touchLocation = touch.locationInNode(self)
if let body = physicsWorld.bodyAtPoint(touchLocation) {
if body.node!.name == GlowCategoryName {
println("Began touch on hero")
isFingerOnGlow = true
}
}
}
Sometimes it works, and the console displays "began touch on hero", sometimes not. Is there a better way to code this first part?
Then I wonder if my following code for touchesMoved is effective:
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if isFingerOnGlow{
var touch = touches.anyObject() as UITouch!
var touchLocation = touch.locationInNode(self)
var previousLocation = touch.previousLocationInNode(self)
var glow = SKSpriteNode(imageNamed: GlowCategoryName)
// Calculate new position along x for glow
var glowX = glow.position.x + (touchLocation.x - previousLocation.x)
var glowY = glow.position.y + (touchLocation.y - previousLocation.y)
// Limit x so that glow won't leave screen to left or right
glowX = max(glowX, glow.size.width/2)
glowX = min(glowX, size.width - glow.size.width/2)
glow.position = CGPointMake(glowX, glowY)
}
}
...
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
isFingerOnGlow = false
}

You can modify the functions like this to track the touches.
var isFingerOnGlow = false
var touchedGlowNode : SKNode!
override func touchesBegan(touches: NSSet, withEvent event:UIEvent) {
var touch = touches.anyObject() as UITouch!
var touchLocation = touch.locationInNode(self)
let body = self.nodeAtPoint(touchLocation)
if body.name == GlowCategoryName {
println("Began touch on hero")
touchedGlowNode = body
isFingerOnGlow = true
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if isFingerOnGlow{
let touch = touches.anyObject() as UITouch!
let touchLocation = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
let distanceX = touchLocation.x - previousLocation.x
let distanceY = touchLocation.y - previousLocation.y
touchedGlowNode.position = CGPointMake(touchedGlowNode.position.x + distanceX,
touchedGlowNode.position.y + distanceY)
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
isFingerOnGlow = false
}

Related

Node not moving

I'm trying to throw a node around the screen, I've taken the below code but nothing actually happens.
When the scene loads the node spawns in the middle of the screen then drops to the bottom.
The code was taken from elsewhere in the site and granted it was a bit old so I'm assuming I'm missing something?
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
override func didMove(to view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
sprite = SKSpriteNode(color: UIColor.red, size: CGSize(width: 50, height: 50))
sprite.physicsBody = SKPhysicsBody(rectangleOf: sprite.size)
sprite.position = CGPoint(x: 50.0, y: 50.0)
self.addChild(sprite)
}
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
}
func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
touchPoint = location
}
}
func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
touching = false
}
func update(currentTime: TimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = velocity
}
}
}

touch moking sprite by tap not swipe swift

I have a block in my code where the sprite will only move if you tap the screen. I would like to have this behaviour when I swipe up or down instead. Here is my code
import SpriteKit
import UIKit
class GameScene: SKScene {
var porker:Porker!
var touchLocation = CGFloat()
var gameOver = false
override func didMoveToView(view: SKView) {
addBG()
addPig()
}
func addBG() {
let bg = SKSpriteNode(imageNamed: "bg");
addChild(bg)
}
func addPig() {
let Pig = SKSpriteNode(imageNamed: "pig")
porker = Porker(guy:Pig)
addChild(Pig)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches{
if !gameOver {
touchLocation = (touch.locationInView(self.view!).y * -1) + (self.size.height/2)
}
}
let moveAction = SKAction.moveToY(touchLocation, duration: 0.5)
moveAction.timingMode = SKActionTimingMode.EaseOut
porker.guy.runAction(moveAction)
}
}
func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
Replace touchesBegan with this code:
// Store the start touch position
let yTouchCurrentPosition = 0.0
let yTouchDistance = 0.0
let yTouchStartPosition = 0.0
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
yTouchStartPosition = touch.locationInNode(self).y
}
}
// Calculate the distance of the touch movement
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
yTouchCurrentPosition = touch.locationInNode(self).y
yTouchDistance = yTouchStartPosition - yTouchCurrentPosition
}
}
// Reset all movement states and move sprite
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
yTouchCurrentPosition = 0.0
yTouchDistance = 0.0
yTouchStartPosition = 0.0
let moveAction = SKAction.moveToY(CGPoint(porker.guy.location.x, porker.guy.location.y + yTouchDistance), duration: 0.5)
moveAction.timingMode = SKActionTimingMode.EaseOut
porker.guy.runAction(moveAction)
}

nodeAtPoint wrong node

I'm making a game with swift and spritekit and I have a function where I have multiple sprites falling from the top of the screen. I also made it so I can be able to detect a touch on the sprite using nodeAtPoint, and made it possible to flick the sprites. My problem is that due to nodeAtPoint dragging the deepest node in the tree, when i click and drag on a sprite, the newest sprite in the scene gets pulled toward my touch instead of the one i originally touched. If anyone has some suggestions on how to only affect the node I touched I'd really appreciate it.
class GameScene: SKScene {
var ball = SKSpriteNode(imagedNamed: "blueBlue")
var touchedNode: SKNode? = SKNode()
override func didMoveToView(view: SKView) {
var create = SKAction.runBlock({() in self.createTargets()})
var wait = SKAction.waitForDuration(2)
var waitAndCreateForever = SKAction.repeatActionForever(SKAction.sequence([create, wait]))
self.runAction(waitAndCreateForever)
}
func createTargets() {
ball = SKSpriteNode(imageNamed: "blueBlue")
let randomx = Int(arc4random_uniform(170) + 290)
ball.zPosition = 0
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width / 11)
ball.physicsBody?.dynamic = true
ball.size = CGSize(width: ball.size.width / 1.5, height: ball.size.height / 1.5)
let random = Int(arc4random_uniform(35))
let textures = [texture1, texture2, texture3, texture4, texture5, texture6, texture7, texture8, texture9, texture10, texture11, texture12, texture13, texture14, texture15, texture16, texture17, texture18, texture19, texture20, texture21, texture22, texture23, texture24, texture25, texture1, texture7, texture18, texture24, texture25, texture1, texture7, texture18, texture24, texture25]
ball.texture = textures[random]
ball.position = CGPoint(x: randomx, y: 1400)
addChild(ball)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
touchedNode = self.nodeAtPoint(touchLocation)
if touchedNode.frame.contains(touchLocation) {
touching = true
touchPoint = touchLocation
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
touching = true
touchPoint = touchLocation
}
override func update(currentTime: NSTimeInterval) {
if touching {
if touchPoint != touchedNode.position {
let dt: CGFloat = 0.1
let distance = CGVector(dx: touchPoint.x - touchedNode.position.x, dy: touchPoint.y - touchedNode.position.y)
let vel = CGVector(dx: distance.dx / dt, dy: distance.dy / dt)
if touchedNode.parent != nil {
touchedNode.physicsBody?.velocity = vel
}
}
}
}
}
I suggest you make the following changes to the code:
Add a check to see if a sprite is already being moved (if yes, do nothing)
Turn off the affectedByGravity property for the sprite being moved
Reset touchedNode and touching variables when the touch ends
Here's an implementation with the above changes...
Replace touchesBegan and touchesMoved with the following
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
if (!touching) {
let node = self.nodeAtPoint(touchLocation)
if node is SKSpriteNode {
touchedNode = node
touching = true
touchPoint = touchLocation
touchedNode!.physicsBody?.affectedByGravity = false
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if (touching) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
touchPoint = touchLocation
}
}
And add this method
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if (touchedNode != nil) {
touchedNode!.physicsBody!.affectedByGravity = true
}
touchedNode = nil
touching = false
}

sprite kit : contact not working

I am trying to make one simple game and this problem happen first my paddle can touch the ball but now it cant do that and
this code also should give me message or in simulator but it is not showing any idea?
import SpriteKit
class GameScene: SKScene , SKPhysicsContactDelegate{
var istouchingpaddle = false
let ballcatagery:UInt32 = 0 * 1 << 0
let paddlecategary :UInt32 = 0 * 1 << 1
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let border = SKPhysicsBody(edgeLoopFromRect: self.frame)
border.friction = 0
self.physicsBody = border
self.physicsWorld.gravity = CGVectorMake(0,-9.8)
self.physicsWorld.contactDelegate = self
let ball = childNodeWithName ("ball") as SKSpriteNode
ball.physicsBody?.applyImpulse(CGVectorMake(30, -30))
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.restitution = 1
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody!.categoryBitMask = ballcatagery
let paddle = childNodeWithName("paddle") as SKSpriteNode
paddle.physicsBody!.categoryBitMask = paddlecategary
ball.physicsBody?.contactTestBitMask = paddlecategary
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == ballcatagery && contact.bodyB.categoryBitMask == paddlecategary{
println("working ")
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
var touch = touches.anyObject() as UITouch
var location = touch.locationInNode(self)
if let body = self.physicsWorld.bodyAtPoint(location){
if body.node!.name == "paddle" {
istouchingpaddle = true
}
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if istouchingpaddle{
var touch = touches.anyObject() as UITouch
var location = touch.locationInNode(self)
var prevlocation = touch.previousLocationInNode(self)
var paddle = childNodeWithName("paddle") as SKSpriteNode
var position = paddle.position.x + (location.x - prevlocation.x)
position = max(position,paddle.size.width/2)
position = min(position, size.width - paddle.size.width/2)
paddle.position = CGPoint(x: position, y: paddle.position.y)
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
istouchingpaddle = false
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
} }
I can produce good results with your code if I change the way of defining categories. Try setting categories like this :
let ballcatagery: UInt32 = 0x1 << 0 // Not that you have misspelled word "category", which can lead to errors if you expect somewhere a word "category" instead of your actual "categary".
let paddlecategary : UInt32 = 0x1 << 1
I can post you the whole code , but you should be good with this.

Draw lines with Sprite kit and swift

I'm using Sprite kit with Swift to assay drawing lines with fingers.
Code:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
touch = touches.anyObject() as UITouch!
firstPoint = touch.locationInNode(self)
}
override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
var touch = touches.anyObject() as UITouch!
var positionInScene = touch.locationInNode(self)
lineNode.removeFromParent()
CGPathMoveToPoint(pathToDraw, nil, firstPoint.x, firstPoint.y)
CGPathAddLineToPoint(pathToDraw, nil, positionInScene.x, positionInScene.y)
lineNode.path = pathToDraw
lineNode.lineWidth = 10.0
lineNode.strokeColor = UIColor.redColor()
self.addChild(lineNode)
firstPoint = positionInScene
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
}
But the lines obtained are empty, only the border has color (image).
Any idea?
SKShapeNode has a property called fillColor which is by default set to clearColor. In your code, you only set the strokeColor(outline) to red, but you should also do so to fillColor.
You can do this with lineNode.fillColor = UIColor.redColor()
Good luck!

Resources