コードが長くて1ページに収まらないので2度投稿させてください。このコードを応用してセレクト画面を作っています。(https://github.com/burczyk/SwiftTeamSelect/blob/master/SwiftTeamSelect/GameScene.swift)ビルドもできスタートボタンも機能しスクロールも上手く行っているのですがホーム画面に戻ろうとするとスクロールの画像達がホーム画面に現れてしまいます。そして再びスタートボタンを押すとクラッシュしてしまいます。どうしたらよいですか?また、ゲームを作るにあたってこれからも質問していきたいのですがソースコードをこのように公開してしまって大丈夫でしょうか?
import SpriteKit class GameScene: SKScene { enum Zone { case Left, Center, Right } var SelectWorldBTN = [SKSpriteNode]() var leftSelectWorldBTN: SKSpriteNode? var centerSelectWorldBTN: SKSpriteNode? var rightSelectWorldBTN: SKSpriteNode? var leftGuide : CGFloat { return round(view!.bounds.width / 3.0) } var rightGuide : CGFloat { return view!.bounds.width - leftGuide } var gap : CGFloat { return (size.width / 2 - leftGuide) / 2 } var SelectWorld = SKSpriteNode() var startBTN = SKSpriteNode() var backhomeBTN = SKSpriteNode() var HomeBackGround = SKSpriteNode() override func didMove(to view: SKView) { createHome() } func createHome(){ HomeBackGround = SKSpriteNode(imageNamed: "HomeBackGround") HomeBackGround.size = CGSize(width: self.frame.width, height: self.frame.height) HomeBackGround.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) self.addChild(HomeBackGround) createstartBTN() } func createstartBTN(){ startBTN = SKSpriteNode(imageNamed: "startBTN") startBTN.position = CGPoint(x: self.frame.width/4, y: self.frame.height/2) startBTN.size = CGSize(width: self.frame.width/10, height: self.frame.height/5) self.addChild(startBTN) } func SelectWorldField(){ SelectWorld = SKSpriteNode(imageNamed: "SelectWorld") SelectWorld.size = CGSize(width: self.frame.width, height: self.frame.height) SelectWorld.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) self.addChild(SelectWorld) createbackhomeBTN() placeSelectWorldBTNOnPositions() calculateZIndexesForPlayers() } func createbackhomeBTN(){ backhomeBTN = SKSpriteNode(imageNamed: "backhomeBTN") backhomeBTN.position = CGPoint(x: self.frame.width/10, y: self.frame.height/15*14) backhomeBTN.size = CGSize(width: self.frame.width/8, height: self.frame.width/8) self.addChild(backhomeBTN) } override init(size: CGSize) { super.init(size:size) createSelectWorldBTN() centerSelectWorldBTN = SelectWorldBTN[SelectWorldBTN.count/2] setLeftAndRightSelectWorldBTN() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } func createSelectWorldBTN() { for i in 0..<9 { let player = SKSpriteNode(imageNamed: "SelectWorldBTN\(i)") player.size = CGSize(width: self.frame.width/8, height: self.frame.width/2) SelectWorldBTN.append(player) } } func placeSelectWorldBTNOnPositions() { for i in 0..<SelectWorldBTN.count/2 { SelectWorldBTN[i].position = CGPoint(x: leftGuide, y: self.frame.height/4*3) } SelectWorldBTN[SelectWorldBTN.count/2].position = CGPoint(x: self.frame.width/2, y: self.frame.height/4*3) for i in SelectWorldBTN.count/2 + 1..<SelectWorldBTN.count { SelectWorldBTN[i].position = CGPoint(x: rightGuide, y: self.frame.height/4*3) } for player in SelectWorldBTN { player.setScale(calculateScaleForX(x: player.position.x)) self.addChild(player) } } func calculateScaleForX(x:CGFloat) -> CGFloat { let minScale = CGFloat(0.5) if x <= leftGuide || x >= rightGuide { return minScale } if x < size.width/2 { let a = 1.0 / (size.width - 2 * leftGuide) let b = 0.5 - a * leftGuide return (a * x + b) } let a = 1.0 / (frame.size.width - 2 * rightGuide) let b = 0.5 - a * rightGuide return (a * x + b) } func calculateZIndexesForPlayers() { var playerCenterIndex : Int = SelectWorldBTN.count / 2 for i in 0..<SelectWorldBTN.count { if centerSelectWorldBTN == SelectWorldBTN[i] { playerCenterIndex = i } } for i in 0...playerCenterIndex { SelectWorldBTN[i].zPosition = CGFloat(i) } for i in playerCenterIndex+1..<SelectWorldBTN.count { SelectWorldBTN[i].zPosition = centerSelectWorldBTN!.zPosition * 2 - CGFloat(i) } } func movePlayerToX(player: SKSpriteNode, x: CGFloat, duration: TimeInterval) { let moveAction = SKAction.moveTo(x: x, duration: duration) let scaleAction = SKAction.scale(to: calculateScaleForX(x: x), duration: duration) player.run(SKAction.group([moveAction, scaleAction])) } func movePlayerByX(player: SKSpriteNode, x: CGFloat) { let duration = 0.01 if player.frame.midX <= rightGuide && player.frame.midX >= leftGuide { player.run(SKAction.moveBy(x: x, y: 0, duration: duration), completion: { player.setScale(self.calculateScaleForX(x: player.frame.midX)) }) if player.frame.midX < leftGuide { player.position = CGPoint(x: leftGuide, y: player.position.y) } else if player.frame.midX > rightGuide { player.position = CGPoint(x: rightGuide, y: player.position.y) } } } func zoneOfCenterSelectWorldBTN() -> Zone { let gap = size.width / 2 - leftGuide switch centerSelectWorldBTN!.frame.midX { case let x where x < leftGuide + gap/2: return .Left case let x where x > rightGuide - gap/2: return .Right default: return .Center } } func setLeftAndRightSelectWorldBTN() { var playerCenterIndex : Int = SelectWorldBTN.count / 2 for i in 0..<SelectWorldBTN.count { if centerSelectWorldBTN == SelectWorldBTN[i] { playerCenterIndex = i } } if playerCenterIndex > 0 && playerCenterIndex < SelectWorldBTN.count { leftSelectWorldBTN = SelectWorldBTN[playerCenterIndex-1] } else { leftSelectWorldBTN = nil } if playerCenterIndex > -1 && playerCenterIndex < SelectWorldBTN.count-1 { rightSelectWorldBTN = SelectWorldBTN[playerCenterIndex+1] } else { rightSelectWorldBTN = nil } } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches{ let location = touch.location(in: self) if startBTN.contains(location){ SelectWorldField() } if backhomeBTN.contains(location){ createHome() } } }
回答1件
あなたの回答
tips
プレビュー