前提・実現したいこと
swiftでiphoneアプリ開発の勉強をしているんですが、下記のエラーが出て困っています。
発生している問題・エラーメッセージ
Use of unresolved identifier 'self' Use of unresolved identifier 'addChild' Use of unresolved identifier 'frame'
該当のソースコード
swift
1import SpriteKit 2import GameplayKit 3import CoreMotion 4 5 6class GameScene: SKScene { 7 8 let motionManager = CMMotionManager() 9 var accelaration: CGFloat = 0.0 10 11 var earth: SKSpriteNode! 12 var spaceship: SKSpriteNode! 13 14 override func didMove(to view: SKView) { 15 self.earth = SKSpriteNode(imageNamed: "earth") 16 self.earth.xScale = 1.5 17 self.earth.yScale = 0.3 18 self.earth.position = CGPoint(x: 0, y: -frame.height / 2) 19 self.earth.zPosition = -1.0 20 addChild(self.earth) 21 22 self.spaceship = SKSpriteNode(imageNamed: "spaceship") 23 self.spaceship.scale(to: CGSize(width: frame.width / 5, height: frame.width / 5)) 24 self.spaceship.position = CGPoint(x: 0, y: self.earth.frame.maxY + 50) 25 addChild(self.spaceship) 26 27 motionManager.accelerometerUpdateInterval = 0.2 28 motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, _) in 29 guard let data = data else { return } 30 let a = data.acceleration 31 self.accelaration = CGFloat(a.x) * 0.75 + self.accelaration * 0.25 32 } 33 } 34 35 override func didSimulatePhysics() { 36 let nextPosition = self.spaceship.position.x + self.accelaration * 50 37 if nextPosition > frame.width / 2 - 30 { return } 38 if nextPosition < -frame.width / 2 + 30 { return } 39 self.spaceship.position.x = nextPosition 40 } 41 } 42 43 func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 44 let missile = SKSpriteNode(imageNamed: "missile") 45 missile.position = CGPoint(x: self.spaceship.position.x, y: self.spaceship.position.y + 50) //Use of unresolved identifier 'self' エラーメッセージ 46 addChild(missile) // Use of unresolved identifier 'addChild' エラーメッセージ 47 48 let moveToTop = SKAction.moveTo(y: frame.height + 10, duration: 0.3) // Use of unresolved identifier 'frame' エラーメッセージ 49 let remove = SKAction.removeFromParent() 50 missile.run(SKAction.sequence([moveToTop, remove])) 51} 52
試したこと
let addChild = SKSpriteNode(imageNamed: "missile")
とりあえず変数として定義したほうが良いのかと思って、上記のように書いてみましたが、新たなエラーが出てしまいダメでした。
self.はviewcontrollerの中のクラスを参照したい時
frame、addchildは何か分かってません。
教えていただけたら幸いです。
回答1件
あなたの回答
tips
プレビュー