0
今天我的编码遇到了一些麻烦。试图制作一个“自动”武器,但无法让选择器正常工作。 下面是代码在xcode中使用选择器swift
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches){
let location = touch.location(in: self)func spawnBullets(){
let Bullet = SKSpriteNode(imageNamed: "circle")
Bullet.zPosition = -1
Bullet.position = CGPoint(x: ship.position.x,y: ship.position.y)
Bullet.size = CGSize(width: 30, height: 30)
Bullet.physicsBody = SKPhysicsBody(circleOfRadius: 15)
Bullet.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
Bullet.name = "Bullet"
Bullet.physicsBody?.isDynamic = true
Bullet.physicsBody?.affectedByGravity = false
self.addChild(Bullet)
var dx = CGFloat(location.x - base2.position.x)
var dy = CGFloat(location.y - base2.position.y)
let magnitude = sqrt(dx * dx + dy * dy)
dx /= magnitude
dy /= magnitude
let vector = CGVector(dx: 30.0 * dx, dy: 30.0 * dy)
Bullet.physicsBody?.applyImpulse(vector)
}
spawnBullets()
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector:#selector("spawnBullets"),userInfo: nil, repeats: true)
}
' 然而,当我运行它,我得到的是选择不引用任何错误。任何人都可以帮助我吗? 谢谢
目标/动作的选择器必须在类的顶层,“let location = ...'这一行是疯狂的。 – vadian
是的。但是当我复制代码时这只是一个错误 –