Swift | I'm trying to rotate a sprite and change the direction of the rotation ever time I tap - swift

I want to rotate a single sprite indefinitely, and every time I tap the button, I want the sprite to rotate in the opposite direction (back and forth from clockwise to counter-clockwise etc.
Below is the code that I have:
http://pastebin.com/Avj8Njwt
class GameScene: SKScene {
var center = SKSpriteNode()
var bg = SKSpriteNode()
var bigCircle = SKSpriteNode()
let counterClockwise = SKAction.rotateByAngle(CGFloat(3.14), duration:1)
let clockwise = SKAction.rotateByAngle(CGFloat(-3.14), duration:1)
var spin = SKAction()
override func didMoveToView(view: SKView) {
//Background
var bgTexture = SKTexture(imageNamed: "images/bg.png")
bg = SKSpriteNode(texture:bgTexture)
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
//Center Circle
var bigCircleTexture = SKTexture(imageNamed: "images/bigCircle.png")
bigCircle = SKSpriteNode(texture:bigCircleTexture)
bigCircle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(bigCircle)
//Center Triangle
var centerTexture = SKTexture(imageNamed: "images/center.png")
center = SKSpriteNode(texture:centerTexture)
center.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
spin = clockwise
center.runAction(SKAction.repeatActionForever(spin))
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if spin == clockwise {
spin = counterClockwise
}
else {
spin = clockwise
}
center.runAction(SKAction.repeatActionForever(spin))
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

Your problem is you're not removing the old SKAction trying to rotate your SKSpriteNode. To do that you need to keep track of which way your sprite is rotating. If I was going to implement this I would subclass SKSpriteNode, like so:
class RotatingSprite: SKSpriteNode {
// This is used to keep track of which way the sprite is rotating.
enum Direction {
case Left, Right
mutating func inverse() {
switch self {
case .Left : self = .Right
case .Right: self = .Left
}
}
}
// These names will be the keys used when running an action.
// This will allow you to stop the rotate-left or rotate-right actions.
static let rotateLeftName = "RotateLeftAction"
static let rotateRightName = "RotateRightAction"
var rotationDirection: Direction? {
didSet {
if let r = rotationDirection {
switch r {
// Checks the sprite isn't already rotating to the left.
// If it isn't, make the sprite rotate to the left.
case .Left where oldValue != .Left:
rotateLeft()
case .Right where oldValue != .Right:
rotateRight()
default:
break
}
}
}
}
private func rotateLeft() {
// Remove the action rotating the sprite to the right.
self.removeActionForKey(RotatingSprite.rotateRightName)
// And start the action rotating the sprite to the left.
let rotateAction = SKAction.rotateByAngle(-CGFloat(M_PI), duration: 1.0)
self.runAction(SKAction.repeatActionForever(rotateAction), withKey: RotatingSprite.rotateLeftName)
}
private func rotateRight() {
self.removeActionForKey(RotatingSprite.rotateLeftName)
let rotateAction = SKAction.rotateByAngle(CGFloat(M_PI), duration: 1.0)
self.runAction(SKAction.repeatActionForever(rotateAction), withKey: RotatingSprite.rotateRightName)
}
}
Now you can use RotatingSprite like so:
class GameScene: SKScene {
let rotatingSprite = RotatingSprite(texture:bgTexture)
override func didMoveToView(view: SKView) {
rotatingSprite.position = CGPoint(x: frame.midX, y: frame.midY)
self.addChild(rotatingSprite)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// If the sprite isn't turning you've got to set it off.
if rotatingSprite.rotationDirection == nil {
rotatingSprite.rotationDirection = .Left
// If it is turning, change its direction.
} else {
rotatingSprite.rotationDirection!.inverse()
}
}
}
Hope that helps!

It is extremely easy to achieve this. Try this ,
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
[sprite removeAllActions];
SKAction *action;
if (isClockWise)
{
action = [SKAction rotateByAngle:M_PI duration:1];
}
else
{
action = [SKAction rotateByAngle:-M_PI duration:1];
}
isClockWise = !isClockWise;
[sprite runAction:[SKAction repeatActionForever:action]];
}
Where sprite is SKSpriteNode and initiate isClockWise to Yes or No depending on your initial movement direction.

A quick and dirty way to do this is the following:
import SpriteKit
class GameScene: SKScene {
var center = SKSpriteNode()
var bg = SKSpriteNode()
var bigCircle = SKSpriteNode()
let counterClockwise = SKAction.rotateByAngle(CGFloat(3.14), duration:1)
let clockwise = SKAction.rotateByAngle(CGFloat(-3.14), duration:1)
var spin = SKAction()
// this is used to identify which direction we are going in. When we change it spin is changed as well
var isClockwise: Bool = true {
didSet {
if isClockwise {
spin = clockwise
} else {
spin = counterClockwise
}
}
}
let actionKey = "spin" // this is used to identify the SKAction
override func didMoveToView(view: SKView) {
//Background
var bgTexture = SKTexture(imageNamed: "images/bg.png")
bg = SKSpriteNode(texture:bgTexture)
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
//Center Circle
var bigCircleTexture = SKTexture(imageNamed: "images/bigCircle.png")
bigCircle = SKSpriteNode(texture:bigCircleTexture)
bigCircle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(bigCircle)
//Center Triangle
var centerTexture = SKTexture(imageNamed: "images/center.png")
center = SKSpriteNode(texture:centerTexture)
center.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
isClockwise = true // set the initial direction to clockwise
center.runAction(SKAction.repeatActionForever(spin), withKey: actionKey)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// remove the existing spin action
center.removeActionForKey(actionKey)
// reset the direction (this will automatically switch the SKAction)
isClockwise = !isClockwise
center.runAction(SKAction.repeatActionForever(spin), withKey: actionKey)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
You need to remove the action before you apply a new one - you can selectively remove the action by calling runAction(action:,withKey:). This enables you to be able to remove that same action using the same key. The logic for changing spin is in didSet for the isClockwise var declaration.

It was as simple as placing center.removeAllActions in each if statement to make sure the sprite isn't currently moving when the direction is supposed to be changed.

Related

SpriteKit creating a button issues

Im attempting to use some of the code from a solution found on this page for creating a button Create Button in SpriteKit: Swift
class GameScene: SKScene {
let button = SKSpriteNode(imageNamed: "yourImgName")
override func didMoveToView(view: SKView) {
button.name = "btn"
button.size.height = 100
button.size.width = 100
button.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 50)
self.addChild(button)
//Adjust button properties (above) as needed
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name {
if name == "btn" {
let yourNextScene = YourNextScene(fileNamed: "YourNextScene")
self.view?.presentScene(yourNextScene!)
}
}
}
}
and the current code that I have is supposed to make the player jump when the button is pressed, but nothing is currently happening when its pressed
import SwiftUI
import SpriteKit
import UIKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let button = SKSpriteNode(imageNamed: "playerOBJ")
let player = SKSpriteNode(imageNamed: "playerOBJ")
let playerRadius = player.frame.width / 2.0
player.position = CGPoint(x: 200, y: 500)
player.name = "Jimmy"
addChild(player)
player.physicsBody = SKPhysicsBody(circleOfRadius: playerRadius)
player.physicsBody?.allowsRotation = false
player.physicsBody?.friction = 0
player.physicsBody?.restitution = 0
player.zPosition = 100
// Button
button.name = "btn"
button.size.height = 100
button.size.width = 100
button.position = CGPoint(x: 100, y: 100)
self.addChild(button)
// Physics
physicsBody = SKPhysicsBody(edgeLoopFrom: frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)))
Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { _ in
player.physicsBody?.applyForce(CGVector(dx: 100, dy: 1000))
}
func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let positionInScene = touch!.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name {
if name == "btn" {
player.physicsBody?.applyForce(CGVector(dx: 0, dy: 10000))
}
}
}
}
override func update(_ currentTime: TimeInterval) { }
}
I'm thinking that maybe this is an issue with the press not being resitered at all but I'm not fully sure
Your main problem is you put all of your code inside the didMove function. You put the touchesBegan function inside the didMove function. When the didMove function finishes, touchesBegan goes away so none of your touches are going to be handled in the game.
You also declared the button and the player as local variables inside the didMove function.
override func didMove(to view: SKView) {
let button = SKSpriteNode(imageNamed: "playerOBJ")
let player = SKSpriteNode(imageNamed: "playerOBJ")
// Rest of function omitted
}
When the didMove function finishes, the button and player go away too. You also have the same image name for the player and button.
The fix is to make the button and player variables properties of the GameScene class. Move touchesBegan out of the didMove function too.
class GameScene: SKScene {
// Declare button and player here.
var button = SKSpriteNode()
var player = SKSpriteNode()
override func didMove(to view: SKView) {
// Initialize button, player, and everything else here.
}
func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Code in touchesBegan func goes here.
}
}

Is there a way I can add more than one sprite in SpriteKit globally?

I am struggling with one issue. Global declaration of my sprite so that I can interact with it. In this game, I have created a local sprite called enemy featured below:
func spawnEnemy() {
let enemy = SKSpriteNode(imageNamed: "as")
let yPosition = CGFloat(frame.maxY - enemy.size.height)
let getXvalue = GKRandomDistribution(lowestValue: Int(frame.minX + enemy.size.width), highestValue: Int(frame.maxX - enemy.size.width))
let xPosition = CGFloat(getXvalue.nextInt())
enemy.position = CGPoint(x: xPosition, y: yPosition)
enemy.name = "asteroid"
enemy.zPosition = 100
addChild(enemy)
let animationDuration:TimeInterval = 6
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: xPosition, y: 0), duration: animationDuration))
actionArray.append(SKAction.self.removeFromParent())
enemy.run(SKAction.sequence(actionArray))
}
I want to tap the enemy to make it disappear from the screen. The variable is declared locally and not globally so the touchesBegan function does not "see" enemy. However, when I move the statement:
let enemy = SKSpriteNode(imageNamed: "as")
outside of local declaration and into global. It works until the code tries to spawn in another enemy and i get an error of "Tried to add an SKNode who already has a parent" This is the code I have running in my view did load:
run(SKAction.repeatForever(SKAction.sequence([SKAction.run{self.spawnEnemy()
}, SKAction.wait(forDuration: 1.0)])))
Every time it spawns a new enemy it crashes and says that the SKNode already has a parent which i understand. However, for my game to function I need the player to be able to touch the individual instance of that enemy and remove it. Hence my code for
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in:self) {
let nodesArray = self.nodes(at:location)
if nodesArray.first?.name == "asteroid" {
print("Test")
enemy.removeFromParent()
print("Test Completed")
}
}
}
Now the error says unresolved use of "enemy" because the enemy is not global. I have been going back and forth on this issue for quite some time. If anyone has any potential solution or work around I would be very grateful, and thank you for your help.
Move your enemies to their own class and handle the touch for each of those enemies in their own class. This cleans up your GameScene and keeps your code more organized. You can now add as many instances of enemy as you want.
FYI not related to this question but somethings to consider after you get this working
when game over or level change or win make sure you have a clean up function to remove all enemies
you should strongly consider recycling your objects vs creating them on the fly...better performance
try to separate as much code to your objects class as possible
class enemy: SKSpriteNode {
init() {
super.init(texture: nil, color: .clear, size: CGSize.zero)
setup()
}
func setup() {
isUserInteractionEnabled = true
name = "asteroid"
zPosition = 100
let image = SKSpriteNode(imageNamed: "as")
imagine.zPosition = 1
addChild(image)
self.size = image.size
animate()
}
func animate() {
let animationDuration: TimeInterval = 6
let move = SKAction.move(to: CGPoint(x: xPosition, y: 0), duration: animationDuration)
let remover = SKAction.self.removeFromParent()
run(SKAction.sequence(move, remover))
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
removeFromParent()
}
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
let sequence = SKAction.sequence([SKAction.run{ self.spawnEnemy()
}, SKAction.wait(forDuration: 1.0)])
run(SKAction.repeatForever(sequence))
}
func spawnEnemy() {
let enemy = Enemy()
let yPosition = CGFloat(frame.maxY - enemy.size.height)
let getXvalue = GKRandomDistribution(lowestValue: Int(frame.minX + enemy.size.width), highestValue: Int(frame.maxX - enemy.size.width))
let xPosition = CGFloat(getXvalue.nextInt())
enemy.position = CGPoint(x: xPosition, y: yPosition)
addChild(enemy)
}
}

Game crash only on iPhone X and iPhone 8 Plus

I'm working on game and the game works perfect on all iPhones and iPad expect iPhone X and iPhone 8 Plus
When I add sprite to the scene the game crash (only on iPhone X and iPhone 8 Plus) because the sprite is already added to scene, On others iPhone the app not crash, I think the bug is in my timer, maybe the bug it's the iPhone simulator? What do you think?
Code:
class GameScene: SKScene, SKPhysicsContactDelegate {
var finger = SKSpriteNode(imageNamed: "Finger") //Finger to swipe the biscuit
var biscuit = SKSpriteNode(imageNamed: "Biscuit")
var glass = SKSpriteNode(imageNamed: "Glass")
var defaultbiscuitpos:CGPoint = CGPoint()
var defaultfingerpos:CGPoint = CGPoint()
/*** Drag Biscuit vars ***/
var touchpoint:CGPoint = CGPoint()
var istouching:Bool = false
var fadeoutfinger = SKAction()
var fadeinfinger = SKAction()
var fingergroup = SKAction()
var burnanimtion = SKAction()
var movefinger = SKAction()
let fingertimer:String = "fingertimer"
var isGameover:Bool = false
//Game mode enum
enum gamemode {
case dip
case ready
case out
case gameover
}
//Game mode (Dip,Ready,Out or game over by enum) **Now is Dip
var mymode = gamemode.dip
override func didMove(to view: SKView) {
//Finger
finger.name = "Finger"
finger.position = CGPoint(x: biscuit.position.x + finger.frame.width, y: biscuit.position.y)
defaultfingerpos = finger.position
finger.alpha = 1.0
finger.zPosition = 5
//Start finger timer to make animation
createTimer(name: fingeranimation, waitt: 3.0, sprite: finger, actioname: fingertimer)
}
//Finger timer func
func fingeranimation () {
//Check if timer is over 4 seconds and the title is dip
if mymode == gamemode.dip {
//Add finger to screen
addChild(finger)
//Set fade in animation for finger
fadeinfinger = SKAction.fadeIn(withDuration: 2.0)
//Set move animation for finger
movefinger = SKAction.moveTo(y: glass.frame.midX, duration: 2.0)
//Set fade out animation for finger
fadeoutfinger = SKAction.fadeOut(withDuration: 2.0)
fingergroup = SKAction.group([fadeinfinger,movefinger,fadeoutfinger])
finger.run(fingergroup, completion: {
//Remove finger from screen
self.finger.removeFromParent()
//Return the finger to apper and return the finger to default position
self.finger.alpha = 1.0
self.finger.position = self.defaultfingerpos
})
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: self)
//Tap on biscuit
if biscuit.contains(location) && mymode != gamemode.gameover {
touchpoint = location
istouching = true
biscuit.physicsBody?.pinned = false
//Stop the finger animation timer
stopTimer(actioname: fingertimer, sprite: finger)
}
}
//Make timer function
func createTimer (name:#escaping os_block_t , waitt:TimeInterval, sprite:SKSpriteNode?,actioname: String) {
let myaction = SKAction.sequence([SKAction.wait(forDuration: waitt), SKAction.run(name)])
run(SKAction.repeatForever(myaction), withKey: actioname)
}
//Stop timer function
func stopTimer(actioname:String, sprite:SKSpriteNode?) {
removeAction(forKey: actioname)
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if istouching && isGameover == false {
let dt:CGFloat = 1.0/15
let distance = CGVector(dx: touchpoint.x - biscuit.position.x, dy: touchpoint.y - biscuit.position.y * 1.65)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
self.biscuit.physicsBody!.velocity = velocity
}
}
}
This problem is now fixed with the release of Xcode 9.1.

Rotate a sprite along an axis

In SpriteKit I need to rotate a sprite along an axis (e.g. the one that passes through the center of the sprite) just like a wheel to be spinned by the user.
I tried to use applyTorque function (to apply a force that is only angular and not linear), but I cannot handle the different forces caused by different movements on the screen (longer the touch on the screen, stronger the force to apply).
Can someone help me to understand how to deal with this problem?
Here is an answer that spins a ball according to how fast you swipe left / right:
class GameScene: SKScene {
let speedLabel = SKLabelNode(text: "Speed: 0")
let wheel = SKShapeNode(circleOfRadius: 25)
var recognizer: UIPanGestureRecognizer!
func pan(recognizer: UIPanGestureRecognizer) {
let velocity = recognizer.velocity(in: view!).x
// Play with this value until if feels right to you.
let adjustor = CGFloat(60)
let speed = velocity / adjustor
wheel.physicsBody!.angularVelocity = -speed
}
// Scene setup:
override func didMove(to view: SKView) {
removeAllChildren()
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
wheel.fillColor = .blue
wheel.physicsBody = SKPhysicsBody(circleOfRadius: 25)
wheel.physicsBody!.affectedByGravity = false
let wheelDot = SKSpriteNode(color: .gray, size: CGSize(width: 5, height:5))
wheel.addChild(wheelDot)
wheelDot.position.y += 20
wheel.setScale(3)
speedLabel.setScale(3)
speedLabel.position.y = (frame.maxY - speedLabel.frame.size.height / 2) - 45
recognizer = UIPanGestureRecognizer(target: self, action: #selector(pan))
view.addGestureRecognizer(recognizer)
addChild(wheel)
addChild(speedLabel)
}
override func didSimulatePhysics() {
speedLabel.text = "Speed: \(abs(Int(wheel.physicsBody!.angularVelocity)))"
}
}
Here is a basic example of spinning a wheel clockwise or counterclockwise depending on if you press on left / right side of screen. Hold to increase speed:
class GameScene : SKScene {
enum Direction { case left, right }
var directionToMove: Direction?
let wheel = SKShapeNode(circleOfRadius: 25)
let speedLabel = SKLabelNode(text: "Speed: 0")
override func didMove(to view: SKView) {
// Scene setup:
anchorPoint = CGPoint(x: 0.5, y: 0.5)
removeAllChildren()
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
wheel.fillColor = .blue
wheel.physicsBody = SKPhysicsBody(circleOfRadius: 25)
wheel.physicsBody!.affectedByGravity = false
let wheelDot = SKSpriteNode(color: .gray, size: CGSize(width: 5, height:5))
wheel.addChild(wheelDot)
wheelDot.position.y += 20
wheel.setScale(3)
speedLabel.setScale(3)
speedLabel.position.y = (frame.maxY - speedLabel.frame.size.height / 2) - 45
addChild(wheel)
addChild(speedLabel)
}
// Change this to touchesBegan for iOS:
override func mouseDown(with event: NSEvent) {
// Change this to touches.first!.location(in: self) for iOS.
let location = event.location(in: self)
// Determine if touch left or right side:
if location.x > 0 {
directionToMove = .right
}
else if location.x < 0 {
directionToMove = .left
}
}
override func mouseUp(with event: NSEvent) {
// Stop applying gas:
directionToMove = nil
print("lol")
}
override func update(_ currentTime: TimeInterval) {
// This is how much speed we gain each frame:
let torque = CGFloat(0.01)
guard let direction = directionToMove else { return }
// Apply torque in the proper direction
switch direction {
case .left:
wheel.physicsBody!.applyTorque(torque)
case .right:
wheel.physicsBody!.applyTorque(-torque)
}
}
override func didSimulatePhysics() {
// Speedometer:
speedLabel.text = "Speed: \(abs(Int(wheel.physicsBody!.angularVelocity)))"
}
}

sprite-kit collision not working

Hi everyone I am trying to make a basic line drawing test with xcode 6 using swift. But my collision system isn't working at all. This is the code of my collision system:
func drawLines() {
CGPathMoveToPoint(path, nil, location.x, location.y)
CGPathAddLineToPoint(path, nil, self.frame.size.width/2, self.frame.size.height / 5)
drawLine.append(SKShapeNode())
drawLine[index] = SKShapeNode()
line.append(drawLine[index])
line[index].path = path
line[index].strokeColor = UIColor.redColor()
line[index].lineWidth = 5.0
line[index].physicsBody = SKPhysicsBody(rectangleOfSize: line[index].frame.size)
line[index].physicsBody.dynamic = false
line[index].zPosition = 1
self.addChild(line[index])
index++
}
I can't figure out the problem but I think I made a mistake in that piece of code.
Here is the rest of my code:
class GameScene: SKScene {
var line: [SKShapeNode] = []
var drawLine: [SKShapeNode] = []
var path = CGPathCreateMutable()
var touch: UITouch!
var location:CGPoint!
var index = 0
let player = SKSpriteNode(imageNamed: "player")
override func didMoveToView(view: SKView) {
player.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
player.physicsBody.dynamic = true
self.physicsWorld.gravity = CGVectorMake(0,-1)
player.zPosition = 1
self.addChild(player)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
touch = touches.anyObject() as UITouch!
location = touch.locationInNode(self)
drawLines()
}
func drawLines() {
CGPathMoveToPoint(path, nil, location.x, location.y)
CGPathAddLineToPoint(path, nil, self.frame.size.width/2, self.frame.size.height / 5)
drawLine.append(SKShapeNode())
drawLine[index] = SKShapeNode()
line.append(drawLine[index])
line[index].path = path
line[index].strokeColor = UIColor.redColor()
line[index].lineWidth = 5.0
line[index].physicsBody = SKPhysicsBody(rectangleOfSize: line[index].frame.size)
line[index].physicsBody.dynamic = false
line[index].zPosition = 1
self.addChild(line[index])
index++
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
-------------------EDIT -------------------
I now use this line of code for the physics of my line:
line[index].physicsBody = SKPhysicsBody(rectangleOfSize:
CGSizeMake(line[index].frame.width*2 ,line[index].frame.height*2))
This problem here I think is that when I draw my line at an angle the rectangle doesn't turn with it so my rectangle ends up way to big.
As far as I know, touchesBegan returns an NSSet of touches (an array of touches roughly saying), therefore you would want to loop through them or access a specific touch by its index. I would recommend looping through touches like the following code.
override func touchesBegan(touches:NSSet) {
for touch: AnyObject in touches {
var location:CGPoint = touch.locationInNode(self.scene)
performSomethingCrazy(withLocationAt: location)
}
}
Enjoy.