2016-10-04 43 views
3

我想知道如何在Swift 3中使用Selector,包括func要求的括号中的值。我可以在Swift中使用带括号的选择器吗?

let fireRecogniser = UISwipeGestureRecognizer(target: self, action: Selector(("shootShot"))) 

^即识别器我有但该方法“shootShot”具有用于Element的参数其是enum,我有。

这里是 'shootShot' 功能:

func shootShot(type: Element) { 

    let shot = SKSpriteNode(imageNamed: "\(type)Shot") 
    shot.texture?.filteringMode = SKTextureFilteringMode.nearest 

    shot.position = CGPoint(x: -self.frame.width/2 /*playerframe*/, y: -(self.frame.height/2) + grnd.frame.height) 
    shot.setScale(1) 

    shot.physicsBody = SKPhysicsBody(circleOfRadius: (shot.frame.height/2)) 
    shot.physicsBody?.affectedByGravity = false 
    shot.physicsBody?.allowsRotation = true 
    shot.physicsBody?.isDynamic = true 
    shot.physicsBody?.restitution = 0 
    shot.physicsBody?.angularDamping = 0 
    shot.physicsBody?.linearDamping = 0 
    shot.physicsBody?.friction = 0 
    shot.physicsBody?.categoryBitMask = Contact.Shot.rawValue 
    shot.physicsBody?.contactTestBitMask = Contact.Enemy.rawValue 

    self.addChild(shot) 

    // THIS WILL DEPEND ON DIFFICULTY 
    let spin = SKAction.rotate(byAngle: 1, duration: 0.3) 
    shot.run(SKAction.repeatForever(spin)) 
    let move = SKAction.moveTo(x: self.frame.width/2, duration: 3.0) 
    let remove = SKAction.removeFromParent() 
    shot.run(SKAction.sequence([move, remove])) 
} 

正如你所看到的,该方法具有Element的功能需要。

有关如何将该参数包含在我的Selector中的帮助? 谢谢。 :)

回答

2

输入您选择这样的斯威夫特3

let fireRecogniser = UISwipeGestureRecognizer(target: self, action: #selector(shootShot(element:))) 
+0

更新和固定:) – pedrouan

1

这是可能的...如果你正在执行的选择之一。

有具有with:参数的perform()过载。您在with:参数中传递的参数将传递给选择器方法。

实施例:

// in some NSObject subclass's method 
perform(#selector(myMethod), with: "Hello") 

// in the same class 
func myMethod(x: String) { 
    print(x) 
} 

如果执行第一行, “你好” 将被打印。

然而,侑情况下,因为你不是选择的演员,你不能用你想要的参数进行选择。你传递目标和行动手势之前

var shotElement: Element! 

您可以将其设置为某个值:

您可以通过添加一个类级别的变量,表示要调用该方法,其Element解决此识别器。

然后访问它在shootShot

let shot = SKSpriteNode(imageNamed: "\(shotElement)Shot") 

我承认这不是完美的解决办法,但它是最简单的。

相关问题