我想用Swift
在SpriteKit中使用随机位置和方向来制作球我的代码在objective-c中有效但我正在尝试将其修改为在swift中工作
我使用此代码进行随机性
func randomInRange(low: CGFloat, high: CGFloat) -> CGFloat {
let value = CGFloat(arc4random_uniform(UINT32_MAX)) / CGFloat(UINT32_MAX)
return value * (high - low) + low
}
用于下降方法
// some constants for the angle and speed values that i am going to use when setting a ball's velocity:
var kCCHaloLowAngle : CGFloat = (200.0 * 3.14) / 180.0;
var kCCHaloHighAngle : CGFloat = (340.0 * 3.14) / 180.0;
var kCCHaloSpeed: CGFloat = 100.0;
func spawnHaloo ()->()
{
// Create ball node.
let ball : SKSpriteNode = SKSpriteNode(imageNamed:"ball")
halo.position = CGPointMake(randomInRange(ball.size.width * 0.5, high: self.size.width - (ball.size.width * 0.5)),
self.size.height + (ball.size.height * 0.5));
ball.physicsBody = SKPhysicsBody(circleOfRadius:ball.frame.size.width/2)
let direction : CGVector = radiansToVector(randomInRange(kCCHaloLowAngle, high: kCCHaloHighAngle));
ball.physicsBody?.velocity = CGVectorMake(direction.dx * kCCHaloSpeed, direction.dy * kCCHaloSpeed);
ball.physicsBody?.restitution = 1.0;
ball.physicsBody?.linearDamping = 0.0;
ball.physicsBody?.friction = 0.0;
}
override func didMoveToView(view: SKView) {
let action1 = (SKAction.waitForDuration(2.0, withRange: 1.0))
let action2 = SKAction.runBlock(spawnHaloo)
let spawnHalo : SKAction = SKAction.sequence([action1,action2])
let action3 = SKAction.repeatActionForever(spawnHalo)
self.runAction(action3)
}
在swift中,objective-c代码与此代码之间的差异是这一行
[SKAction performSelector:@selector(spawnHalo) onTarget:self]
而不是我使用
let action2 = SKAction.runBlock(spawnHaloo)
因为当我在swift中使用它时它会给我这个错误
Cannot invoke 'performSelector' with an argument list of type '(selector: (), onTarget: AnyObject.Protocol)'
也许这里有人可以提供帮助吗?