-
Notifications
You must be signed in to change notification settings - Fork 0
/
enchant.js
6997 lines (6667 loc) · 220 KB
/
enchant.js
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
/**
* enchant.js v0.8.3
* http://enchantjs.com
*
* Copyright UEI Corporation
* Released under the MIT license.
*/
(function(window, undefined) {
// ECMA-262 5th edition Functions
if (typeof Object.defineProperty !== 'function') {
Object.defineProperty = function(obj, prop, desc) {
if ('value' in desc) {
obj[prop] = desc.value;
}
if ('get' in desc) {
obj.__defineGetter__(prop, desc.get);
}
if ('set' in desc) {
obj.__defineSetter__(prop, desc.set);
}
return obj;
};
}
if (typeof Object.defineProperties !== 'function') {
Object.defineProperties = function(obj, descs) {
for (var prop in descs) {
if (descs.hasOwnProperty(prop)) {
Object.defineProperty(obj, prop, descs[prop]);
}
}
return obj;
};
}
if (typeof Object.create !== 'function') {
Object.create = function(prototype, descs) {
function F() {
}
F.prototype = prototype;
var obj = new F();
if (descs != null) {
Object.defineProperties(obj, descs);
}
return obj;
};
}
if (typeof Object.getPrototypeOf !== 'function') {
Object.getPrototypeOf = function(obj) {
return obj.__proto__;
};
}
if (typeof Function.prototype.bind !== 'function') {
Function.prototype.bind = function(thisObject) {
var func = this;
var args = Array.prototype.slice.call(arguments, 1);
var Nop = function() {
};
var bound = function() {
var a = args.concat(Array.prototype.slice.call(arguments));
return func.apply(
this instanceof Nop ? this : thisObject || window, a);
};
Nop.prototype = func.prototype;
bound.prototype = new Nop();
return bound;
};
}
window.getTime = (function() {
var origin;
if (window.performance && window.performance.now) {
origin = Date.now();
return function() {
return origin + window.performance.now();
};
} else if (window.performance && window.performance.webkitNow) {
origin = Date.now();
return function() {
return origin + window.performance.webkitNow();
};
} else {
return Date.now;
}
}());
// define requestAnimationFrame
window.requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
(function() {
var lastTime = window.getTime();
var frame = 1000 / 60;
return function(func) {
var _id = setTimeout(function() {
lastTime = window.getTime();
func(lastTime);
}, Math.max(0, lastTime + frame - window.getTime()));
return _id;
};
}());
/**
* Export the library classes globally.
*
* When no arguments are given, all classes defined in enchant.js as well as all classes defined in
* plugins will be exported. When more than one argument is given, by default only classes defined
* in enchant.js will be exported. When you wish to export plugin classes you must explicitly deliver
* the plugin identifiers as arguments.
*
* @example
* enchant(); // All classes will be exported.
* enchant(''); // Only classes in enchant.js will be exported.
* enchant('ui'); // enchant.js classes and ui.enchant.js classes will be exported.
*
* @param {...String} [modules] Export module. Multiple designations possible.
* @function
* @global
* @name enchant
*/
var enchant = function(modules) {
if (modules != null) {
if (!(modules instanceof Array)) {
modules = Array.prototype.slice.call(arguments);
}
modules = modules.filter(function(module) {
return [module].join();
});
}
(function include(module, prefix) {
var submodules = [],
i, len;
for (var prop in module) {
if (module.hasOwnProperty(prop)) {
if (typeof module[prop] === 'function') {
window[prop] = module[prop];
} else if (typeof module[prop] === 'object' && module[prop] !== null && Object.getPrototypeOf(module[prop]) === Object.prototype) {
if (modules == null) {
submodules.push(prop);
} else {
i = modules.indexOf(prefix + prop);
if (i !== -1) {
submodules.push(prop);
modules.splice(i, 1);
}
}
}
}
}
for (i = 0, len = submodules.length; i < len; i++) {
include(module[submodules[i]], prefix + submodules[i] + '.');
}
}(enchant, ''));
// issue 185
if (enchant.Class.getInheritanceTree(window.Game).length <= enchant.Class.getInheritanceTree(window.Core).length) {
window.Game = window.Core;
}
if (modules != null && modules.length) {
throw new Error('Cannot load module: ' + modules.join(', '));
}
};
// export enchant
window.enchant = enchant;
window.addEventListener("message", function(msg, origin) {
try {
var data = JSON.parse(msg.data);
if (data.type === "event") {
enchant.Core.instance.dispatchEvent(new enchant.Event(data.value));
} else if (data.type === "debug") {
switch (data.value) {
case "start":
enchant.Core.instance.start();
break;
case "pause":
enchant.Core.instance.pause();
break;
case "resume":
enchant.Core.instance.resume();
break;
case "tick":
enchant.Core.instance._tick();
break;
default:
break;
}
}
} catch (e) {
// ignore
}
}, false);
/**
* @name enchant.Class
* @class
* A Class representing a class which supports inheritance.
* @param {Function} [superclass] The class from which the
* new class will inherit the class definition.
* @param {*} [definition] Class definition.
* @constructor
*/
enchant.Class = function(superclass, definition) {
return enchant.Class.create(superclass, definition);
};
/**
* Creates a class.
*
* When defining a class that extends from another class,
* the constructor of the other class will be used by default.
* Even if you override this constructor, you must still call it
* to ensure that the class is initialized correctly.
*
* @example
* // Creates a Ball class.
* var Ball = Class.create({
*
* // Ball's constructor
* initialize: function(radius) {
* // ... code ...
* },
*
* // Defines a fall method that doesn't take any arguments.
* fall: function() {
* // ... code ...
* }
* });
*
* // Creates a Ball class that extends from "Sprite"
* var Ball = Class.create(Sprite);
*
* // Creates a Ball class that extends from "Sprite"
* var Ball = Class.create(Sprite, {
*
* // Overwrite Sprite's constructor
* initialize: function(radius) {
*
* // Call Sprite's constructor.
* Sprite.call(this, radius * 2, radius * 2);
*
* this.image = core.assets['ball.gif'];
* }
* });
*
* @param {Function} [superclass] The class from which the
* new class will inherit the class definition.
* @param {*} [definition] Class definition.
* @static
*/
enchant.Class.create = function(superclass, definition) {
if (superclass == null && definition) {
throw new Error("superclass is undefined (enchant.Class.create)");
} else if (superclass == null) {
throw new Error("definition is undefined (enchant.Class.create)");
}
if (arguments.length === 0) {
return enchant.Class.create(Object, definition);
} else if (arguments.length === 1 && typeof arguments[0] !== 'function') {
return enchant.Class.create(Object, arguments[0]);
}
for (var prop in definition) {
if (definition.hasOwnProperty(prop)) {
if (typeof definition[prop] === 'object' && definition[prop] !== null && Object.getPrototypeOf(definition[prop]) === Object.prototype) {
if (!('enumerable' in definition[prop])) {
definition[prop].enumerable = true;
}
} else {
definition[prop] = { value: definition[prop], enumerable: true, writable: true };
}
}
}
var Constructor = function() {
if (this instanceof Constructor) {
Constructor.prototype.initialize.apply(this, arguments);
} else {
return new Constructor();
}
};
Constructor.prototype = Object.create(superclass.prototype, definition);
Constructor.prototype.constructor = Constructor;
if (Constructor.prototype.initialize == null) {
Constructor.prototype.initialize = function() {
superclass.apply(this, arguments);
};
}
var tree = this.getInheritanceTree(superclass);
for (var i = 0, l = tree.length; i < l; i++) {
if (typeof tree[i]._inherited === 'function') {
tree[i]._inherited(Constructor);
break;
}
}
return Constructor;
};
/**
* Get the inheritance tree of this class.
* @param {Function} Constructor
* @return {Function[]} Parent's constructor
*/
enchant.Class.getInheritanceTree = function(Constructor) {
var ret = [];
var C = Constructor;
var proto = C.prototype;
while (C !== Object) {
ret.push(C);
proto = Object.getPrototypeOf(proto);
C = proto.constructor;
}
return ret;
};
/**
* @namespace
* enchant.js environment variables.
* Execution settings can be changed by modifying these before calling new Core().
*/
enchant.ENV = {
/**
* Version of enchant.js
* @type String
*/
VERSION: '0.8.3',
/**
* Identifier of the current browser.
* @type String
*/
BROWSER: (function(ua) {
if (/Eagle/.test(ua)) {
return 'eagle';
} else if (/Opera/.test(ua)) {
return 'opera';
} else if (/MSIE|Trident/.test(ua)) {
return 'ie';
} else if (/Chrome/.test(ua)) {
return 'chrome';
} else if (/(?:Macintosh|Windows).*AppleWebKit/.test(ua)) {
return 'safari';
} else if (/(?:iPhone|iPad|iPod).*AppleWebKit/.test(ua)) {
return 'mobilesafari';
} else if (/Firefox/.test(ua)) {
return 'firefox';
} else if (/Android/.test(ua)) {
return 'android';
} else {
return '';
}
}(navigator.userAgent)),
/**
* The CSS vendor prefix of the current browser.
* @type String
*/
VENDOR_PREFIX: (function() {
var ua = navigator.userAgent;
if (ua.indexOf('Opera') !== -1) {
return 'O';
} else if (/MSIE|Trident/.test(ua)) {
return 'ms';
} else if (ua.indexOf('WebKit') !== -1) {
return 'webkit';
} else if (navigator.product === 'Gecko') {
return 'Moz';
} else {
return '';
}
}()),
/**
* Determines if the current browser supports touch.
* True, if touch is enabled.
* @type Boolean
*/
TOUCH_ENABLED: (function() {
var div = document.createElement('div');
div.setAttribute('ontouchstart', 'return');
return typeof div.ontouchstart === 'function';
}()),
/**
* Determines if the current browser is an iPhone with a retina display.
* True, if this display is a retina display.
* @type Boolean
*/
RETINA_DISPLAY: (function() {
if (navigator.userAgent.indexOf('iPhone') !== -1 && window.devicePixelRatio === 2) {
var viewport = document.querySelector('meta[name="viewport"]');
if (viewport == null) {
viewport = document.createElement('meta');
document.head.appendChild(viewport);
}
viewport.setAttribute('content', 'width=640');
return true;
} else {
return false;
}
}()),
/**
* Determines if for current browser Flash should be used to play
* sound instead of the native audio class.
* True, if flash should be used.
* @type Boolean
*/
USE_FLASH_SOUND: (function() {
var ua = navigator.userAgent;
var vendor = navigator.vendor || "";
// non-local access, not on mobile mobile device, not on safari
return (location.href.indexOf('http') === 0 && ua.indexOf('Mobile') === -1 && vendor.indexOf('Apple') !== -1);
}()),
/**
* If click/touch event occure for these tags the setPreventDefault() method will not be called.
* @type String[]
*/
USE_DEFAULT_EVENT_TAGS: ['input', 'textarea', 'select', 'area'],
/**
* Method names of CanvasRenderingContext2D that will be defined as Surface method.
* @type String[]
*/
CANVAS_DRAWING_METHODS: [
'putImageData', 'drawImage', 'drawFocusRing', 'fill', 'stroke',
'clearRect', 'fillRect', 'strokeRect', 'fillText', 'strokeText'
],
/**
* Keybind Table.
* You can use 'left', 'up', 'right', 'down' for preset event.
* @example
* enchant.ENV.KEY_BIND_TABLE = {
* 37: 'left',
* 38: 'up',
* 39: 'right',
* 40: 'down',
* 32: 'a', //-> use 'space' key as 'a button'
* };
* @type Object
*/
KEY_BIND_TABLE: {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
},
/**
* If keydown event occure for these keycodes the setPreventDefault() method will be called.
* @type Number[]
*/
PREVENT_DEFAULT_KEY_CODES: [37, 38, 39, 40],
/**
* Determines if Sound is enabled on Mobile Safari.
* @type Boolean
*/
SOUND_ENABLED_ON_MOBILE_SAFARI: true,
/**
* Determines if "touch to start" scene is enabled.
* It is necessary on Mobile Safari because WebAudio Sound is
* muted by browser until play any sound in touch event handler.
* If set it to false, you should control this behavior manually.
* @type Boolean
*/
USE_TOUCH_TO_START_SCENE: true,
/**
* Determines if WebAudioAPI is enabled. (true: use WebAudioAPI instead of Audio element if possible)
* @type Boolean
*/
USE_WEBAUDIO: (function() {
return location.protocol !== 'file:';
}()),
/**
* Determines if animation feature is enabled. (true: Timeline instance will be generated in new Node)
* @type Boolean
*/
USE_ANIMATION: true,
/**
* Specifies range of the touch detection.
* The detection area will be (COLOR_DETECTION_LEVEL * 2 + 1)px square.
* @type Boolean
*/
COLOR_DETECTION_LEVEL: 2
};
/**
* @scope enchant.Event.prototype
*/
enchant.Event = enchant.Class.create({
/**
* @name enchant.Event
* @class
* A class for an independent implementation of events similar to DOM Events.
* Does not include phase concepts.
* @param {String} type Event type.
* @constructs
*/
initialize: function(type) {
/**
* The type of the event.
* @type String
*/
this.type = type;
/**
* The target of the event.
* @type *
*/
this.target = null;
/**
* The x-coordinate of the event's occurrence.
* @type Number
*/
this.x = 0;
/**
* The y-coordinate of the event's occurrence.
* @type Number
*/
this.y = 0;
/**
* The x-coordinate of the event's occurrence relative to the object
* which issued the event.
* @type Number
*/
this.localX = 0;
/**
* The y-coordinate of the event's occurrence relative to the object
* which issued the event.
* @type Number
*/
this.localY = 0;
},
_initPosition: function(pageX, pageY) {
var core = enchant.Core.instance;
this.x = this.localX = (pageX - core._pageX) / core.scale;
this.y = this.localY = (pageY - core._pageY) / core.scale;
}
});
/**
* An event dispatched once the core has finished loading.
*
* When preloading images, it is necessary to wait until preloading is complete
* before starting the game.
* Issued by: {@link enchant.Core}
*
* @example
* var core = new Core(320, 320);
* core.preload('player.gif');
* core.onload = function() {
* ... // Describes initial core processing
* };
* core.start();
* @type String
*/
enchant.Event.LOAD = 'load';
/**
* An event dispatched when an error occurs.
* Issued by: {@link enchant.Core}, {@link enchant.Surface}, {@link enchant.WebAudioSound}, {@link enchant.DOMSound}
*/
enchant.Event.ERROR = 'error';
/**
* An event dispatched when the display size is changed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
@type String
*/
enchant.Event.CORE_RESIZE = 'coreresize';
/**
* An event dispatched while the core is loading.
* Dispatched each time an image is preloaded.
* Issued by: {@link enchant.LoadingScene}
* @type String
*/
enchant.Event.PROGRESS = 'progress';
/**
* An event which is occurring when a new frame is beeing processed.
* Issued object: {@link enchant.Core}, {@link enchant.Node}
* @type String
*/
enchant.Event.ENTER_FRAME = 'enterframe';
/**
* An event dispatched at the end of processing a new frame.
* Issued by: {@link enchant.Core}, {@link enchant.Node}
* @type String
*/
enchant.Event.EXIT_FRAME = 'exitframe';
/**
* An event dispatched when a Scene begins.
* Issued by: {@link enchant.Scene}
* @type String
*/
enchant.Event.ENTER = 'enter';
/**
* An event dispatched when a Scene ends.
* Issued by: {@link enchant.Scene}
* @type String
*/
enchant.Event.EXIT = 'exit';
/**
* An event dispatched when a Child is added to a Node.
* Issued by: {@link enchant.Group}, {@link enchant.Scene}
* @type String
*/
enchant.Event.CHILD_ADDED = 'childadded';
/**
* An event dispatched when a Node is added to a Group.
* Issued by: {@link enchant.Node}
* @type String
*/
enchant.Event.ADDED = 'added';
/**
* An event dispatched when a Node is added to a Scene.
* Issued by: {@link enchant.Node}
* @type String
*/
enchant.Event.ADDED_TO_SCENE = 'addedtoscene';
/**
* An event dispatched when a Child is removed from a Node.
* Issued by: {@link enchant.Group}, {@link enchant.Scene}
* @type String
* @type String
*/
enchant.Event.CHILD_REMOVED = 'childremoved';
/**
* An event dispatched when a Node is deleted from a Group.
* Issued by: {@link enchant.Node}
* @type String
*/
enchant.Event.REMOVED = 'removed';
/**
* An event dispatched when a Node is deleted from a Scene.
* Issued by: {@link enchant.Node}
* @type String
*/
enchant.Event.REMOVED_FROM_SCENE = 'removedfromscene';
/**
* An event dispatched when a touch event intersecting a Node begins.
* A mouse event counts as a touch event. Issued by: {@link enchant.Node}
* @type String
*/
enchant.Event.TOUCH_START = 'touchstart';
/**
* An event dispatched when a touch event intersecting the Node has been moved.
* A mouse event counts as a touch event. Issued by: {@link enchant.Node}
* @type String
*/
enchant.Event.TOUCH_MOVE = 'touchmove';
/**
* An event dispatched when a touch event intersecting the Node ends.
* A mouse event counts as a touch event. Issued by: {@link enchant.Node}
* @type String
*/
enchant.Event.TOUCH_END = 'touchend';
/**
* An event dispatched when an Entity is rendered.
* Issued by: {@link enchant.Entity}
* @type String
*/
enchant.Event.RENDER = 'render';
/**
* An event dispatched when a button is pressed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.INPUT_START = 'inputstart';
/**
* An event dispatched when button inputs change.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.INPUT_CHANGE = 'inputchange';
/**
* An event dispatched when button input ends.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.INPUT_END = 'inputend';
/**
* An internal event which is occurring when a input changes.
* Issued object: {@link enchant.InputSource}
* @type String
*/
enchant.Event.INPUT_STATE_CHANGED = 'inputstatechanged';
/**
* An event dispatched when the 'left' button is pressed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.LEFT_BUTTON_DOWN = 'leftbuttondown';
/**
* An event dispatched when the 'left' button is released.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.LEFT_BUTTON_UP = 'leftbuttonup';
/**
* An event dispatched when the 'right' button is pressed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.RIGHT_BUTTON_DOWN = 'rightbuttondown';
/**
* An event dispatched when the 'right' button is released.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.RIGHT_BUTTON_UP = 'rightbuttonup';
/**
* An event dispatched when the 'up' button is pressed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.UP_BUTTON_DOWN = 'upbuttondown';
/**
* An event dispatched when the 'up' button is released.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.UP_BUTTON_UP = 'upbuttonup';
/**
* An event dispatched when the 'down' button is pressed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.DOWN_BUTTON_DOWN = 'downbuttondown';
/**
* An event dispatched when the 'down' button is released.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.DOWN_BUTTON_UP = 'downbuttonup';
/**
* An event dispatched when the 'a' button is pressed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.A_BUTTON_DOWN = 'abuttondown';
/**
* An event dispatched when the 'a' button is released.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.A_BUTTON_UP = 'abuttonup';
/**
* An event dispatched when the 'b' button is pressed.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.B_BUTTON_DOWN = 'bbuttondown';
/**
* An event dispatched when the 'b' button is released.
* Issued by: {@link enchant.Core}, {@link enchant.Scene}
* @type String
*/
enchant.Event.B_BUTTON_UP = 'bbuttonup';
/**
* An event dispatched when an Action is added to a Timeline.
* When looped, an Action is removed from the Timeline and added back into it.
* @type String
*/
enchant.Event.ADDED_TO_TIMELINE = "addedtotimeline";
/**
* An event dispatched when an Action is removed from a Timeline.
* When looped, an Action is removed from the timeline and added back into it.
* @type String
*/
enchant.Event.REMOVED_FROM_TIMELINE = "removedfromtimeline";
/**
* An event dispatched when an Action begins.
* @type String
*/
enchant.Event.ACTION_START = "actionstart";
/**
* An event dispatched when an Action finishes.
* @type String
*/
enchant.Event.ACTION_END = "actionend";
/**
* An event dispatched when an Action has gone through one frame.
* @type String
*/
enchant.Event.ACTION_TICK = "actiontick";
/**
* An event dispatched to the Timeline when an Action is added.
* @type String
*/
enchant.Event.ACTION_ADDED = "actionadded";
/**
* An event dispatched to the Timeline when an Action is removed.
* @type String
*/
enchant.Event.ACTION_REMOVED = "actionremoved";
/**
* An event dispatched when an animation finishes, meaning null element was encountered
* Issued by: {@link enchant.Sprite}
* @type String
*/
enchant.Event.ANIMATION_END = "animationend";
/**
* @scope enchant.EventTarget.prototype
*/
enchant.EventTarget = enchant.Class.create({
/**
* @name enchant.EventTarget
* @class
* A class for implementation of events similar to DOM Events.
* However, it does not include the concept of phases.
* @constructs
*/
initialize: function() {
this._listeners = {};
},
/**
* Add a new event listener which will be executed when the event
* is dispatched.
* @param {String} type Type of the events.
* @param {Function(e:enchant.Event)} listener Event listener to be added.
*/
addEventListener: function(type, listener) {
var listeners = this._listeners[type];
if (listeners == null) {
this._listeners[type] = [listener];
} else if (listeners.indexOf(listener) === -1) {
listeners.unshift(listener);
}
},
/**
* Synonym for addEventListener.
* @param {String} type Type of the events.
* @param {Function(e:enchant.Event)} listener Event listener to be added.
* @see enchant.EventTarget#addEventListener
*/
on: function() {
this.addEventListener.apply(this, arguments);
},
/**
* Delete an event listener.
* @param {String} [type] Type of the events.
* @param {Function(e:enchant.Event)} listener Event listener to be deleted.
*/
removeEventListener: function(type, listener) {
var listeners = this._listeners[type];
if (listeners != null) {
var i = listeners.indexOf(listener);
if (i !== -1) {
listeners.splice(i, 1);
}
}
},
/**
* Clear all defined event listeners of a given type.
* If no type is given, all listeners will be removed.
* @param {String} type Type of the events.
*/
clearEventListener: function(type) {
if (type != null) {
delete this._listeners[type];
} else {
this._listeners = {};
}
},
/**
* Issue an event.
* @param {enchant.Event} e Event to be issued.
*/
dispatchEvent: function(e) {
e.target = this;
e.localX = e.x - this._offsetX;
e.localY = e.y - this._offsetY;
if (this['on' + e.type] != null){
this['on' + e.type](e);
}
var listeners = this._listeners[e.type];
if (listeners != null) {
listeners = listeners.slice();
for (var i = 0, len = listeners.length; i < len; i++) {
listeners[i].call(this, e);
}
}
}
});
(function() {
var core;
/**
* @scope enchant.Core.prototype
*/
enchant.Core = enchant.Class.create(enchant.EventTarget, {
/**
* @name enchant.Core
* @class
* A class for controlling the core’s main loop and scenes.
*
* There can be only one instance at a time. When the
* constructor is executed while an instance exists, the
* existing instance will be overwritten. The existing instance
* can be accessed from {@link enchant.Core.instance}.
*
* @param {Number} [width=320] The width of the core viewport.
* @param {Number} [height=320] The height of the core viewport.
* @constructs
* @extends enchant.EventTarget
*/
initialize: function(width, height) {
if (window.document.body === null) {
// @TODO postpone initialization after window.onload
throw new Error("document.body is null. Please excute 'new Core()' in window.onload.");
}
enchant.EventTarget.call(this);
var initial = true;
if (core) {
initial = false;
core.stop();
}
core = enchant.Core.instance = this;
this._calledTime = 0;
this._mousedownID = 0;
this._surfaceID = 0;
this._soundID = 0;
this._scenes = [];
width = width || 320;
height = height || 320;
var stage = document.getElementById('enchant-stage');
var scale, sWidth, sHeight;
if (!stage) {
stage = document.createElement('div');
stage.id = 'enchant-stage';
stage.style.position = 'absolute';
if (document.body.firstChild) {
document.body.insertBefore(stage, document.body.firstChild);
} else {
document.body.appendChild(stage);
}
scale = Math.min(
window.innerWidth / width,
window.innerHeight / height
);
this._pageX = stage.getBoundingClientRect().left;
this._pageY = stage.getBoundingClientRect().top;
} else {
var style = window.getComputedStyle(stage);
sWidth = parseInt(style.width, 10);
sHeight = parseInt(style.height, 10);
if (sWidth && sHeight) {
scale = Math.min(
sWidth / width,
sHeight / height
);
} else {