@@ -45,23 +45,101 @@ Phaser.Button = function (game, x, y, key, callback, callbackContext, overFrame,
4545 /**
4646 * The Phaser Object Type.
4747 * @property {number } type
48+ * @readonly
4849 */
4950 this . type = Phaser . BUTTON ;
5051
5152 /**
52- * The frames for each state - can be integers or strings when set. Indexed by `Button.STATE_*` .
53- * @property {Array< string|integer|null> } _stateFrames
53+ * The name or ID of the Over state frame .
54+ * @property {string|integer } onOverFrame
5455 * @private
5556 */
56- this . _stateFrames = [ null , null , null , null ] ;
57+ this . _onOverFrame = null ;
5758
5859 /**
59- * The sounds and markers for each state.
60- * Indexed by `Button.STATE_* << 1` for the sound and `+1` for the marker; expanded on-demand.
61- * @property {object[] } _stateSounds
60+ * The name or ID of the Out state frame.
61+ * @property {string|integer } onOutFrame
6262 * @private
6363 */
64- this . _stateSounds = [ ] ;
64+ this . _onOutFrame = null ;
65+
66+ /**
67+ * The name or ID of the Down state frame.
68+ * @property {string|integer } onDownFrame
69+ * @private
70+ */
71+ this . _onDownFrame = null ;
72+
73+ /**
74+ * The name or ID of the Up state frame.
75+ * @property {string|integer } onUpFrame
76+ * @private
77+ */
78+ this . _onUpFrame = null ;
79+
80+ /**
81+ * The Sound to be played when this Buttons Over state is activated.
82+ * @property {Phaser.Sound|Phaser.AudioSprite|null } onOverSound
83+ * @deprecated
84+ * @readonly
85+ */
86+ this . onOverSound = null ;
87+
88+ /**
89+ * The Sound to be played when this Buttons Out state is activated.
90+ * @property {Phaser.Sound|Phaser.AudioSprite|null } onOutSound
91+ * @deprecated
92+ * @readonly
93+ */
94+ this . onOutSound = null ;
95+
96+ /**
97+ * The Sound to be played when this Buttons Down state is activated.
98+ * @property {Phaser.Sound|Phaser.AudioSprite|null } onDownSound
99+ * @deprecated
100+ * @readonly
101+ */
102+ this . onDownSound = null ;
103+
104+ /**
105+ * The Sound to be played when this Buttons Up state is activated.
106+ * @property {Phaser.Sound|Phaser.AudioSprite|null } onUpSound
107+ * @deprecated
108+ * @readonly
109+ */
110+ this . onUpSound = null ;
111+
112+ /**
113+ * The Sound Marker used in conjunction with the onOverSound.
114+ * @property {string } onOverSoundMarker
115+ * @deprecated
116+ * @readonly
117+ */
118+ this . onOverSoundMarker = '' ;
119+
120+ /**
121+ * The Sound Marker used in conjunction with the onOutSound.
122+ * @property {string } onOutSoundMarker
123+ * @deprecated
124+ * @readonly
125+ */
126+ this . onOutSoundMarker = '' ;
127+
128+ /**
129+ * The Sound Marker used in conjunction with the onDownSound.
130+ * @property {string } onDownSoundMarker
131+ * @deprecated
132+ * @readonly
133+ */
134+ this . onDownSoundMarker = '' ;
135+
136+ /**
137+ * The Sound Marker used in conjunction with the onUpSound.
138+ * @property {string } onUpSoundMarker
139+ * @deprecated
140+ * @readonly
141+ */
142+ this . onUpSoundMarker = '' ;
65143
66144 /**
67145 * The Signal (or event) dispatched when this Button is in an Over state.
@@ -133,12 +211,12 @@ Phaser.Button = function (game, x, y, key, callback, callbackContext, overFrame,
133211Phaser . Button . prototype = Object . create ( Phaser . Image . prototype ) ;
134212Phaser . Button . prototype . constructor = Phaser . Button ;
135213
136- // State constants, useful to index into small state arrays .
137- // (Arranged by "expected likelines" of custom frames/sounds and may change - don't hard-code.)
138- Phaser . Button . STATE_OVER = 0 ;
139- Phaser . Button . STATE_DOWN = 1 ;
140- Phaser . Button . STATE_UP = 2 ;
141- Phaser . Button . STATE_OUT = 3 ;
214+ // State constants; local only. These are tied to property names in Phaser.Button .
215+ var STATE_OVER = 'Over' ;
216+ var STATE_OUT = 'Out' ;
217+ var STATE_DOWN = 'Down' ;
218+ var STATE_UP = 'Up' ;
219+
142220
143221/**
144222* Clears all of the frames set on this Button.
@@ -168,25 +246,27 @@ Phaser.Button.prototype.removedFromWorld = function () {
168246*
169247* @method Phaser.Button#setStateFrame
170248* @private
171- * @param {integer } state - See `Button. STATE_*`
249+ * @param {object } state - See `STATE_*`
172250* @param {number|string } frame - The number or string representing the frame.
173251* @param {boolean } switchImmediately - Immediately switch to the frame if it was set - and this is true.
174252*/
175253Phaser . Button . prototype . setStateFrame = function ( state , frame , switchImmediately )
176254{
177255
178- if ( frame != null ) // Anything but null/undefined
256+ var frameKey = '_on' + state + 'Frame' ;
257+
258+ if ( frame != null ) // not null or undefined
179259 {
180- this . _stateFrames [ state ] = frame ;
260+ this [ frameKey ] = frame ;
181261
182262 if ( switchImmediately )
183263 {
184- this . changeStateFrame ( frame ) ;
264+ this . changeStateFrame ( state ) ;
185265 }
186266 }
187267 else
188268 {
189- this . _stateFrames [ state ] = null ;
269+ this [ frameKey ] = null ;
190270 }
191271
192272} ;
@@ -196,7 +276,7 @@ Phaser.Button.prototype.setStateFrame = function (state, frame, switchImmediatel
196276*
197277* @method Phaser.Button#changeStateFrame
198278* @private
199- * @param {integer } state - See `Button. STATE_*`
279+ * @param {object } state - See `STATE_*`
200280* @return {boolean } True only if the frame was assigned a value, possibly the same one it already had.
201281*/
202282Phaser . Button . prototype . changeStateFrame = function ( state ) {
@@ -206,7 +286,9 @@ Phaser.Button.prototype.changeStateFrame = function (state) {
206286 return false ;
207287 }
208288
209- var frame = this . _stateFrames [ state ] ;
289+ var frameKey = '_on' + state + 'Frame' ;
290+ var frame = this [ frameKey ] ;
291+
210292 if ( typeof frame === 'string' )
211293 {
212294 this . frameName = frame ;
@@ -238,10 +320,10 @@ Phaser.Button.prototype.changeStateFrame = function (state) {
238320*/
239321Phaser . Button . prototype . setFrames = function ( overFrame , outFrame , downFrame , upFrame ) {
240322
241- this . setStateFrame ( Phaser . Button . STATE_OVER , overFrame , this . input . pointerOver ( ) ) ;
242- this . setStateFrame ( Phaser . Button . STATE_OUT , outFrame , ! this . input . pointerOver ( ) ) ;
243- this . setStateFrame ( Phaser . Button . STATE_DOWN , downFrame , this . input . pointerDown ( ) ) ;
244- this . setStateFrame ( Phaser . Button . STATE_UP , upFrame , this . input . pointerUp ( ) ) ;
323+ this . setStateFrame ( STATE_OVER , overFrame , this . input . pointerOver ( ) ) ;
324+ this . setStateFrame ( STATE_OUT , outFrame , ! this . input . pointerOver ( ) ) ;
325+ this . setStateFrame ( STATE_DOWN , downFrame , this . input . pointerDown ( ) ) ;
326+ this . setStateFrame ( STATE_UP , upFrame , this . input . pointerUp ( ) ) ;
245327
246328} ;
247329
@@ -250,30 +332,24 @@ Phaser.Button.prototype.setFrames = function (overFrame, outFrame, downFrame, up
250332*
251333* @method Phaser.Button#setStateSound
252334* @private
253- * @param {integer } state - See `Button. STATE_*`
335+ * @param {object } state - See `STATE_*`
254336* @param {Phaser.Sound|Phaser.AudioSprite } [sound] - Sound.
255- * @param {string } [marker=null ] - Sound marker.
337+ * @param {string } [marker='' ] - Sound marker.
256338*/
257339Phaser . Button . prototype . setStateSound = function ( state , sound , marker ) {
258340
259- var soundIndex = state << 1 ;
260- var markerIndex = soundIndex + 1 ;
261- var sounds = this . _stateSounds ;
341+ var soundKey = 'on' + state + 'Sound' ;
342+ var markerKey = 'on' + state + 'SoundMarker' ;
262343
263344 if ( sound instanceof Phaser . Sound || sound instanceof Phaser . AudioSprite )
264345 {
265- while ( markerIndex > sounds . length ) { // Dense null
266- sounds . push ( null ) ;
267- }
268-
269- sounds [ soundIndex ] = sound ;
270- sounds [ markerIndex ] = typeof marker === 'string' ? marker : null ;
346+ this [ soundKey ] = sound ;
347+ this [ markerKey ] = typeof marker === 'string' ? marker : '' ;
271348 }
272- else if ( markerIndex < sounds . length )
349+ else
273350 {
274- // Only null if set-through
275- sounds [ soundIndex ] = null ;
276- sounds [ markerIndex ] = null ;
351+ this [ soundKey ] = null ;
352+ this [ markerKey ] = '' ;
277353 }
278354
279355} ;
@@ -283,19 +359,19 @@ Phaser.Button.prototype.setStateSound = function (state, sound, marker) {
283359*
284360* @method Phaser.Button#playStateSound
285361* @private
286- * @param {integer } state - See `Button. STATE_*`
362+ * @param {object } state - See `STATE_*`
287363* @return {boolean } True only if a sound was played.
288364*/
289365Phaser . Button . prototype . playStateSound = function ( state ) {
290366
291- var soundIndex = state << 1 ;
292- var markerIndex = soundIndex + 1 ;
293- var sounds = this . _stateSounds ;
367+ var soundKey = 'on' + state + 'Sound' ;
368+ var sound = this [ soundKey ] ;
294369
295- var sound = sounds [ soundIndex ] ;
296370 if ( sound )
297371 {
298- var marker = sounds [ markerIndex ] ;
372+ var markerKey = 'on' + state + 'SoundMarker' ;
373+ var marker = this [ markerKey ] ;
374+
299375 sound . play ( marker ) ;
300376 return true ;
301377 }
@@ -325,10 +401,10 @@ Phaser.Button.prototype.playStateSound = function (state) {
325401*/
326402Phaser . Button . prototype . setSounds = function ( overSound , overMarker , downSound , downMarker , outSound , outMarker , upSound , upMarker ) {
327403
328- this . setStateSound ( Phaser . Button . STATE_OVER , overSound , overMarker ) ;
329- this . setStateSound ( Phaser . Button . STATE_OUT , outSound , outMarker ) ;
330- this . setStateSound ( Phaser . Button . STATE_DOWN , downSound , downMarker ) ;
331- this . setStateSound ( Phaser . Button . STATE_UP , upSound , upMarker ) ;
404+ this . setStateSound ( STATE_OVER , overSound , overMarker ) ;
405+ this . setStateSound ( STATE_OUT , outSound , outMarker ) ;
406+ this . setStateSound ( STATE_DOWN , downSound , downMarker ) ;
407+ this . setStateSound ( STATE_UP , upSound , upMarker ) ;
332408
333409} ;
334410
@@ -342,7 +418,7 @@ Phaser.Button.prototype.setSounds = function (overSound, overMarker, downSound,
342418*/
343419Phaser . Button . prototype . setOverSound = function ( sound , marker ) {
344420
345- this . setStateSound ( Phaser . Button . STATE_OVER , sound , marker ) ;
421+ this . setStateSound ( STATE_OVER , sound , marker ) ;
346422
347423} ;
348424
@@ -356,7 +432,7 @@ Phaser.Button.prototype.setOverSound = function (sound, marker) {
356432*/
357433Phaser . Button . prototype . setOutSound = function ( sound , marker ) {
358434
359- this . setStateSound ( Phaser . Button . STATE_OUT , sound , marker ) ;
435+ this . setStateSound ( STATE_OUT , sound , marker ) ;
360436
361437} ;
362438
@@ -370,7 +446,7 @@ Phaser.Button.prototype.setOutSound = function (sound, marker) {
370446*/
371447Phaser . Button . prototype . setDownSound = function ( sound , marker ) {
372448
373- this . setStateSound ( Phaser . Button . STATE_DOWN , sound , marker ) ;
449+ this . setStateSound ( STATE_DOWN , sound , marker ) ;
374450
375451} ;
376452
@@ -384,7 +460,7 @@ Phaser.Button.prototype.setDownSound = function (sound, marker) {
384460*/
385461Phaser . Button . prototype . setUpSound = function ( sound , marker ) {
386462
387- this . setStateSound ( Phaser . Button . STATE_UP , sound , marker ) ;
463+ this . setStateSound ( STATE_UP , sound , marker ) ;
388464
389465} ;
390466
@@ -404,14 +480,14 @@ Phaser.Button.prototype.onInputOverHandler = function (sprite, pointer) {
404480 return ;
405481 }
406482
407- this . changeStateFrame ( Phaser . Button . STATE_OVER ) ;
483+ this . changeStateFrame ( STATE_OVER ) ;
408484
409485 if ( this . onOverMouseOnly && ! pointer . isMouse )
410486 {
411487 return ;
412488 }
413489
414- this . playStateSound ( Phaser . Button . STATE_OVER ) ;
490+ this . playStateSound ( STATE_OVER ) ;
415491
416492 if ( this . onInputOver )
417493 {
@@ -430,9 +506,9 @@ Phaser.Button.prototype.onInputOverHandler = function (sprite, pointer) {
430506*/
431507Phaser . Button . prototype . onInputOutHandler = function ( sprite , pointer ) {
432508
433- this . changeStateFrame ( Phaser . Button . STATE_OUT ) ;
509+ this . changeStateFrame ( STATE_OUT ) ;
434510
435- this . playStateSound ( Phaser . Button . STATE_OUT ) ;
511+ this . playStateSound ( STATE_OUT ) ;
436512
437513 if ( this . onInputOut )
438514 {
@@ -450,9 +526,9 @@ Phaser.Button.prototype.onInputOutHandler = function (sprite, pointer) {
450526*/
451527Phaser . Button . prototype . onInputDownHandler = function ( sprite , pointer ) {
452528
453- this . changeStateFrame ( Phaser . Button . STATE_DOWN ) ;
529+ this . changeStateFrame ( STATE_DOWN ) ;
454530
455- this . playStateSound ( Phaser . Button . STATE_DOWN ) ;
531+ this . playStateSound ( STATE_DOWN ) ;
456532
457533 if ( this . onInputDown )
458534 {
@@ -470,7 +546,7 @@ Phaser.Button.prototype.onInputDownHandler = function (sprite, pointer) {
470546*/
471547Phaser . Button . prototype . onInputUpHandler = function ( sprite , pointer , isOver ) {
472548
473- this . playStateSound ( Phaser . Button . STATE_UP ) ;
549+ this . playStateSound ( STATE_UP ) ;
474550
475551 // Input dispatched early, before state change (but after sound)
476552 if ( this . onInputUp )
@@ -485,21 +561,21 @@ Phaser.Button.prototype.onInputUpHandler = function (sprite, pointer, isOver) {
485561
486562 if ( this . forceOut )
487563 {
488- this . changeStateFrame ( Phaser . Button . STATE_OUT ) ;
564+ this . changeStateFrame ( STATE_OUT ) ;
489565 }
490566 else
491567 {
492- var changedUp = this . changeStateFrame ( Phaser . Button . STATE_UP ) ;
568+ var changedUp = this . changeStateFrame ( STATE_UP ) ;
493569 if ( ! changedUp )
494570 {
495571 // No Up frame to show..
496572 if ( isOver )
497573 {
498- this . changeStateFrame ( Phaser . Button . STATE_OVER ) ;
574+ this . changeStateFrame ( STATE_OVER ) ;
499575 }
500576 else
501577 {
502- this . changeStateFrame ( Phaser . Button . STATE_OUT ) ;
578+ this . changeStateFrame ( STATE_OUT ) ;
503579 }
504580 }
505581 }
0 commit comments