Home Reference Source Repository

js/SpriteKit/SKScene.js

  1. 'use strict'
  2.  
  3. import CGPoint from '../CoreGraphics/CGPoint'
  4. import CGSize from '../CoreGraphics/CGSize'
  5. import SKColor from './SKColor'
  6. import SKEffectNode from './SKEffectNode'
  7. import SKSceneScaleMode from './SKSceneScaleMode'
  8. //import SKCameraNode from './SKCameraNode'
  9. //import SKView from './SKView'
  10. //import SKSceneDelegate from './SKSceneDelegate'
  11. //import SKPhysicsWorld from './SKPhysicsWorld'
  12. //import SKNode from './SKNode'
  13.  
  14.  
  15. /**
  16. * The root node for all Sprite Kit objects displayed in a view.
  17. * @access public
  18. * @extends {SKEffectNode}
  19. * @see https://developer.apple.com/documentation/spritekit/skscene
  20. */
  21. export default class SKScene extends SKEffectNode {
  22.  
  23. // Initializing a Scene
  24.  
  25. /**
  26. * Initializes a new scene object.
  27. * @access public
  28. * @constructor
  29. * @param {CGSize} size - The size of the scene in points.
  30. * @desc This is the class’s designated initializer method.
  31. * @see https://developer.apple.com/documentation/spritekit/skscene/1520435-init
  32. */
  33. constructor(size) {
  34. super()
  35.  
  36. // Determining What Portion of the Scene Is Visible in the View
  37.  
  38. /**
  39. * The camera node in the scene that determines what part of the scene’s coordinate space is visible in the view.
  40. * @type {?SKCameraNode}
  41. * @see https://developer.apple.com/documentation/spritekit/skscene/1519696-camera
  42. */
  43. this.camera = null
  44.  
  45. /**
  46. * The point in the view’s frame that corresponds to the scene’s origin.
  47. * @type {CGPoint}
  48. * @see https://developer.apple.com/documentation/spritekit/skscene/1519864-anchorpoint
  49. */
  50. this.anchorPoint = new CGPoint(0, 0)
  51.  
  52. /**
  53. * The dimensions of the scene in points.
  54. * @type {CGSize}
  55. * @see https://developer.apple.com/documentation/spritekit/skscene/1519831-size
  56. */
  57. this.size = size ? size : new CGSize(1, 1)
  58.  
  59. /**
  60. * Defines how the scene is mapped to the view that presents it.
  61. * @type {SKSceneScaleMode}
  62. * @see https://developer.apple.com/documentation/spritekit/skscene/1519562-scalemode
  63. */
  64. this.scaleMode = SKSceneScaleMode.fill
  65.  
  66.  
  67. // Setting the Background Color of a Scene
  68.  
  69. /**
  70. * The background color of the scene.
  71. * @type {SKColor}
  72. * @see https://developer.apple.com/documentation/spritekit/skscene/1520278-backgroundcolor
  73. */
  74. this.backgroundColor = new SKColor(0.15, 0.15, 0.15, 1.0)
  75.  
  76.  
  77. // Presenting a Scene
  78.  
  79. this._view = null
  80.  
  81. // Executing the Animation Loop
  82.  
  83. /**
  84. * A delegate to be called during the animation loop.
  85. * @type {?SKSceneDelegate}
  86. * @see https://developer.apple.com/documentation/spritekit/skscene/1520213-delegate
  87. */
  88. this.delegate = null
  89.  
  90.  
  91. // Working with Physics in the Scene
  92.  
  93. //this._physicsWorld = new SKPhysicsWorld()
  94. this._physicsWorld = null
  95.  
  96. // Working with Audio in the Scene
  97.  
  98. /**
  99. * A node used to determine the position of the listener for positional audio in the scene.
  100. * @type {?SKNode}
  101. * @see https://developer.apple.com/documentation/spritekit/skscene/1520363-listener
  102. */
  103. this.listener = null
  104.  
  105. //this._audioEngine = null
  106. }
  107.  
  108. _copyValue(src) {
  109. this.camera = src.camera
  110. this.anchorPoint = src.anchorPoint.copy()
  111. this.size = src.size.copy()
  112. this.scaleMode = src.scaleMode
  113. this.backgroundColor = src.backgroundColor._copy()
  114. this._view = src._view
  115. this.delegate = src.delegate
  116. this._physicsWorld = src._physicsWorld
  117. this.listener = src.listener
  118. }
  119.  
  120. // Determining What Portion of the Scene Is Visible in the View
  121.  
  122. /**
  123. * Called whenever the scene’s size changes.
  124. * @access public
  125. * @param {CGSize} oldSize - The old size of the scene, in points.
  126. * @returns {void}
  127. * @desc This method is intended to be overridden in a subclass. Typically, you use this method to adjust the positions of nodes in the scene.
  128. * @see https://developer.apple.com/documentation/spritekit/skscene/1519545-didchangesize
  129. */
  130. didChangeSize(oldSize) {
  131. }
  132.  
  133. // Converting Between View and Scene Coordinates
  134.  
  135. /**
  136. * Converts a point from view coordinates to scene coordinates.
  137. * @access public
  138. * @param {CGPoint} point - A point in view coordinates.
  139. * @returns {CGPoint} -
  140. * @desc The scene must be presented in a view before calling this method.
  141. * @see https://developer.apple.com/documentation/spritekit/skscene/1520395-convertpoint
  142. */
  143. convertPointFromView(point) {
  144. return null
  145. }
  146.  
  147. /**
  148. * Converts a point from scene coordinates to view coordinates.
  149. * @access public
  150. * @param {CGPoint} point - A point in scene coordinates.
  151. * @returns {CGPoint} -
  152. * @desc The scene must be presented in a view before calling this method.
  153. * @see https://developer.apple.com/documentation/spritekit/skscene/1520082-convertpoint
  154. */
  155. convertPointToView(point) {
  156. return null
  157. }
  158.  
  159. // Presenting a Scene
  160.  
  161. /**
  162. * Called immediately after the scene has been initialized or decoded.
  163. * @access public
  164. * @returns {void}
  165. * @desc This method is intended to be overridden in a subclass. You can use this method to implement any custom behavior after it has been initialized or decoded.
  166. * @see https://developer.apple.com/documentation/spritekit/skscene/1645216-scenedidload
  167. */
  168. sceneDidLoad() {
  169. }
  170.  
  171. /**
  172. * Called immediately before a scene is removed from a view.
  173. * @access public
  174. * @param {SKView} view - The view that is presenting the scene.
  175. * @returns {void}
  176. * @desc This method is intended to be overridden in a subclass. You can use this method to implement any custom behavior for your scene when it is about to be removed from the view.
  177. * @see https://developer.apple.com/documentation/spritekit/skscene/1519703-willmove
  178. */
  179. willMoveFrom(view) {
  180. }
  181.  
  182. /**
  183. * Called immediately after a scene is presented by a view.
  184. * @access public
  185. * @param {SKView} view - The view that is presenting the scene.
  186. * @returns {void}
  187. * @desc This method is intended to be overridden in a subclass. You can use this method to implement any custom behavior for your scene when it is about to be presented by a view. For example, you might use this method to create the scene’s contents.
  188. * @see https://developer.apple.com/documentation/spritekit/skscene/1519607-didmove
  189. */
  190. didMoveTo(view) {
  191. }
  192. /**
  193. * The view that is currently presenting the scene.
  194. * @type {?SKView}
  195. * @desc To present a scene, you call the presentScene(_:) method or presentScene(_:transition:) method on the SKView class. If the scene is not currently presented, this property holds nil.
  196. * @see https://developer.apple.com/documentation/spritekit/skscene/1519726-view
  197. */
  198. get view() {
  199. return this._view
  200. }
  201.  
  202. // Executing the Animation Loop
  203.  
  204. /**
  205. * Performs any scene-specific updates that need to occur before scene actions are evaluated.
  206. * @access public
  207. * @param {number} currentTime - The current system time.
  208. * @returns {void}
  209. * @desc Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.
  210. * @see https://developer.apple.com/documentation/spritekit/skscene/1519802-update
  211. */
  212. update(currentTime) {
  213. }
  214.  
  215. /**
  216. * Performs any scene-specific updates that need to occur after scene actions are evaluated.
  217. * @access public
  218. * @returns {void}
  219. * @desc Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.
  220. * @see https://developer.apple.com/documentation/spritekit/skscene/1519903-didevaluateactions
  221. */
  222. didEvaluateActions() {
  223. }
  224.  
  225. /**
  226. * Performs any scene-specific updates that need to occur after physics simulations are performed.
  227. * @access public
  228. * @returns {void}
  229. * @desc Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.
  230. * @see https://developer.apple.com/documentation/spritekit/skscene/1519965-didsimulatephysics
  231. */
  232. didSimulatePhysics() {
  233. }
  234.  
  235. /**
  236. * Performs any scene-specific updates that need to occur after constraints are applied.
  237. * @access public
  238. * @returns {void}
  239. * @desc Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.
  240. * @see https://developer.apple.com/documentation/spritekit/skscene/1520006-didapplyconstraints
  241. */
  242. didApplyConstraints() {
  243. }
  244.  
  245. /**
  246. * Called after the scene has finished all of the steps required to process animations.
  247. * @access public
  248. * @returns {void}
  249. * @desc Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene. This method is the last method to be called before the scene is rendered.
  250. * @see https://developer.apple.com/documentation/spritekit/skscene/1520269-didfinishupdate
  251. */
  252. didFinishUpdate() {
  253. }
  254.  
  255. // Working with Physics in the Scene
  256. /**
  257. * The physics simulation associated with the scene.
  258. * @type {SKPhysicsWorld}
  259. * @desc Every scene automatically creates a physics world object to simulate physics on nodes in the scene. You use this property to access the scene’s global physics properties, such as gravity. To add physics to a particular node, see physicsBody.
  260. * @see https://developer.apple.com/documentation/spritekit/skscene/1519584-physicsworld
  261. */
  262. get physicsWorld() {
  263. return this._physicsWorld
  264. }
  265.  
  266. // Working with Audio in the Scene
  267. /**
  268. * The AV Foundation audio engine used to play audio from audio nodes contained in the scene.
  269. * @type {AVAudioEngine}
  270. * @desc An audio engine instance is automatically created for you when the scene is created. You can use methods and properties on a scene’s audio engine for overall control of all of its child audio nodes. The following code shows how a scene’s overall volume can be reduced from its default of 1.0 down to 0.2 and then paused:let scene = SKScene()
  271. scene.audioEngine.mainMixerNode.outputVolume = 0.2
  272. scene.audioEngine.pause()
  273. let scene = SKScene()
  274. scene.audioEngine.mainMixerNode.outputVolume = 0.2
  275. scene.audioEngine.pause()
  276.  
  277. * @see https://developer.apple.com/documentation/spritekit/skscene/1519644-audioengine
  278. */
  279. //get audioEngine() {
  280. // return this._audioEngine
  281. //}
  282. }