1+ // mods by Patrick OReilly
2+ // Twitter: @pato_reilly Web: http://patricko.byethost9.com
3+
4+ var game = new Phaser . Game ( 800 , 600 , Phaser . CANVAS , 'phaser-example' , { preload : preload , create : create , update : update , render : render } ) ;
5+
6+ function preload ( ) {
7+
8+ game . load . spritesheet ( 'dude' , 'assets/games/starstruck/dude.png' , 32 , 48 ) ;
9+
10+ }
11+
12+ var flyer ;
13+
14+ function create ( ) {
15+
16+ cursors = game . input . keyboard . createCursorKeys ( ) ;
17+
18+ flyer = game . add . sprite ( 400 , 200 , 'dude' ) ;
19+
20+ flyer . animations . add ( 'left' , [ 0 , 1 , 2 , 3 ] , 10 , true ) ;
21+ flyer . animations . add ( 'right' , [ 5 , 6 , 7 , 8 ] , 10 , true ) ;
22+
23+ // This gets it moving
24+ flyer . body . velocity . setTo ( 200 , 200 ) ;
25+
26+ // This makes the game world bounce-able
27+ flyer . body . collideWorldBounds = true ;
28+
29+ // This sets the image bounce energy for the horizontal
30+ // and vertical vectors (as an x,y point). "1" is 100% energy return
31+ flyer . body . bounce . setTo ( 0.8 , 0.8 ) ;
32+
33+ // This sets the gravity the sprite responds to in the world, as a point
34+ // Leave x=0 and set y=8 to simulate falling
35+ flyer . body . gravity . setTo ( 0 , 8 ) ;
36+
37+ }
38+
39+ // Change the vertical and horizontal acceleration property accordingly with the key pressed
40+ // Also turn on and off the animation. Dude should have wings ;)
41+
42+ function update ( ) {
43+
44+ if ( cursors . up . isDown )
45+ {
46+ flyer . body . acceleration . y = - 600 ;
47+
48+ if ( flyer . body . velocity . x > 0 )
49+ {
50+ flyer . animations . play ( 'right' ) ;
51+ }
52+ else
53+ {
54+ flyer . animations . play ( 'left' ) ;
55+ }
56+ }
57+ else if ( cursors . down . isDown )
58+ {
59+ flyer . body . acceleration . y = 600 ;
60+
61+ if ( flyer . body . velocity . x > 0 )
62+ {
63+ flyer . animations . play ( 'right' ) ;
64+ }
65+ else
66+ {
67+ flyer . animations . play ( 'left' ) ;
68+ }
69+ }
70+ else if ( cursors . left . isDown )
71+ {
72+ flyer . body . acceleration . x = - 500 ;
73+ flyer . animations . play ( 'left' ) ;
74+ }
75+ else if ( cursors . right . isDown )
76+ {
77+ flyer . body . acceleration . x = 500 ;
78+ flyer . animations . play ( 'right' ) ;
79+ }
80+ else
81+ {
82+ flyer . frame = 4 ;
83+ flyer . body . acceleration . setTo ( 0 , 0 ) ;
84+ flyer . animations . stop ( ) ;
85+ }
86+
87+ }
88+
89+ function render ( ) {
90+
91+ //debug helper
92+ game . debug . renderSpriteInfo ( flyer , 32 , 32 ) ;
93+
94+ }
0 commit comments