Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 应用程序运行后崩溃?_Swift_Sprite Kit - Fatal编程技术网

Swift 应用程序运行后崩溃?

Swift 应用程序运行后崩溃?,swift,sprite-kit,Swift,Sprite Kit,我想用弹跳球做一个游戏,我想每8秒产生一个球。目前,它在产卵后不会移动。我的问题是,在第一个球产生后,应用程序在接下来的5秒内崩溃。我不知道为什么会这样。我将scheduledTimerWithTimeInterval()放在touchesBegind函数中 func scheduledTimerWithTimeInterval(){ Enemytimer = Timer.scheduledTimer(timeInterval: 8, target:self, selector:#sel

我想用弹跳球做一个游戏,我想每8秒产生一个球。目前,它在产卵后不会移动。我的问题是,在第一个球产生后,应用程序在接下来的5秒内崩溃。我不知道为什么会这样。我将scheduledTimerWithTimeInterval()放在touchesBegind函数中

func scheduledTimerWithTimeInterval(){
    Enemytimer = Timer.scheduledTimer(timeInterval: 8, target:self, selector:#selector(GameScene.spawnEnemies), userInfo:nil, repeats:true)
}
func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat{
    return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}

func spawnEnemies(){
    let xPos = randomBetweenNumbers(firstNum: 0, secondNum:frame.width)
    let yPos = randomBetweenNumbers(firstNum: 0, secondNum: frame.height)
    enemies.size = CGSize(width: 20, height:20)
    enemies.color = UIColor(red:255.0,green:0.0,blue:0.0,alpha:1.0)
    enemies.colorBlendFactor = 1.0
    enemies.position = CGPoint(x: xPos, y:yPos)
    enemies.physicsBody?.affectedByGravity = false
    self.addChild(enemies)

您很可能遇到内存问题。我会将scheduledTimerWithTimeInterval方法移出touchStart。现在,每次用户触摸屏幕时,游戏都会启动一个新的计时器(不启动其他计时器)。将scheduledTimerWithTimeInterval移动到viewDidLoad或类似StartName的位置,以确保只调用一次。

请编辑您的问题,并向我们显示崩溃消息,以便我们提供更好的帮助。不要使用计时器,请使用内置的Spritekit函数,您可以在SOIt崩溃的地方找到如何执行此操作,可能是因为您试图将
敌人
节点添加到另一个父节点,而它已经有一个父节点(根据您的代码)旋风,您能指出它在哪里吗?我在哪里可以这样做?但我只希望在用户触摸屏幕时计时器开始。但是,现在加载应用程序后会创建一个新球,我希望在用户第一次触摸屏幕后创建新球。只是为了测试,我试着把它放到viewDidLoad中,但在创建新球大约5秒钟后,它仍然崩溃/