Ios 允许一个操作的多个实例
我试图在swift中创建一个“太空入侵者”游戏,当用户触摸屏幕时,一颗子弹从船上射出,但当子弹在屏幕上移动时,我再次尝试触摸它时,我有一个NSException,游戏中断。如何设置动作,以便可以有多个动作实例,从而使射手半自动。下面是我当前的场景控制器Ios 允许一个操作的多个实例,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我试图在swift中创建一个“太空入侵者”游戏,当用户触摸屏幕时,一颗子弹从船上射出,但当子弹在屏幕上移动时,我再次尝试触摸它时,我有一个NSException,游戏中断。如何设置动作,以便可以有多个动作实例,从而使射手半自动。下面是我当前的场景控制器 import SpriteKit class GameScene: SKScene { let background = SKSpriteNode(imageNamed: "background") let heroShip =
import SpriteKit
class GameScene: SKScene {
let background = SKSpriteNode(imageNamed: "background")
let heroShip = SKSpriteNode(imageNamed: "heroShip")
let bullet = SKSpriteNode(imageNamed: "bullet")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
heroShip.position = CGPointMake(self.size.width/6.0, self.size.height/2.0)
self.heroShip.zPosition = 1.0
self.addChild(background)
self.addChild(heroShip)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
bullet.position = CGPointMake(heroShip.position.x + bullet.size.width/2, heroShip.position.y)
let action = SKAction.moveToX(self.frame.width + self.bullet.size.width, duration: 0.5)
self.addChild(bullet)
bullet.runAction(action, completion: {
self.bullet.removeAllActions()
self.bullet.removeFromParent()
})
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
导入SpriteKit
类游戏场景:SKScene{
让background=SKSpriteNode(图像名为:“background”)
让heroShip=SKSpriteNode(图像名为“heroShip”)
让bullet=SKSpriteNode(图像名为:“bullet”)
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
background.position=CGPointMake(CGRectGetMidX(self.frame)、CGRectGetMidY(self.frame))
heroShip.position=CGPointMake(self.size.width/6.0,self.size.height/2.0)
self.heroShip.zPosition=1.0
self.addChild(背景)
self.addChild(heroShip)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
bullet.position=CGPointMake(heroShip.position.x+bullet.size.width/2,heroShip.position.y)
let action=SKAction.moveToX(self.frame.width+self.bullet.size.width,持续时间:0.5)
self.addChild(项目符号)
bullet.runAction(操作、完成:{
self.bullet.removeAllActions()
self.bullet.removeFromParent()
})
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
}
如果你想从一艘船上射出多颗子弹,你必须创建一颗子弹的多个实例。你现在拥有的是bullet
类的bullet
属性,这是一个错误。
您可能希望在iAction
因此,请尝试以下方法:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.position = CGPointMake(heroShip.position.x + bullet.size.width/2, heroShip.position.y)
let action = SKAction.moveToX(self.frame.width + bullet.size.width, duration: 0.5)
self.addChild(bullet)
bullet.runAction(action, completion: {
bullet.removeAllActions()
bullet.removeFromParent()
})
}
从类的顶部如果你想从一艘船上射出多个子弹,你必须创建一个子弹的多个实例。你现在拥有的是
bullet
类的bullet
属性,这是一个错误。
您可能希望在iAction
因此,请尝试以下方法:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.position = CGPointMake(heroShip.position.x + bullet.size.width/2, heroShip.position.y)
let action = SKAction.moveToX(self.frame.width + bullet.size.width, duration: 0.5)
self.addChild(bullet)
bullet.runAction(action, completion: {
bullet.removeAllActions()
bullet.removeFromParent()
})
}
如果你在子弹移动时触摸屏幕,你需要创建另一个子弹对吗?那么你想从一艘船上射出多个子弹?如果你在子弹移动时触摸屏幕,你需要创建另一个子弹对吗?那么你想从一艘船上射出多个子弹?是的,没错,我意识到在我发布这篇文章之后,再也没有新的bullet节点了。无论如何,谢谢你的帮助!是的,这是正确的,我意识到在我发布这篇文章之后,再也没有新的项目符号节点了。无论如何,谢谢你的帮助!