前提・実現したいこと
プログラムを独学で勉強中です。インターネットのコードを参考に、
xcode Spritekit の背景にグラデーション色を取り入れたいです。
発生している問題・エラーメッセージ
CAGradientLayerにて背景色を反映させることには成功しましたが、
SKshapenodeで作成したシャボン玉が背景に隠れてしまいました。
プログラムの改修方法、ご教示願います。
該当のソースコード
import SpriteKit import GameplayKit import Foundation import UIKit class GameScene: SKScene { var circle = SKShapeNode(circleOfRadius: 100) // var circles = [SKShapeNode]() var location = CGPoint(x:0,y:0) var animator: UIDynamicAnimator? var i = 0 var targetedNode = SCNNode() var currentScale = SCNVector3() override func didMove(to view: SKView) { //グラデーションの開始色 let topColor = UIColor(red:0.07, green:0.13, blue:0.26, alpha:1) //グラデーションの開始色 let bottomColor = UIColor(red:0.54, green:0.74, blue:0.74, alpha:1) //グラデーションの色を配列で管理 let gradientColors: [CGColor] = [topColor.cgColor, bottomColor.cgColor] //グラデーションレイヤーを作成 let gradientLayer: CAGradientLayer = CAGradientLayer() //グラデーションの色をレイヤーに割り当てる gradientLayer.colors = gradientColors //グラデーションレイヤーをスクリーンサイズにする gradientLayer.frame = (self.view?.bounds)! //グラデーションレイヤーをビューの一番下に配置 self.view?.layer.insertSublayer(gradientLayer, at:0) self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) // self.backgroundColor = UIColor.black // ボタンを生成. let myButton = UIButton() myButton.frame = CGRect(x:0,y:0,width:200,height:40) myButton.backgroundColor = UIColor.red myButton.layer.masksToBounds = true myButton.setTitle("Add Block", for: []) myButton.setTitleColor(UIColor.white, for: []) myButton.setTitle("Done", for: UIControlState.highlighted) myButton.setTitleColor(UIColor.black, for: UIControlState.highlighted) myButton.layer.cornerRadius = 20.0 myButton.layer.position = CGPoint(x: self.view!.frame.width/2, y:200) myButton.addTarget(self, action: #selector(onClickMyButton(sender:)), for: UIControlEvents.touchUpInside) self.view?.addSubview(myButton) // let dogImage = SKTexture(imageNamed: "dog.jpg") // self.circle.fillTexture = dogImage // self.addChild(circle) } @objc func onClickMyButton(sender:UIButton) { animator?.removeAllBehaviors() // let location = touch.location(in: self) self.circle = SKShapeNode(circleOfRadius: 100) self.circle.physicsBody = SKPhysicsBody(circleOfRadius: 100) self.circle.fillColor = SKColor.white self.circle.position = location self.circle.physicsBody!.affectedByGravity = true self.circle.physicsBody?.restitution = 0.5 self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) self.addChild(circle) self.i = i+1 circle.name = "Circle(String(i))" circle.fillTexture = SKTexture(imageNamed: "Bubble.png") // 円にグローを効かせる(デフォルト値は0.0) circle.glowWidth = 5.0 } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for _ : AnyObject in touches { let location = touches.first!.location(in: self) let node : SKNode? = self.atPoint(location) print("Node Name: (String(describing: node?.name))") if node?.name != nil { circle = node as! SKShapeNode print("Node Name: (String(describing: node?.name))") } } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { for _ : AnyObject in touches { let location = touches.first!.location(in: self) let action = SKAction.move(to: CGPoint(x:location.x, y:location.y), duration:0) self.circle.physicsBody!.affectedByGravity = false circle.run(action) } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { let location = touches.first!.location(in: self) let node : SKNode? = self.atPoint(location) if node?.name != nil { animator?.removeAllBehaviors() self.circle.physicsBody!.affectedByGravity = true physicsWorld.gravity = CGVector(dx:0,dy:-7.0) } } override func update(_ currentTime: TimeInterval) { } }
あなたの回答
tips
プレビュー