我正在创建一个游戏,并且正在尝试创建一种射击方法。我让子弹朝着枪产生,并朝操纵杆方向移动。这就是我用枪以一致的速率(射击速率)生成子弹并朝操纵杆方向移动的方式。
override func didMoveToView(view: SKView) {
if fireWeapon == true {
NSTimer.scheduledTimerWithTimeInterval(0.25, target: self,
selector: Selector ("spawnBullet1"), userInfo: nil, repeats: true)
}
}
func spawnBullet1(){
self.addChild(bullet1)
bullet1.position = CGPoint (x: hero.position.x , y:hero.position.y)
bullet1.xScale = 0.5
bullet1.yScale = 0.5
bullet1.physicsBody = SKPhysicsBody(rectangleOfSize: bullet1.size)
bullet1.physicsBody?.categoryBitMask = PhysicsCategory.bullet1
bullet1.physicsBody?.contactTestBitMask = PhysicsCategory.enemy1
bullet1.physicsBody?.affectedByGravity = false
bullet1.physicsBody?.dynamic = false
}
override func touchesBegan(touches: Set<UITouch>, withEvent
event:UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if (CGRectContainsPoint(joystick.frame, location)) {
stickActive = true
if stickActive == true {
fireWeapon = true
}
该方法仅启动第一个项目符号,然后在应用程序崩溃之前启动第二个项目符号。子弹移动的一切都完美无缺,只是产生了一堆子弹进行射击。我想不出另一种方法来提高射击速度。
您总是在拨打电话时尝试添加同一个孩子 self.addChild(bullet1)
您需要每次在spawnBullet1
函数中创建一个新实例,并将该对象添加为子对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句