【发布时间】:2016-04-27 01:29:20
【问题描述】:
我正在创建一个游戏,我正在尝试创建一种射击方法。我让子弹在枪上产生并朝着操纵杆的方向移动。这就是我如何用枪以一致的速度(射速)产生子弹并朝着操纵杆的方向移动。
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
}
此方法仅启动第一个项目符号,然后在启动第二个项目符号之前应用程序崩溃。子弹运动的一切都是完美的,只是产生一串子弹进行射击。我想不出另一种方法来创造火速。
【问题讨论】:
-
崩溃时收到的错误信息是什么?
-
另外,bullet1 变量是什么?
-
它说,类似。错误崩溃,尝试添加已经有父节点的 SkSpritenode”,这通常是当您调用相同的 self.addchild 两次 @mate Hegedus 时会发生的情况
标签: swift sprite-kit touch game-physics