|
| 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('contra2', 'assets/pics/contra2.png'); |
| 7 | + game.load.image('bunny', 'assets/sprites/bunny.png'); |
| 8 | + game.load.image('tetrisblock1', 'assets/sprites/tetrisblock1.png'); |
| 9 | + game.load.image('tetrisblock2', 'assets/sprites/tetrisblock2.png'); |
| 10 | + game.load.image('tetrisblock3', 'assets/sprites/tetrisblock3.png'); |
| 11 | + |
| 12 | + // Load our physics data exported from PhysicsEditor |
| 13 | + game.load.physics('physicsData', 'assets/physics/sprites.json'); |
| 14 | + |
| 15 | +} |
| 16 | + |
| 17 | +var contra; |
| 18 | +var bunny; |
| 19 | +var tetris1; |
| 20 | +var tetris2; |
| 21 | +var tetris3; |
| 22 | + |
| 23 | +var start = false; |
| 24 | + |
| 25 | +function create() { |
| 26 | + |
| 27 | + // Enable p2 physics |
| 28 | + game.physics.startSystem(Phaser.Physics.P2JS); |
| 29 | + |
| 30 | + // Make things a bit more bouncey |
| 31 | + game.physics.p2.defaultRestitution = 0.8; |
| 32 | + |
| 33 | + contra = game.add.sprite(100, 200, 'contra2'); |
| 34 | + bunny = game.add.sprite(500, 250, 'bunny'); |
| 35 | + tetris1 = game.add.sprite(100, 400, 'tetrisblock1'); |
| 36 | + tetris2 = game.add.sprite(300, 450, 'tetrisblock2'); |
| 37 | + tetris3 = game.add.sprite(600, 450, 'tetrisblock3'); |
| 38 | + |
| 39 | + // Enable the physics bodies on all the sprites and turn on the visual debugger |
| 40 | + game.physics.p2.enable([ contra, bunny, tetris1, tetris2, tetris3 ], true); |
| 41 | + |
| 42 | + // Clear the shapes and load the 'contra2' polygon from the physicsData JSON file in the cache |
| 43 | + contra.body.clearShapes(); |
| 44 | + contra.body.loadPolygon('physicsData', 'contra2'); |
| 45 | + |
| 46 | + bunny.body.clearShapes(); |
| 47 | + bunny.body.loadPolygon('physicsData', 'bunny'); |
| 48 | + |
| 49 | + tetris1.body.clearShapes(); |
| 50 | + tetris1.body.loadPolygon('physicsData', 'tetrisblock1'); |
| 51 | + |
| 52 | + tetris2.body.clearShapes(); |
| 53 | + tetris2.body.loadPolygon('physicsData', 'tetrisblock2'); |
| 54 | + |
| 55 | + tetris3.body.clearShapes(); |
| 56 | + tetris3.body.loadPolygon('physicsData', 'tetrisblock3'); |
| 57 | + |
| 58 | + // Just starts it rotating |
| 59 | + game.input.onDown.add(boom, this); |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +function boom() { |
| 64 | + |
| 65 | + if (game.input.activePointer.x > tetris1.x) |
| 66 | + { |
| 67 | + tetris1.body.rotateLeft(200); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + tetris1.body.rotateRight(200); |
| 72 | + } |
| 73 | + |
| 74 | + if (game.input.activePointer.y < tetris1.y) |
| 75 | + { |
| 76 | + tetris1.body.moveForward(400); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + tetris1.body.moveBackward(400); |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +function update() { |
| 86 | +} |
| 87 | + |
| 88 | +function render() { |
| 89 | +} |
0 commit comments