Reputation: 105
In my Swift Game I have three functions that controls the missiles shooting. Two of these have parameters. But I'm having problems calling the one's with parameters in the didMove properly. When I add for example the "func direction()", swift requires me to add CGPoint for "to target" and "from". What am I suppose to type in there? I haven't really been working on parameters before. The game works fine when I run it, but since I cant call it in the didMove, when i press "Retry" for example, the missiles aren't shooting since it's not called in the didMove.
func fireMissile() {
let missile = SKSpriteNode(color: .yellow, size: CGSize(width: 20,
height: 5))
missile.name = "Missile"
missile.position = CGPoint(x: player.position.x + 28, y:
player.position.y + 10)
missile.zPosition = 2
missile.physicsBody = SKPhysicsBody(rectangleOf: missile.size)
missile.physicsBody?.isDynamic = false
missile.physicsBody?.categoryBitMask = ColliderType.Bullet
missile.physicsBody?.collisionBitMask = ColliderType.Enemy |
ColliderType.Boat
missile.physicsBody?.contactTestBitMask = ColliderType.Enemy |
ColliderType.Boat
let missileFlightTime = travelTime(to: missileDestination, from:
player.position, atSpeed: missileSpeed)
missile.zRotation = direction(to: missileDestination, from:
missile.position)
self.addChild(missile)
let missileMove = SKAction.move(to: missileDestination,
duration:
TimeInterval(missileFlightTime))
let missileRemove = SKAction.removeFromParent()
missile.run(SKAction.sequence([missileMove, missileRemove]))
}
func travelTime(to target : CGPoint, from : CGPoint, atSpeed speed :
CGFloat) -> TimeInterval {
let distance = sqrt(pow(abs(target.x - from.x),2) +
pow(abs(target.y - from.y),2))
return TimeInterval(distance/speed)
}
func direction(to target : CGPoint, from: CGPoint) -> CGFloat {
let x = target.x - from.x
let y = target.y - from.y
var angle = atan(y / x)
if x < 0 {
angle = angle + CGFloat.pi
}
return angle
}
Upvotes: 0
Views: 172
Reputation: 8134
The direction
Function has the following signature:
func direction(to target : CGPoint, from: CGPoint) -> CGFloat
Which means it takes 2 parameters which are CGPoints and it returns a CGFloat. The first parameter has an external name of ‘to’ and an internal name of ‘target’. The second parameter has one parameter name, ‘from’.
Let’s assume that you want to calculate the direction from the ship’s position (ship.position
) to the missile’s target (missileDestination
). You would call the direction
function as follows:
Let dir = direction(to: misileDestination, from: ship.position)
As you start typing direction(
, Xcode fills in the method signature because it’s trying to help you. This is more useful if there were multiple signatures (we could have a direction
function that just assumes the start position is the ship and so only has ‘to’ as a parameter. Then, if you started typing directionn(
, XCode would offer both method signatures to save you having to remember them).
The internal/external parameter names thing it to make your code more readable. You use external names when calling a function - travelTime(to: onePlace, from: anotherPlace, atSpeed 500)
reads just like English. The internal names make the function code more readable - instead of using to
, from
and atSpeed
as variable names, ( which, to be honest, wouldn’t be that bad), it uses target
, from
and speed
.
P.S. If you want that direction
function that assumes the starting point is the ship, this is how you’d do it:
func direction(to target: CGPoint) -> CGFloat {
return direction(to: target, from: ship.direction)
}
Simply write another function with the same name but with a different signature. (Other languages do this differently, with multiple function signâtes for the same block of code), in this case, we then simply call the first direction
function, specifying the from:
parameter as the ship’s position.
Upvotes: 1