|
| 1 | + |
| 2 | +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); |
| 3 | + |
| 4 | +function preload() { |
| 5 | + |
| 6 | + game.load.image('chunk', 'assets/sprites/chunk.png'); |
| 7 | + game.load.image('arrow', 'assets/sprites/asteroids_ship.png'); |
| 8 | + game.load.image('p', 'assets/sprites/mushroom2.png'); |
| 9 | + game.load.image('ball', 'assets/sprites/shinyball.png'); |
| 10 | + |
| 11 | +} |
| 12 | + |
| 13 | +var sprite; |
| 14 | +var sprite2; |
| 15 | + |
| 16 | +var track; |
| 17 | +var bmd; |
| 18 | + |
| 19 | +function create() { |
| 20 | + |
| 21 | + game.stage.backgroundColor = '#124184'; |
| 22 | + |
| 23 | + bmd = game.add.bitmapData(800, 600); |
| 24 | + bmd.fillStyle('#ffffff'); |
| 25 | + var bg = game.add.sprite(0, 0, bmd); |
| 26 | + bg.body.moves = false; |
| 27 | + |
| 28 | + test8(); |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +// gravity into floor jiggle |
| 33 | +function test8() { |
| 34 | + |
| 35 | + game.physics.gravity.y = 150; |
| 36 | + |
| 37 | + sprite = game.add.sprite(400, 600-100, 'ball'); |
| 38 | + sprite.body.moves = false; |
| 39 | + sprite.body.collideWorldBounds = true; |
| 40 | + sprite.body.bounce.setTo(0.8, 0.8); |
| 41 | + |
| 42 | + sprite2 = game.add.sprite(500, 100, 'ball'); |
| 43 | + sprite2.body.collideWorldBounds = true; |
| 44 | + sprite2.body.bounce.setTo(0.5, 0.5); |
| 45 | + // sprite2.body.friction = 0.1; |
| 46 | + // sprite2.body.canSleep = true; |
| 47 | + |
| 48 | + track = sprite2; |
| 49 | + |
| 50 | + game.input.onDown.add(launch8, this); |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +function launch8() { |
| 55 | + |
| 56 | + sprite.body.velocity.y = -200; |
| 57 | + // sprite2.body.velocity.y = 200; |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +// both sprites are shot into each other vertically |
| 62 | +function test7() { |
| 63 | + |
| 64 | + // game.physics.gravity.y = 50; |
| 65 | + |
| 66 | + sprite = game.add.sprite(400, 600, 'ball'); |
| 67 | + sprite.body.collideWorldBounds = true; |
| 68 | + sprite.body.bounce.setTo(0.8, 0.8); |
| 69 | + |
| 70 | + sprite2 = game.add.sprite(400, 100, 'ball'); |
| 71 | + sprite2.body.collideWorldBounds = true; |
| 72 | + sprite2.body.bounce.setTo(0.8, 0.8); |
| 73 | + |
| 74 | + track = sprite2; |
| 75 | + |
| 76 | + game.input.onDown.add(launch7, this); |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +function launch7() { |
| 81 | + |
| 82 | + sprite.body.velocity.y = -200; |
| 83 | + // sprite2.body.velocity.y = 200; |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +// sprite is on the bottom, blocked by the world bounds, sprite2 falls onto it via gravity only |
| 89 | +function test6() { |
| 90 | + |
| 91 | + game.physics.gravity.y = 100; |
| 92 | + |
| 93 | + sprite = game.add.sprite(380, 600, 'p'); |
| 94 | + sprite.body.collideWorldBounds = true; |
| 95 | + |
| 96 | + sprite2 = game.add.sprite(400, 100, 'ball'); |
| 97 | + sprite2.body.collideWorldBounds = true; |
| 98 | + sprite2.body.bounce.setTo(0.8, 0.8); |
| 99 | + |
| 100 | + track = sprite2; |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +// sprite is on the bottom, blocked by the world bounds, sprite2 falls onto it |
| 105 | +function test5() { |
| 106 | + |
| 107 | + game.physics.gravity.y = 50; |
| 108 | + |
| 109 | + sprite = game.add.sprite(400, 600, 'ball'); |
| 110 | + sprite.body.collideWorldBounds = true; |
| 111 | + |
| 112 | + sprite2 = game.add.sprite(400, 100, 'ball'); |
| 113 | + sprite2.body.collideWorldBounds = true; |
| 114 | + sprite2.body.bounce.setTo(0.8, 0.8); |
| 115 | + |
| 116 | + track = sprite2; |
| 117 | + |
| 118 | + game.input.onDown.add(launch5, this); |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +function launch5() { |
| 123 | + |
| 124 | + sprite2.body.velocity.y = 200; |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +// bounce both sprites into each other, one with lots less velocity - checking newtons craddle approach |
| 130 | +function test4() { |
| 131 | + |
| 132 | + game.physics.gravity.y = 100; |
| 133 | + |
| 134 | + sprite = game.add.sprite(780, 400, 'ball'); |
| 135 | + sprite.body.collideWorldBounds = true; |
| 136 | + sprite.body.bounce.setTo(0.8, 0.8); |
| 137 | + |
| 138 | + sprite2 = game.add.sprite(0, 400, 'ball'); |
| 139 | + sprite2.body.collideWorldBounds = true; |
| 140 | + sprite2.body.bounce.setTo(0.8, 0.8); |
| 141 | + |
| 142 | + track = sprite2; |
| 143 | + |
| 144 | + game.input.onDown.add(launch4, this); |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +function launch4() { |
| 149 | + |
| 150 | + sprite.body.velocity.x = 70; |
| 151 | + sprite2.body.velocity.x = -250; |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +// bounce both sprites into each other, identical bounce + velocity |
| 156 | +function test3() { |
| 157 | + |
| 158 | + game.physics.gravity.y = 100; |
| 159 | + |
| 160 | + sprite = game.add.sprite(780, 400, 'p'); |
| 161 | + sprite.body.collideWorldBounds = true; |
| 162 | + sprite.body.bounce.setTo(0.8, 0.8); |
| 163 | + |
| 164 | + sprite2 = game.add.sprite(0, 400, 'p'); |
| 165 | + sprite2.body.collideWorldBounds = true; |
| 166 | + sprite2.body.bounce.setTo(0.8, 0.8); |
| 167 | + |
| 168 | + track = sprite2; |
| 169 | + |
| 170 | + game.input.onDown.add(launch3, this); |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +function launch3() { |
| 175 | + |
| 176 | + sprite.body.velocity.x = 225; |
| 177 | + sprite2.body.velocity.x = -225; |
| 178 | + |
| 179 | +} |
| 180 | + |
| 181 | +// sprite is on the right, blocked by the world bounds, sprite2 bounces into it |
| 182 | +function test2() { |
| 183 | + |
| 184 | + game.physics.gravity.y = 100; |
| 185 | + |
| 186 | + sprite = game.add.sprite(780, 400, 'p'); |
| 187 | + sprite.body.collideWorldBounds = true; |
| 188 | + |
| 189 | + sprite2 = game.add.sprite(0, 400, 'p'); |
| 190 | + sprite2.body.collideWorldBounds = true; |
| 191 | + sprite2.body.bounce.setTo(0.8, 0.8); |
| 192 | + |
| 193 | + track = sprite2; |
| 194 | + |
| 195 | + game.input.onDown.add(launch2, this); |
| 196 | + |
| 197 | +} |
| 198 | + |
| 199 | +function launch2() { |
| 200 | + |
| 201 | + sprite2.body.velocity.x = -225; |
| 202 | + |
| 203 | +} |
| 204 | + |
| 205 | +// sprite is on the left, blocked by the world bounds, sprite2 bounces into it |
| 206 | +function test1() { |
| 207 | + |
| 208 | + game.physics.gravity.y = 100; |
| 209 | + |
| 210 | + sprite = game.add.sprite(0, 400, 'p'); |
| 211 | + sprite.body.collideWorldBounds = true; |
| 212 | + |
| 213 | + sprite2 = game.add.sprite(700, 400, 'p'); |
| 214 | + sprite2.body.collideWorldBounds = true; |
| 215 | + sprite2.body.bounce.setTo(0.8, 0.8); |
| 216 | + |
| 217 | + track = sprite2; |
| 218 | + |
| 219 | + game.input.onDown.add(launch1, this); |
| 220 | + |
| 221 | +} |
| 222 | + |
| 223 | +function launch1() { |
| 224 | + |
| 225 | + sprite2.body.velocity.x = -225; |
| 226 | + |
| 227 | +} |
| 228 | + |
| 229 | +function update() { |
| 230 | + |
| 231 | + game.physics.collide(sprite, sprite2); |
| 232 | + |
| 233 | + // sprite.rotation = sprite.body.angle; |
| 234 | + |
| 235 | + bmd.fillStyle('#ffff00'); |
| 236 | + bmd.fillRect(track.body.center.x, track.body.center.y, 2, 2); |
| 237 | + |
| 238 | +} |
| 239 | + |
| 240 | +function render() { |
| 241 | + |
| 242 | + game.debug.renderBodyInfo(sprite, 16, 24); |
| 243 | + game.debug.renderBodyInfo(sprite2, 16, 190); |
| 244 | + |
| 245 | +} |
0 commit comments