前提・実現したいこと
ゲームを作成している時に
体力ゲージを視覚的に見せるコードを記載しています。
全てのコードが制限により表示させられていませんが現在HP40まで作成しています。
さらに
life41.fillColor = .yellow
life41.zPosition = など
とHP40ごとに色を変えていき同位置に
6列のHP240まで表示したいと思っています。
HPの記載の他、そのHPゲージを削除していく過程も
冗長的になってしまっていますので今のうちに見直したいのですが良い方法が思いつきません。
アドバイスいただけると助かります。
該当のソースコード
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var score = 0
var life = SKShapeNode()
var life2 = SKShapeNode()
var life3 = SKShapeNode()
var life4 = SKShapeNode()
var life5 = SKShapeNode()
var life6 = SKShapeNode()
var life7 = SKShapeNode()
var life8 = SKShapeNode()
var life9 = SKShapeNode()
var life10 = SKShapeNode()
var life11 = SKShapeNode()
var life12 = SKShapeNode()
var life13 = SKShapeNode()
var life14 = SKShapeNode()
var life15 = SKShapeNode()
var life16 = SKShapeNode()
var life17 = SKShapeNode()
var life18 = SKShapeNode()
var life19 = SKShapeNode()
var life20 = SKShapeNode()
override func didMove(to view: SKView) { 体力ゲージ()
}
func 体力ゲージ(){ let life = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life.fillColor = .orange life.position = CGPoint(x: size.width/60*11, y: size.height*0.9) addChild(life) let life2 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life2.fillColor = .orange life2.position = CGPoint(x: size.width/60*12, y: size.height*0.9) addChild(life2) let life3 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life3.fillColor = .orange life3.position = CGPoint(x: size.width/60*13, y: size.height*0.9) addChild(life3) let life4 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life4.fillColor = .orange life4.position = CGPoint(x: size.width/60*14, y: size.height*0.9) addChild(life4) let life5 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life5.fillColor = .orange life5.position = CGPoint(x: size.width/60*15, y: size.height*0.9) addChild(life5) let life6 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life6.fillColor = .orange life6.position = CGPoint(x: size.width/60*16, y: size.height*0.9) addChild(life6) let life7 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life7.fillColor = .orange life7.position = CGPoint(x: size.width/60*17, y: size.height*0.9) addChild(life7) let life8 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life8.fillColor = .orange life8.position = CGPoint(x: size.width/60*18, y: size.height*0.9) addChild(life8) let life9 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life9.fillColor = .orange life9.position = CGPoint(x: size.width/60*19, y: size.height*0.9) addChild(life9) let life10 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life10.fillColor = .orange life10.position = CGPoint(x: size.width/60*20, y: size.height*0.9) addChild(life10) let life11 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life11.fillColor = .orange life11.position = CGPoint(x: size.width/60*21, y: size.height*0.9) addChild(life11) let life12 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life12.fillColor = .orange life12.position = CGPoint(x: size.width/60*22, y: size.height*0.9) addChild(life12) let life13 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life13.fillColor = .orange life13.position = CGPoint(x: size.width/60*23, y: size.height*0.9) addChild(life13) let life14 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life14.fillColor = .orange life14.position = CGPoint(x: size.width/60*24, y: size.height*0.9) addChild(life14) let life15 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life15.fillColor = .orange life15.position = CGPoint(x: size.width/60*25, y: size.height*0.9) addChild(life15) let life16 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life16.fillColor = .orange life16.position = CGPoint(x: size.width/60*26, y: size.height*0.9) addChild(life16) let life17 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life17.fillColor = .orange life17.position = CGPoint(x: size.width/60*27, y: size.height*0.9) addChild(life17) let life18 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life18.fillColor = .orange life18.position = CGPoint(x: size.width/60*28, y: size.height*0.9) addChild(life18) let life19 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life19.fillColor = .orange life19.position = CGPoint(x: size.width/60*29, y: size.height*0.9) addChild(life19) let life20 = SKShapeNode(rectOf: CGSize(width: size.width/60, height: size.height/8)) life20.fillColor = .orange life20.position = CGPoint(x: size.width/60*30, y: size.height*0.9) addChild(life20) self.life = life self.life2 = life2 self.life3 = life3 self.life4 = life4 self.life5 = life5 self.life6 = life6 self.life7 = life7 self.life8 = life8 self.life9 = life9 self.life10 = life10 self.life11 = life11 self.life12 = life12 self.life13 = life13 self.life14 = life14 self.life15 = life15 self.life16 = life16 self.life17 = life17 self.life18 = life18 self.life19 = life19 self.life20 = life20 } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let rondom = Int.random(in: 1...4) score += rondom if score == 1{ life.removeFromParent() } if score == 2{ removeChildren(in: [life,life2]) } if score == 3{ removeChildren(in: [life,life2,life3]) } if score == 4{ removeChildren(in: [life,life2,life3,life4]) } if score == 5{ removeChildren(in: [life,life2,life3,life4,life5]) } if score == 6{ removeChildren(in: [life,life2,life3,life4,life5,life6]) } if score == 7{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7]) } if score == 8{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8]) } if score == 9{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9]) } if score == 10{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10]) } if score == 11{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11]) } if score == 12{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12]) } if score == 13{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13]) } if score == 14{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13,life14]) } if score == 15{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13,life14,life15]) } if score == 16{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13,life14,life15,life16]) } if score == 17{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13,life14,life15,life16,life17]) } if score == 18{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13,life14,life15,life16,life17,life18]) } if score == 19{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13,life14,life15,life16,life17,life18,life19]) } if score >= 20{ removeChildren(in: [life,life2,life3,life4,life5,life6,life7,life8,life9,life10,life11,life12,life13,life14,life15,life16,life17,life18,life19,life20]) } }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/09 10:51