/
FlxG.as
executable file
·1256 lines (1173 loc) · 38.7 KB
/
FlxG.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.flixel
{
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Stage;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import org.flixel.plugin.DebugPathDisplay;
import org.flixel.plugin.TimerManager;
import org.flixel.system.FlxDebugger;
import org.flixel.system.FlxQuadTree;
import org.flixel.system.input.*;
/**
* This is a global helper class full of useful functions for audio,
* input, basic info, and the camera system among other things.
* Utilities for maths and color and things can be found in <code>FlxU</code>.
* <code>FlxG</code> is specifically for Flixel-specific properties.
*
* @author Adam Atomic
*/
public class FlxG
{
/**
* If you build and maintain your own version of flixel,
* you can give it your own name here.
*/
static public var LIBRARY_NAME:String = "flixel";
/**
* Assign a major version to your library.
* Appears before the decimal in the console.
*/
static public var LIBRARY_MAJOR_VERSION:uint = 2;
/**
* Assign a minor version to your library.
* Appears after the decimal in the console.
*/
static public var LIBRARY_MINOR_VERSION:uint = 55;
/**
* Debugger overlay layout preset: Wide but low windows at the bottom of the screen.
*/
static public const DEBUGGER_STANDARD:uint = 0;
/**
* Debugger overlay layout preset: Tiny windows in the screen corners.
*/
static public const DEBUGGER_MICRO:uint = 1;
/**
* Debugger overlay layout preset: Large windows taking up bottom half of screen.
*/
static public const DEBUGGER_BIG:uint = 2;
/**
* Debugger overlay layout preset: Wide but low windows at the top of the screen.
*/
static public const DEBUGGER_TOP:uint = 3;
/**
* Debugger overlay layout preset: Large windows taking up left third of screen.
*/
static public const DEBUGGER_LEFT:uint = 4;
/**
* Debugger overlay layout preset: Large windows taking up right third of screen.
*/
static public const DEBUGGER_RIGHT:uint = 5;
/**
* Some handy color presets. Less glaring than pure RGB full values.
* Primarily used in the visual debugger mode for bounding box displays.
* Red is used to indicate an active, movable, solid object.
*/
static public const RED:uint = 0xffff0012;
/**
* Green is used to indicate solid but immovable objects.
*/
static public const GREEN:uint = 0xff00f225;
/**
* Blue is used to indicate non-solid objects.
*/
static public const BLUE:uint = 0xff0090e9;
/**
* Pink is used to indicate objects that are only partially solid, like one-way platforms.
*/
static public const PINK:uint = 0xfff01eff;
/**
* White... for white stuff.
*/
static public const WHITE:uint = 0xffffffff;
/**
* And black too.
*/
static public const BLACK:uint = 0xff000000;
/**
* Internal tracker for game object.
*/
static internal var _game:FlxGame;
/**
* Handy shared variable for implementing your own pause behavior.
*/
static public var paused:Boolean;
/**
* Whether you are running in Debug or Release mode.
* Set automatically by <code>FlxPreloader</code> during startup.
*/
static public var debug:Boolean;
/**
* Represents the amount of time in seconds that passed since last frame.
*/
static public var elapsed:Number;
/**
* How fast or slow time should pass in the game; default is 1.0.
*/
static public var timeScale:Number;
/**
* The width of the screen in game pixels.
*/
static public var width:uint;
/**
* The height of the screen in game pixels.
*/
static public var height:uint;
/**
* The dimensions of the game world, used by the quad tree for collisions and overlap checks.
*/
static public var worldBounds:FlxRect;
/**
* How many times the quad tree should divide the world on each axis.
* Generally, sparse collisions can have fewer divisons,
* while denser collision activity usually profits from more.
* Default value is 6.
*/
static public var worldDivisions:uint;
/**
* Whether to show visual debug displays or not.
* Default = false.
*/
static public var visualDebug:Boolean;
/**
* Setting this to true will disable/skip stuff that isn't necessary for mobile platforms like Android. [BETA]
*/
static public var mobile:Boolean;
/**
* The global random number generator seed (for deterministic behavior in recordings and saves).
*/
static public var globalSeed:Number;
/**
* <code>FlxG.levels</code> and <code>FlxG.scores</code> are generic
* global variables that can be used for various cross-state stuff.
*/
static public var levels:Array;
static public var level:int;
static public var scores:Array;
static public var score:int;
/**
* <code>FlxG.saves</code> is a generic bucket for storing
* FlxSaves so you can access them whenever you want.
*/
static public var saves:Array;
static public var save:int;
/**
* A reference to a <code>FlxMouse</code> object. Important for input!
*/
static public var mouse:Mouse;
/**
* A reference to a <code>FlxKeyboard</code> object. Important for input!
*/
static public var keys:Keyboard;
/**
* A handy container for a background music object.
*/
static public var music:FlxSound;
/**
* A list of all the sounds being played in the game.
*/
static public var sounds:FlxGroup;
/**
* Whether or not the game sounds are muted.
*/
static public var mute:Boolean;
/**
* Internal volume level, used for global sound control.
*/
static protected var _volume:Number;
/**
* An array of <code>FlxCamera</code> objects that are used to draw stuff.
* By default flixel creates one camera the size of the screen.
*/
static public var cameras:Array;
/**
* By default this just refers to the first entry in the cameras array
* declared above, but you can do what you like with it.
*/
static public var camera:FlxCamera;
/**
* Allows you to possibly slightly optimize the rendering process IF
* you are not doing any pre-processing in your game state's <code>draw()</code> call.
* @default false
*/
static public var useBufferLocking:Boolean;
/**
* Internal helper variable for clearing the cameras each frame.
*/
static protected var _cameraRect:Rectangle;
/**
* An array container for plugins.
* By default flixel uses a couple of plugins:
* DebugPathDisplay, and TimerManager.
*/
static public var plugins:Array;
/**
* Set this hook to get a callback whenever the volume changes.
* Function should take the form <code>myVolumeHandler(Volume:Number)</code>.
*/
static public var volumeHandler:Function;
/**
* Useful helper objects for doing Flash-specific rendering.
* Primarily used for "debug visuals" like drawing bounding boxes directly to the screen buffer.
*/
static public var flashGfxSprite:Sprite;
static public var flashGfx:Graphics;
/**
* Internal storage system to prevent graphics from being used repeatedly in memory.
*/
static protected var _cache:Object;
static public function getLibraryName():String
{
return FlxG.LIBRARY_NAME + " v" + FlxG.LIBRARY_MAJOR_VERSION + "." + FlxG.LIBRARY_MINOR_VERSION;
}
/**
* Log data to the debugger.
*
* @param Data Anything you want to log to the console.
*/
static public function log(Data:Object):void
{
if((_game != null) && (_game._debugger != null))
_game._debugger.log.add((Data == null)?"ERROR: null object":Data.toString());
}
/**
* Add a variable to the watch list in the debugger.
* This lets you see the value of the variable all the time.
*
* @param AnyObject A reference to any object in your game, e.g. Player or Robot or this.
* @param VariableName The name of the variable you want to watch, in quotes, as a string: e.g. "speed" or "health".
* @param DisplayName Optional, display your own string instead of the class name + variable name: e.g. "enemy count".
*/
static public function watch(AnyObject:Object,VariableName:String,DisplayName:String=null):void
{
if((_game != null) && (_game._debugger != null))
_game._debugger.watch.add(AnyObject,VariableName,DisplayName);
}
/**
* Remove a variable from the watch list in the debugger.
* Don't pass a Variable Name to remove all watched variables for the specified object.
*
* @param AnyObject A reference to any object in your game, e.g. Player or Robot or this.
* @param VariableName The name of the variable you want to watch, in quotes, as a string: e.g. "speed" or "health".
*/
static public function unwatch(AnyObject:Object,VariableName:String=null):void
{
if((_game != null) && (_game._debugger != null))
_game._debugger.watch.remove(AnyObject,VariableName);
}
/**
* How many times you want your game to update each second.
* More updates usually means better collisions and smoother motion.
* NOTE: This is NOT the same thing as the Flash Player framerate!
*/
static public function get framerate():Number
{
return 1000/_game._step;
}
/**
* @private
*/
static public function set framerate(Framerate:Number):void
{
_game._step = 1000/Framerate;
if(_game._maxAccumulation < _game._step)
_game._maxAccumulation = _game._step;
}
/**
* How many times you want your game to update each second.
* More updates usually means better collisions and smoother motion.
* NOTE: This is NOT the same thing as the Flash Player framerate!
*/
static public function get flashFramerate():Number
{
if(_game.root != null)
return _game.stage.frameRate;
else
return 0;
}
/**
* @private
*/
static public function set flashFramerate(Framerate:Number):void
{
_game._flashFramerate = Framerate;
if(_game.root != null)
_game.stage.frameRate = _game._flashFramerate;
_game._maxAccumulation = 2000/_game._flashFramerate - 1;
if(_game._maxAccumulation < _game._step)
_game._maxAccumulation = _game._step;
}
/**
* Generates a random number. Deterministic, meaning safe
* to use if you want to record replays in random environments.
*
* @return A <code>Number</code> between 0 and 1.
*/
static public function random():Number
{
return globalSeed = FlxU.srand(globalSeed);
}
/**
* Shuffles the entries in an array into a new random order.
* <code>FlxG.shuffle()</code> is deterministic and safe for use with replays/recordings.
* HOWEVER, <code>FlxU.shuffle()</code> is NOT deterministic and unsafe for use with replays/recordings.
*
* @param A A Flash <code>Array</code> object containing...stuff.
* @param HowManyTimes How many swaps to perform during the shuffle operation. Good rule of thumb is 2-4 times as many objects are in the list.
*
* @return The same Flash <code>Array</code> object that you passed in in the first place.
*/
static public function shuffle(Objects:Array,HowManyTimes:uint):Array
{
var i:uint = 0;
var index1:uint;
var index2:uint;
var object:Object;
while(i < HowManyTimes)
{
index1 = FlxG.random()*Objects.length;
index2 = FlxG.random()*Objects.length;
object = Objects[index2];
Objects[index2] = Objects[index1];
Objects[index1] = object;
i++;
}
return Objects;
}
/**
* Fetch a random entry from the given array.
* Will return null if random selection is missing, or array has no entries.
* <code>FlxG.getRandom()</code> is deterministic and safe for use with replays/recordings.
* HOWEVER, <code>FlxU.getRandom()</code> is NOT deterministic and unsafe for use with replays/recordings.
*
* @param Objects A Flash array of objects.
* @param StartIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
* @param Length Optional restriction on the number of values you want to randomly select from.
*
* @return The random object that was selected.
*/
static public function getRandom(Objects:Array,StartIndex:uint=0,Length:uint=0):Object
{
if(Objects != null)
{
var l:uint = Length;
if((l == 0) || (l > Objects.length - StartIndex))
l = Objects.length - StartIndex;
if(l > 0)
return Objects[StartIndex + uint(FlxG.random()*l)];
}
return null;
}
/**
* Load replay data from a string and play it back.
*
* @param Data The replay that you want to load.
* @param State Optional parameter: if you recorded a state-specific demo or cutscene, pass a new instance of that state here.
* @param CancelKeys Optional parameter: an array of string names of keys (see FlxKeyboard) that can be pressed to cancel the playback, e.g. ["ESCAPE","ENTER"]. Also accepts 2 custom key names: "ANY" and "MOUSE" (fairly self-explanatory I hope!).
* @param Timeout Optional parameter: set a time limit for the replay. CancelKeys will override this if pressed.
* @param Callback Optional parameter: if set, called when the replay finishes. Running to the end, CancelKeys, and Timeout will all trigger Callback(), but only once, and CancelKeys and Timeout will NOT call FlxG.stopReplay() if Callback is set!
*/
static public function loadReplay(Data:String,State:FlxState=null,CancelKeys:Array=null,Timeout:Number=0,Callback:Function=null):void
{
_game._replay.load(Data);
if(State == null)
FlxG.resetGame();
else
FlxG.switchState(State);
_game._replayCancelKeys = CancelKeys;
_game._replayTimer = Timeout*1000;
_game._replayCallback = Callback;
_game._replayRequested = true;
}
/**
* Resets the game or state and replay requested flag.
*
* @param StandardMode If true, reload entire game, else just reload current game state.
*/
static public function reloadReplay(StandardMode:Boolean=true):void
{
if(StandardMode)
FlxG.resetGame();
else
FlxG.resetState();
if(_game._replay.frameCount > 0)
_game._replayRequested = true;
}
/**
* Stops the current replay.
*/
static public function stopReplay():void
{
_game._replaying = false;
if(_game._debugger != null)
_game._debugger.vcr.stopped();
resetInput();
}
/**
* Resets the game or state and requests a new recording.
*
* @param StandardMode If true, reset the entire game, else just reset the current state.
*/
static public function recordReplay(StandardMode:Boolean=true):void
{
if(StandardMode)
FlxG.resetGame();
else
FlxG.resetState();
_game._recordingRequested = true;
}
/**
* Stop recording the current replay and return the replay data.
*
* @return The replay data in simple ASCII format (see <code>FlxReplay.save()</code>).
*/
static public function stopRecording():String
{
_game._recording = false;
if(_game._debugger != null)
_game._debugger.vcr.stopped();
return _game._replay.save();
}
/**
* Request a reset of the current game state.
*/
static public function resetState():void
{
_game._requestedState = new (FlxU.getClass(FlxU.getClassName(_game._state,false)))();
}
/**
* Like hitting the reset button on a game console, this will re-launch the game as if it just started.
*/
static public function resetGame():void
{
_game._requestedReset = true;
}
/**
* Reset the input helper objects (useful when changing screens or states)
*/
static public function resetInput():void
{
keys.reset();
mouse.reset();
}
/**
* Set up and play a looping background soundtrack.
*
* @param Music The sound file you want to loop in the background.
* @param Volume How loud the sound should be, from 0 to 1.
*/
static public function playMusic(Music:Class,Volume:Number=1.0):void
{
if(music == null)
music = new FlxSound();
else if(music.active)
music.stop();
music.loadEmbedded(Music,true);
music.volume = Volume;
music.survive = true;
music.play();
}
/**
* Creates a new sound object.
*
* @param EmbeddedSound The embedded sound resource you want to play. To stream, use the optional URL parameter instead.
* @param Volume How loud to play it (0 to 1).
* @param Looped Whether to loop this sound.
* @param AutoDestroy Whether to destroy this sound when it finishes playing. Leave this value set to "false" if you want to re-use this <code>FlxSound</code> instance.
* @param AutoPlay Whether to play the sound.
* @param URL Load a sound from an external web resource instead. Only used if EmbeddedSound = null.
*
* @return A <code>FlxSound</code> object.
*/
static public function loadSound(EmbeddedSound:Class=null,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=false,AutoPlay:Boolean=false,URL:String=null):FlxSound
{
if((EmbeddedSound == null) && (URL == null))
{
FlxG.log("WARNING: FlxG.loadSound() requires either\nan embedded sound or a URL to work.");
return null;
}
var sound:FlxSound = sounds.recycle(FlxSound) as FlxSound;
if(EmbeddedSound != null)
sound.loadEmbedded(EmbeddedSound,Looped,AutoDestroy);
else
sound.loadStream(URL,Looped,AutoDestroy);
sound.volume = Volume;
if(AutoPlay)
sound.play();
return sound;
}
/**
* Creates a new sound object from an embedded <code>Class</code> object.
* NOTE: Just calls FlxG.loadSound() with AutoPlay == true.
*
* @param EmbeddedSound The sound you want to play.
* @param Volume How loud to play it (0 to 1).
* @param Looped Whether to loop this sound.
* @param AutoDestroy Whether to destroy this sound when it finishes playing. Leave this value set to "false" if you want to re-use this <code>FlxSound</code> instance.
*
* @return A <code>FlxSound</code> object.
*/
static public function play(EmbeddedSound:Class,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=true):FlxSound
{
return FlxG.loadSound(EmbeddedSound,Volume,Looped,AutoDestroy,true);
}
/**
* Creates a new sound object from a URL.
* NOTE: Just calls FlxG.loadSound() with AutoPlay == true.
*
* @param URL The URL of the sound you want to play.
* @param Volume How loud to play it (0 to 1).
* @param Looped Whether or not to loop this sound.
* @param AutoDestroy Whether to destroy this sound when it finishes playing. Leave this value set to "false" if you want to re-use this <code>FlxSound</code> instance.
*
* @return A FlxSound object.
*/
static public function stream(URL:String,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=true):FlxSound
{
return FlxG.loadSound(null,Volume,Looped,AutoDestroy,true,URL);
}
/**
* Set <code>volume</code> to a number between 0 and 1 to change the global volume.
*
* @default 0.5
*/
static public function get volume():Number
{
return _volume;
}
/**
* @private
*/
static public function set volume(Volume:Number):void
{
_volume = Volume;
if(_volume < 0)
_volume = 0;
else if(_volume > 1)
_volume = 1;
if(volumeHandler != null)
volumeHandler(FlxG.mute?0:_volume);
}
/**
* Called by FlxGame on state changes to stop and destroy sounds.
*
* @param ForceDestroy Kill sounds even if they're flagged <code>survive</code>.
*/
static internal function destroySounds(ForceDestroy:Boolean=false):void
{
if((music != null) && (ForceDestroy || !music.survive))
{
music.destroy();
music = null;
}
var i:uint = 0;
var sound:FlxSound;
var l:uint = sounds.members.length;
while(i < l)
{
sound = sounds.members[i++] as FlxSound;
if((sound != null) && (ForceDestroy || !sound.survive))
sound.destroy();
}
}
/**
* Called by the game loop to make sure the sounds get updated each frame.
*/
static internal function updateSounds():void
{
if((music != null) && music.active)
music.update();
if((sounds != null) && sounds.active)
sounds.update();
}
/**
* Pause all sounds currently playing.
*/
static public function pauseSounds():void
{
if((music != null) && music.exists && music.active)
music.pause();
var i:uint = 0;
var sound:FlxSound;
var l:uint = sounds.length;
while(i < l)
{
sound = sounds.members[i++] as FlxSound;
if((sound != null) && sound.exists && sound.active)
sound.pause();
}
}
/**
* Resume playing existing sounds.
*/
static public function resumeSounds():void
{
if((music != null) && music.exists)
music.play();
var i:uint = 0;
var sound:FlxSound;
var l:uint = sounds.length;
while(i < l)
{
sound = sounds.members[i++] as FlxSound;
if((sound != null) && sound.exists)
sound.resume();
}
}
/**
* Check the local bitmap cache to see if a bitmap with this key has been loaded already.
*
* @param Key The string key identifying the bitmap.
*
* @return Whether or not this file can be found in the cache.
*/
static public function checkBitmapCache(Key:String):Boolean
{
return (_cache[Key] != undefined) && (_cache[Key] != null);
}
/**
* Generates a new <code>BitmapData</code> object (a colored square) and caches it.
*
* @param Width How wide the square should be.
* @param Height How high the square should be.
* @param Color What color the square should be (0xAARRGGBB)
* @param Unique Ensures that the bitmap data uses a new slot in the cache.
* @param Key Force the cache to use a specific Key to index the bitmap.
*
* @return The <code>BitmapData</code> we just created.
*/
static public function createBitmap(Width:uint, Height:uint, Color:uint, Unique:Boolean=false, Key:String=null):BitmapData
{
if(Key == null)
{
Key = Width+"x"+Height+":"+Color;
if(Unique && checkBitmapCache(Key))
{
var inc:uint = 0;
var ukey:String;
do
{
ukey = Key + inc++;
} while(checkBitmapCache(ukey));
Key = ukey;
}
}
if(!checkBitmapCache(Key))
_cache[Key] = new BitmapData(Width,Height,true,Color);
return _cache[Key];
}
/**
* Loads a bitmap from a file, caches it, and generates a horizontally flipped version if necessary.
*
* @param Graphic The image file that you want to load.
* @param Reverse Whether to generate a flipped version.
* @param Unique Ensures that the bitmap data uses a new slot in the cache.
* @param Key Force the cache to use a specific Key to index the bitmap.
*
* @return The <code>BitmapData</code> we just created.
*/
static public function addBitmap(Graphic:Class, Reverse:Boolean=false, Unique:Boolean=false, Key:String=null):BitmapData
{
var needReverse:Boolean = false;
if(Key == null)
{
Key = String(Graphic)+(Reverse?"_REVERSE_":"");
if(Unique && checkBitmapCache(Key))
{
var inc:uint = 0;
var ukey:String;
do
{
ukey = Key + inc++;
} while(checkBitmapCache(ukey));
Key = ukey;
}
}
//If there is no data for this key, generate the requested graphic
if(!checkBitmapCache(Key))
{
_cache[Key] = (new Graphic).bitmapData;
if(Reverse)
needReverse = true;
}
var pixels:BitmapData = _cache[Key];
if(!needReverse && Reverse && (pixels.width == (new Graphic).bitmapData.width))
needReverse = true;
if(needReverse)
{
var newPixels:BitmapData = new BitmapData(pixels.width<<1,pixels.height,true,0x00000000);
newPixels.draw(pixels);
var mtx:Matrix = new Matrix();
mtx.scale(-1,1);
mtx.translate(newPixels.width,0);
newPixels.draw(pixels,mtx);
pixels = newPixels;
_cache[Key] = pixels;
}
return pixels;
}
/**
* Dumps the cache's image references.
*/
static public function clearBitmapCache():void
{
_cache = new Object();
}
/**
* Read-only: retrieves the Flash stage object (required for event listeners)
* Will be null if it's not safe/useful yet.
*/
static public function get stage():Stage
{
if(_game.root != null)
return _game.stage;
return null;
}
/**
* Read-only: access the current game state from anywhere.
*/
static public function get state():FlxState
{
return _game._state;
}
/**
* Switch from the current game state to the one specified here.
*/
static public function switchState(State:FlxState):void
{
_game._requestedState = State;
}
/**
* Change the way the debugger's windows are laid out.
*
* @param Layout See the presets above (e.g. <code>DEBUGGER_MICRO</code>, etc).
*/
static public function setDebuggerLayout(Layout:uint):void
{
if(_game._debugger != null)
_game._debugger.setLayout(Layout);
}
/**
* Just resets the debugger windows to whatever the last selected layout was (<code>DEBUGGER_STANDARD</code> by default).
*/
static public function resetDebuggerLayout():void
{
if(_game._debugger != null)
_game._debugger.resetLayout();
}
/**
* Add a new camera object to the game.
* Handy for PiP, split-screen, etc.
*
* @param NewCamera The camera you want to add.
*
* @return This <code>FlxCamera</code> instance.
*/
static public function addCamera(NewCamera:FlxCamera):FlxCamera
{
FlxG._game.addChildAt(NewCamera._flashSprite,FlxG._game.getChildIndex(FlxG._game._mouse));
FlxG.cameras.push(NewCamera);
return NewCamera;
}
/**
* Remove a camera from the game.
*
* @param Camera The camera you want to remove.
* @param Destroy Whether to call destroy() on the camera, default value is true.
*/
static public function removeCamera(Camera:FlxCamera,Destroy:Boolean=true):void
{
try
{
FlxG._game.removeChild(Camera._flashSprite);
}
catch(E:Error)
{
FlxG.log("Error removing camera, not part of game.");
}
if(Destroy)
Camera.destroy();
}
/**
* Dumps all the current cameras and resets to just one camera.
* Handy for doing split-screen especially.
*
* @param NewCamera Optional; specify a specific camera object to be the new main camera.
*/
static public function resetCameras(NewCamera:FlxCamera=null):void
{
var cam:FlxCamera;
var i:uint = 0;
var l:uint = cameras.length;
while(i < l)
{
cam = FlxG.cameras[i++] as FlxCamera;
FlxG._game.removeChild(cam._flashSprite);
cam.destroy();
}
FlxG.cameras.length = 0;
if(NewCamera == null)
NewCamera = new FlxCamera(0,0,FlxG.width,FlxG.height)
FlxG.camera = FlxG.addCamera(NewCamera);
}
/**
* All screens are filled with this color and gradually return to normal.
*
* @param Color The color you want to use.
* @param Duration How long it takes for the flash to fade.
* @param OnComplete A function you want to run when the flash finishes.
* @param Force Force the effect to reset.
*/
static public function flash(Color:uint=0xffffffff, Duration:Number=1, OnComplete:Function=null, Force:Boolean=false):void
{
var i:uint = 0;
var l:uint = FlxG.cameras.length;
while(i < l)
(FlxG.cameras[i++] as FlxCamera).flash(Color,Duration,OnComplete,Force);
}
/**
* The screen is gradually filled with this color.
*
* @param Color The color you want to use.
* @param Duration How long it takes for the fade to finish.
* @param OnComplete A function you want to run when the fade finishes.
* @param Force Force the effect to reset.
*/
static public function fade(Color:uint=0xff000000, Duration:Number=1, OnComplete:Function=null, Force:Boolean=false):void
{
var i:uint = 0;
var l:uint = FlxG.cameras.length;
while(i < l)
(FlxG.cameras[i++] as FlxCamera).fade(Color,Duration,OnComplete,Force);
}
/**
* A simple screen-shake effect.
*
* @param Intensity Percentage of screen size representing the maximum distance that the screen can move while shaking.
* @param Duration The length in seconds that the shaking effect should last.
* @param OnComplete A function you want to run when the shake effect finishes.
* @param Force Force the effect to reset (default = true, unlike flash() and fade()!).
* @param Direction Whether to shake on both axes, just up and down, or just side to side (use class constants SHAKE_BOTH_AXES, SHAKE_VERTICAL_ONLY, or SHAKE_HORIZONTAL_ONLY). Default value is SHAKE_BOTH_AXES (0).
*/
static public function shake(Intensity:Number=0.05, Duration:Number=0.5, OnComplete:Function=null, Force:Boolean=true, Direction:uint=0):void
{
var i:uint = 0;
var l:uint = FlxG.cameras.length;
while(i < l)
(FlxG.cameras[i++] as FlxCamera).shake(Intensity,Duration,OnComplete,Force,Direction);
}
/**
* Get and set the background color of the game.
* Get functionality is equivalent to FlxG.camera.bgColor.
* Set functionality sets the background color of all the current cameras.
*/
static public function get bgColor():uint
{
if(FlxG.camera == null)
return 0xff000000;
else
return FlxG.camera.bgColor;
}
static public function set bgColor(Color:uint):void
{
var i:uint = 0;
var l:uint = FlxG.cameras.length;
while(i < l)
(FlxG.cameras[i++] as FlxCamera).bgColor = Color;
}
/**
* Call this function to see if one <code>FlxObject</code> overlaps another.
* Can be called with one object and one group, or two groups, or two objects,
* whatever floats your boat! For maximum performance try bundling a lot of objects
* together using a <code>FlxGroup</code> (or even bundling groups together!).
*
* <p>NOTE: does NOT take objects' scrollfactor into account, all overlaps are checked in world space.</p>
*
* @param ObjectOrGroup1 The first object or group you want to check.
* @param ObjectOrGroup2 The second object or group you want to check. If it is the same as the first, flixel knows to just do a comparison within that group.
* @param NotifyCallback A function with two <code>FlxObject</code> parameters - e.g. <code>myOverlapFunction(Object1:FlxObject,Object2:FlxObject)</code> - that is called if those two objects overlap.
* @param ProcessCallback A function with two <code>FlxObject</code> parameters - e.g. <code>myOverlapFunction(Object1:FlxObject,Object2:FlxObject)</code> - that is called if those two objects overlap. If a ProcessCallback is provided, then NotifyCallback will only be called if ProcessCallback returns true for those objects!
*
* @return Whether any oevrlaps were detected.
*/
static public function overlap(ObjectOrGroup1:FlxBasic=null,ObjectOrGroup2:FlxBasic=null,NotifyCallback:Function=null,ProcessCallback:Function=null):Boolean
{
if(ObjectOrGroup1 == null)
ObjectOrGroup1 = FlxG.state;
if(ObjectOrGroup2 === ObjectOrGroup1)
ObjectOrGroup2 = null;
FlxQuadTree.divisions = FlxG.worldDivisions;
var quadTree:FlxQuadTree = new FlxQuadTree(FlxG.worldBounds.x,FlxG.worldBounds.y,FlxG.worldBounds.width,FlxG.worldBounds.height);
quadTree.load(ObjectOrGroup1,ObjectOrGroup2,NotifyCallback,ProcessCallback);
var result:Boolean = quadTree.execute();
quadTree.destroy();
return result;
}
/**
* Call this function to see if one <code>FlxObject</code> collides with another.
* Can be called with one object and one group, or two groups, or two objects,
* whatever floats your boat! For maximum performance try bundling a lot of objects
* together using a <code>FlxGroup</code> (or even bundling groups together!).
*
* <p>This function just calls FlxG.overlap and presets the ProcessCallback parameter to FlxObject.separate.
* To create your own collision logic, write your own ProcessCallback and use FlxG.overlap to set it up.</p>
*
* <p>NOTE: does NOT take objects' scrollfactor into account, all overlaps are checked in world space.</p>
*
* @param ObjectOrGroup1 The first object or group you want to check.
* @param ObjectOrGroup2 The second object or group you want to check. If it is the same as the first, flixel knows to just do a comparison within that group.
* @param NotifyCallback A function with two <code>FlxObject</code> parameters - e.g. <code>myOverlapFunction(Object1:FlxObject,Object2:FlxObject)</code> - that is called if those two objects overlap.
*
* @return Whether any objects were successfully collided/separated.
*/
static public function collide(ObjectOrGroup1:FlxBasic=null, ObjectOrGroup2:FlxBasic=null, NotifyCallback:Function=null):Boolean
{
return overlap(ObjectOrGroup1,ObjectOrGroup2,NotifyCallback,FlxObject.separate);
}
/**
* Adds a new plugin to the global plugin array.
*
* @param Plugin Any object that extends FlxBasic. Useful for managers and other things. See org.flixel.plugin for some examples!
*
* @return The same <code>FlxBasic</code>-based plugin you passed in.
*/