0
votes

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.

1
"isn't working (at all)" is not a problem description. Describe what is happening and what should happen. - LearnCocos2D
nothing happens the player just falls, and there is no collision at all - that guy

1 Answers

-2
votes

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.