Skip to content

Commit abf26df

Browse files
committed
New P2 examples and fixed Body debug assignment parameter.
1 parent 386ee19 commit abf26df

14 files changed

Lines changed: 478 additions & 110 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.image('atari', 'assets/sprites/atari130xe.png');
7+
game.load.image('sky', 'assets/skies/sunset.png');
8+
9+
}
10+
11+
var sprite;
12+
var cursors;
13+
14+
function create() {
15+
16+
game.add.image(0, 0, 'sky');
17+
18+
// Enable p2 physics
19+
game.physics.startSystem(Phaser.Physics.P2JS);
20+
21+
// Make things a bit more bouncey
22+
game.physics.p2.defaultRestitution = 0.8;
23+
24+
// Add a sprite
25+
sprite = game.add.sprite(200, 200, 'atari');
26+
27+
// Enable if for physics. This creates a default rectangular body.
28+
game.physics.p2.enable(sprite);
29+
30+
// Modify a few body properties
31+
sprite.body.setZeroDamping();
32+
sprite.body.fixedRotation = true;
33+
34+
text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' });
35+
36+
cursors = game.input.keyboard.createCursorKeys();
37+
38+
}
39+
40+
function update() {
41+
42+
sprite.body.setZeroVelocity();
43+
44+
if (cursors.left.isDown)
45+
{
46+
sprite.body.moveLeft(400);
47+
}
48+
else if (cursors.right.isDown)
49+
{
50+
sprite.body.moveRight(400);
51+
}
52+
53+
if (cursors.up.isDown)
54+
{
55+
sprite.body.moveUp(400);
56+
}
57+
else if (cursors.down.isDown)
58+
{
59+
sprite.body.moveDown(400);
60+
}
61+
62+
}

examples/p2 physics/gravity.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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('arrow', 'assets/sprites/xenon2_ship.png');
7+
8+
}
9+
10+
var sprite;
11+
var bmd;
12+
13+
function create() {
14+
15+
// Enable p2 physics
16+
game.physics.startSystem(Phaser.Physics.P2JS);
17+
18+
game.stage.backgroundColor = '#124184';
19+
20+
bmd = game.add.bitmapData(800, 600);
21+
bmd.context.fillStyle = '#ffffff';
22+
23+
var bg = game.add.sprite(0, 0, bmd);
24+
25+
game.physics.p2.gravity.y = 100;
26+
game.physics.p2.defaultRestitution = 0.8;
27+
28+
sprite = game.add.sprite(32, 450, 'arrow');
29+
30+
game.physics.p2.enable(sprite);
31+
32+
sprite.body.fixedRotation = true;
33+
34+
text = game.add.text(20, 20, 'click to the left / right of the ship', { fill: '#ffffff', font: '14pt Arial' });
35+
36+
game.input.onDown.add(launch, this);
37+
38+
}
39+
40+
function launch() {
41+
42+
if (game.input.x < sprite.x)
43+
{
44+
sprite.body.velocity.x = -200;
45+
sprite.body.velocity.y = -200;
46+
}
47+
else
48+
{
49+
sprite.body.velocity.x = 200;
50+
sprite.body.velocity.y = -200;
51+
}
52+
53+
}
54+
55+
function update() {
56+
57+
bmd.context.fillStyle = '#ffff00';
58+
bmd.context.fillRect(sprite.x, sprite.y, 2, 2);
59+
60+
}
61+
62+
function render() {
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
8+
// Load our physics data exported from PhysicsEditor
9+
game.load.physics('physicsData', 'assets/physics/sprites.json');
10+
11+
}
12+
13+
var contra;
14+
var start = false;
15+
16+
function create() {
17+
18+
// Enable p2 physics
19+
game.physics.startSystem(Phaser.Physics.P2JS);
20+
21+
contra = game.add.sprite(400, 300, 'contra2');
22+
23+
// Enable the physics body on this sprite and turn on the visual debugger
24+
game.physics.p2.enable(contra, true);
25+
26+
// Clear the shapes and load the 'contra2' polygon from the physicsData JSON file in the cache
27+
contra.body.clearShapes();
28+
contra.body.loadPolygon('physicsData', 'contra2');
29+
30+
// Just starts it rotating
31+
game.input.onDown.add(function() { start = true; }, this);
32+
33+
}
34+
35+
function update() {
36+
37+
if (start)
38+
{
39+
contra.body.rotateLeft(5);
40+
}
41+
42+
}
43+
44+
function render() {
45+
46+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

examples/wip/ninja overlap.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
4+
5+
function preload() {
6+
7+
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
8+
game.load.image('diamond', 'assets/sprites/diamond.png');
9+
game.load.image('sky', 'assets/skies/cavern1.png');
10+
11+
}
12+
13+
var sprite1;
14+
var cursors;
15+
var diamonds;
16+
17+
function create() {
18+
19+
game.add.image(0, 0, 'sky');
20+
21+
game.physics.startSystem(Phaser.Physics.NINJA);
22+
23+
game.physics.ninja.gravity = 0;
24+
25+
sprite1 = game.add.sprite(500, 200, 'gameboy');
26+
game.physics.ninja.enableAABB(sprite1);
27+
28+
diamonds = game.add.group();
29+
diamonds.enableBody = true;
30+
31+
for (var i = 0; i < 20; i++)
32+
{
33+
var d = diamonds.create(game.world.randomX, game.world.randomY, 'diamond');
34+
}
35+
36+
cursors = game.input.keyboard.createCursorKeys();
37+
38+
}
39+
40+
function collisionHandler(s, d) {
41+
42+
d.kill();
43+
44+
}
45+
46+
function update() {
47+
48+
game.physics.arcade.overlap(sprite1, diamonds, collisionHandler, null, this);
49+
50+
if (cursors.left.isDown)
51+
{
52+
sprite1.body.moveLeft(10);
53+
}
54+
else if (cursors.right.isDown)
55+
{
56+
sprite1.body.moveRight(10);
57+
}
58+
59+
if (cursors.up.isDown)
60+
{
61+
sprite1.body.moveUp(10);
62+
}
63+
else if (cursors.down.isDown)
64+
{
65+
sprite1.body.moveDown(10);
66+
}
67+
68+
}
69+
70+
function render() {
71+
72+
game.debug.rectangle(sprite1.body);
73+
74+
for (var i = 0; i < diamonds.length; i++)
75+
{
76+
game.debug.rectangle(diamonds.children[i].body);
77+
}
78+
79+
80+
}

examples/wip/ninja1.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:
44

55
function preload() {
66

7-
game.load.spritesheet('ninja-tiles', 'assets/physics/ninja-tiles.png', 128, 128, 34);
7+
game.load.spritesheet('ninja-tiles', 'assets/physics/ninja-tiles128.png', 128, 128, 34);
88
game.load.image('a', 'assets/sprites/firstaid.png');
99

1010
}
@@ -34,11 +34,11 @@ function create() {
3434
game.physics.ninja.enableAABB(sprite1);
3535

3636
// But you can change it to either a Tile or a Circle
37-
tile1 = game.add.sprite(0, 550, 'ninja-tiles', 14);
37+
tile1 = game.add.sprite(0, 500, 'ninja-tiles', 14);
3838
tile1.width = 100;
3939
tile1.height = 100;
4040

41-
game.physics.ninja.enableTile(tile1, 14);
41+
game.physics.ninja.enableTile(tile1, tile1.frame);
4242

4343
// sprite1.body.aabb.friction = 0;
4444

@@ -107,9 +107,9 @@ function update() {
107107

108108
function render() {
109109

110-
game.debug.text(sprite1.body.shape.velocity.x, 32, 32);
111-
game.debug.text(sprite1.body.shape.velocity.y, 32, 64);
112-
game.debug.text(game.math.radToDeg(sprite1.body.angle), 32, 96);
110+
// game.debug.text(sprite1.body.shape.velocity.x, 32, 32);
111+
// game.debug.text(sprite1.body.shape.velocity.y, 32, 64);
112+
// game.debug.text(game.math.radToDeg(sprite1.body.angle), 32, 96);
113113

114114
// tile1.render(game.context, 'ninja-tiles');
115115
// tile2.render(game.context, 'ninja-tiles');

0 commit comments

Comments
 (0)