プログラミング初心者です。SpriteKitでゲームを作っております。 モグラたたきゲームでタイマーのメソッドを使用し3種類のモグラをランダムに出現させていますが、現在、全ての穴からモグラが一斉に出てくる状態になっています。この出現するモグラの数をランダムにしたいです。どなたかご教示いただきたく、よろしくお願いいたします。 ```**該当のソースコード** import UIKit import SpriteKit import GameplayKit import AVFoundation class GameScene: SKScene,SKPhysicsContactDelegate { ///モグラの位置 var enemyPoint = [[150,250],[250,600],[200,800],[500,400],[550,150],[650,700],[550,950],[300,1100]] ///モグラを入れる配列 var enemy01Array:[SKSpriteNode] = [] var enemy02Array:[SKSpriteNode] = [] var enemy03Array:[SKSpriteNode] = [] //タイマー var enemyTimer = Timer() override func didMove(to view: SKView) { //もぐらの穴 for i in 0...7{ let hole = SKSpriteNode(imageNamed:"hole.png") hole.position = CGPoint(x:enemyPoint[i][0],y:enemyPoint[i][1]) self.addChild(hole) } //タイマーの設定 enemyTimer = Timer.scheduledTimer( timeInterval: 3.5, target:self, selector:#selector(GameScene.randomLoop), userInfo:nil,repeats:true) } //モグラをランダムに表示 @objc func randomLoop(){ for i in 0...7{ let random = Int(arc4random_uniform(20)) let enemy01 = SKSpriteNode(imageNamed: "enemy01.png") enemy01.position = CGPoint(x:enemyPoint[i][0],y:enemyPoint[i][1]) enemy01.zPosition = 1 let enemy02 = SKSpriteNode(imageNamed: "enemy02.png") enemy02.position = CGPoint(x:enemyPoint[i][0],y:enemyPoint[i][1]) enemy02.zPosition = 1 let enemy03 = SKSpriteNode(imageNamed: "enemy03.png") enemy03.position = CGPoint(x:enemyPoint[i][0],y:enemyPoint[i][1]) enemy02.zPosition = 1 enemy01Array.append(enemy01) enemy02Array.append(enemy02) enemy03Array.append(enemy03) enemy01.name = "enemy01" enemy02.name = "enemy02" enemy03.name = "enemy03" if(random>0 && random<13){ self.addChild(enemy01) }else if(random>=13 && random<18){ self.addChild(enemy02) }else{ self.addChild(enemy03) } let action1 = SKAction.wait(forDuration: 0.8,withRange:2.0) let action2 = SKAction.hide() let action3 = SKAction.wait(forDuration: 0.5,withRange:2.0) let action4 = SKAction.unhide() let actionS = SKAction.sequence([action1,action2,action3,action4]) enemy01.run(actionS) enemy02.run(actionS) enemy03.run(actionS) //Nodeが追加され続けてしまっていたタイマー追加までに消える DispatchQueue.main.asyncAfter(deadline: .now() + 3.0){ let Node1 = self.childNode(withName: "enemy01") let Node2 = self.childNode(withName: "enemy02") let Node3 = self.childNode(withName: "enemy03") Node1?.removeFromParent() Node2?.removeFromParent() Node3?.removeFromParent() } } } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch:AnyObject = touches.first{ let location = touch.location(in:self) let touchNode = self.atPoint(location) if let name = touchNode.name { if name == "enemy01" { let enemy01 = SKSpriteNode(imageNamed:"enemy01") enemy01.position = touchNode.position self.addChild(enemy01) //モグラを1回転するアクション let action1 = SKAction.rotate(byAngle: 6.28, duration: 0.2) let action2 = SKAction.moveTo(y: touchNode.position.y + 150,duration: 0.2) let actionG = SKAction.group([action1,action2]) let action3 = SKAction.removeFromParent() let actionS = SKAction.sequence([actionG,action3]) enemy01.run(actionS) touchNode.position.y = -100 }else if let name = touchNode.name { if name == "enemy02" { let enemy02 = SKSpriteNode(imageNamed:"enemy02") enemy02.position = touchNode.position self.addChild(enemy02) //モグラを1回転するアクション let action1 = SKAction.rotate(byAngle: 6.28, duration: 0.3) let action2 = SKAction.moveTo(y: touchNode.position.y + 150,duration: 0.3) let actionG = SKAction.group([action1,action2]) let action3 = SKAction.removeFromParent() let actionS = SKAction.sequence([actionG,action3]) enemy02.run(actionS) touchNode.position.y = -100 }else if let name = touchNode.name { if name == "enemy03" { let enemy03 = SKSpriteNode(imageNamed:"enemy03") enemy03.xScale = 0.6 enemy03.yScale = 0.6 enemy03.position = touchNode.position self.addChild(enemy03) //モグラを1回転するアクション let action1 = SKAction.rotate(byAngle: 6.28, duration: 0.3) let action2 = SKAction.moveTo(y: touchNode.position.y + 150,duration: 0.3) let actionG = SKAction.group([action1,action2]) let action3 = SKAction.removeFromParent() let actionS = SKAction.sequence([actionG,action3]) enemy03.run(actionS) touchNode.position.y = -100 } } } } } } override func update(_ currentTime: TimeInterval) { // Called before each frame is rendered } } ```**使っているツールのバージョンなど補足情報** swift3
あなたの回答
tips
プレビュー