norm
norm

Reputation: 11

'Cannot convert value of type 'Int' to expected argument type 'CGPoint'

In the code:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    self.runAction(SKAction.playSoundFileNamed("ball.mp3", waitForCompletion: false))

    let touch = touches.first
    var location:CGPoint = touch!.locationInNode(self)

    var torpedo: SKSpriteNode = SKSpriteNode(imageNamed: "heartbullet")
    torpedo.position = player.position

    torpedo.physicsBody = SKPhysicsBody(circleOfRadius: torpedo.size.width/2)
    torpedo.physicsBody?.dynamic = true
    torpedo.physicsBody?.categoryBitMask = photoTorpedoCategory
    torpedo.physicsBody?.contactTestBitMask = alienCategory
    torpedo.physicsBody?.collisionBitMask = 0
    torpedo.physicsBody?.usesPreciseCollisionDetection = true

    var offset:CGPoint = vecSub(location, b: torpedo.position)
    if (offset.y < 0){
        return
    }

    self.addChild(torpedo)

    var direction:CGPoint = vecNormalize(offset)

    var shotLength:CGPoint = vecMult(direction, b: 1000)
    var finalDestination:CGPoint = vecAdd(shotLength, b: torpedo.position)

    let velocity = 568/1
    let moveDuration:Float = Float(self.size.width) / Float(self.size.width) / Float(velocity)

    var actionArray:[SKAction] = [SKAction]()
    actionArray.append(SKAction.moveTo(finalDestination, duration: NSTimeInterval(moveDuration)))
    actionArray.append(SKAction.removeFromParent())

    torpedo.runAction(SKAction.sequence(actionArray))
}


func vecAdd(a:CGPoint, b:CGPoint)->CGPoint{
    return CGPointMake(a.x + b.x, a.y + b.y)
}

func vecSub(a:CGPoint, b:CGPoint)->CGPoint{
    return CGPointMake(a.x - b.x, a.y - b.y)
}

func vecMult(a:CGPoint, b:CGPoint)->CGPoint{
    return CGPointMake(a.x * b.x, a.y * b.y)
}

func vecLenght(a:CGPoint)->CGFloat{
    return CGFloat(sqrtf(CFloat(a.x)*CFloat(a.x)+CFloat(a.y)*CFloat(a.y)))
}

func vecNormalize(a:CGPoint)->CGPoint{
    let length:CGFloat = vecLenght(a)
    return CGPointMake(a.x / length, a.y / length)
}

It shows the error in this line:

var shotLength:CGPoint = vecMult(direction, b: 1000)

'Cannot convert value of type 'Int' to expected argument type 'CGPoint'

I've been searching and i don't know how to fix it.

Can someone help me?

Upvotes: 0

Views: 3410

Answers (1)

David Berry
David Berry

Reputation: 41226

Then, your problem is that you have a function that expects 2 points and you're passing it a point and a number. You probably want to redefine vecMult as:

func vecMult(a:CGPoint, b:CGFloat) -> CGPoint {
    return CGPoint(x:a.x * b, y:a.y * b)
}

Upvotes: 1

Related Questions