gpt4 book ai didi

swift - 如何在按钮处于事件状态时重复生成 SKnode

转载 作者:搜寻专家 更新时间:2023-11-01 07:24:41 24 4
gpt4 key购买 nike

我正在创建一个游戏,我正在尝试创建一种射击方法。我让子弹在枪上产生并朝操纵杆的方向移动。这是我如何用枪以一致的速率(射速)产生子弹并朝操纵杆的方向移动。

 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)

时,您总是试图添加同一个 child

您需要在 spawnBullet1 函数中每次创建一个新实例,并将该对象添加为子对象。

关于swift - 如何在按钮处于事件状态时重复生成 SKnode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com