Skip to content

Commit 03ae324

Browse files
committed
New user submitted examples
1 parent a361a18 commit 03ae324

7 files changed

Lines changed: 440 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ New features:
5252
* Added Device.vibration to check if the Vibration API is available or not.
5353
* Added Device.trident and Device.tridentVersion for testing IE11 and forced IE11 to Canvas renderer until pixi updates to support it.
5454

55+
New Examples:
56+
57+
* Physics - Bounce by Patrick OReilly.
58+
* Physics - Bounce with gravity by Patrick OReilly.
59+
* Physics - Bounce accelerator (use the keyboard) by Patrick OReilly.
60+
* Physics - Bounce knock (use the keyboard) by Patrick OReilly.
61+
* Physics - Snake (use the keyboard to control the snake like creature) by Patrick OReilly and Richard Davey.
62+
5563
Updates:
5664

5765
* When a Sprite is destroyed any active filters are removed as well.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

examples/physics/bounce knock.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.image('dude', 'assets/sprites/phaser-dude.png');
9+
game.load.image('ball', 'assets/sprites/pangball.png');
10+
11+
}
12+
13+
var image;
14+
15+
function create() {
16+
17+
cursors = game.input.keyboard.createCursorKeys();
18+
19+
// This creates a simple sprite that is using our loaded image and
20+
// displays it on-screen
21+
// and assign it to a variable
22+
ball = game.add.sprite(400, 200, 'ball');
23+
knocker = game.add.sprite(400, 200, 'dude');
24+
25+
// This gets it moving
26+
ball.body.velocity.setTo(200, 200);
27+
28+
// This makes the game world bounce-able
29+
ball.body.collideWorldBounds = true;
30+
31+
// This sets the image bounce energy for the horizontal
32+
// and vertical vectors (as an x,y point). "1" is 100% energy return
33+
ball.body.bounce.setTo(1, 1);
34+
35+
// This sets the gravity the sprite responds to in the world, as a point
36+
// Here we leave x=0 and set y=8 to simulate falling
37+
ball.body.gravity.setTo(0, 8);
38+
39+
}
40+
41+
// Move the knocker with the arrow keys
42+
function update () {
43+
44+
if (cursors.up.isDown)
45+
{
46+
knocker.body.velocity.y = -400;
47+
}
48+
else if (cursors.down.isDown)
49+
{
50+
knocker.body.velocity.y = 400;
51+
}
52+
else if (cursors.left.isDown)
53+
{
54+
knocker.body.velocity.x = -400;
55+
}
56+
else if (cursors.right.isDown)
57+
{
58+
knocker.body.velocity.x = 400;
59+
}
60+
else
61+
{
62+
knocker.body.velocity.setTo(0, 0);
63+
}
64+
65+
// Enable physics between the knocker and the ball
66+
game.physics.collide(knocker, ball);
67+
68+
}
69+
70+
function render () {
71+
72+
//debug helper
73+
game.debug.renderSpriteInfo(ball, 32, 32);
74+
75+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
// You can fill the preloader with as many assets as your game requires
9+
10+
// Here we are loading an image. The first parameter is the unique
11+
// string by which we'll identify the image later in our code.
12+
13+
// The second parameter is the URL of the image (relative)
14+
game.load.image('flyer', 'assets/sprites/phaser-dude.png');
15+
}
16+
17+
var image;
18+
19+
function create() {
20+
21+
// This creates a simple sprite that is using our loaded image and displays it on-screen and assign it to a variable
22+
image = game.add.sprite(400, 200, 'flyer');
23+
24+
// This gets it moving
25+
image.body.velocity.setTo(200, 200);
26+
27+
// This makes the game world bounce-able
28+
image.body.collideWorldBounds = true;
29+
30+
// This sets the image bounce energy for the horizontal and vertical vectors (as an x,y point). "1" is 100% energy return
31+
image.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+
image.body.gravity.setTo(0, 8);
36+
37+
}
38+
39+
function update () {
40+
41+
// nothing required here
42+
43+
}
44+
45+
function render () {
46+
47+
//debug helper
48+
game.debug.renderSpriteInfo(image,32,32);
49+
50+
}

examples/physics/bounce.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// You can fill the preloader with as many assets as your game requires
9+
10+
// Here we are loading an image. The first parameter is the unique
11+
// string by which we'll identify the image later in our code.
12+
13+
// The second parameter is the URL of the image (relative)
14+
game.load.image('flyer', 'assets/sprites/phaser-dude.png');
15+
}
16+
17+
var image;
18+
19+
function create() {
20+
21+
// This creates a simple sprite that is using our loaded image and
22+
// displays it on-screen
23+
// and assign it to a variable
24+
image = game.add.sprite(0, 0, 'flyer');
25+
26+
// This gets it moving
27+
image.body.velocity.setTo(200,200);
28+
29+
// This makes the game world bounce-able
30+
image.body.collideWorldBounds = true;
31+
32+
// This sets the image bounce energy for the horizontal
33+
// and vertical vectors. "1" is 100% energy return
34+
image.body.bounce.setTo(1,1);
35+
36+
}
37+
38+
function update () {
39+
40+
//nothing required here
41+
42+
}
43+
44+
function render () {
45+
//debug helper
46+
game.debug.renderSpriteInfo(image,32,32);
47+
}

examples/physics/snake.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Snake by Patrick OReilly and Richard Davey
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.image('ball','assets/sprites/shinyball.png');
9+
10+
}
11+
12+
var snakeHead; //head of snake sprite
13+
var snakeSection = new Array(); //array of sprites that make the snake body sections
14+
var snakePath = new Array(); //arrary of positions(points) that have to be stored for the path the sections follow
15+
var numSnakeSections = 30; //number of snake body sections
16+
var snakeSpacer = 6; //parameter that sets the spacing between sections
17+
18+
function create() {
19+
20+
game.world.setBounds(0, 0, 800, 600);
21+
22+
cursors = game.input.keyboard.createCursorKeys();
23+
24+
snakeHead = game.add.sprite(400, 300, 'ball');
25+
snakeHead.anchor.setTo(0.5, 0.5);
26+
27+
// Init snakeSection array
28+
for (var i = 1; i <= numSnakeSections-1; i++)
29+
{
30+
snakeSection[i] = game.add.sprite(400, 300, 'ball');
31+
snakeSection[i].anchor.setTo(0.5, 0.5);
32+
}
33+
34+
// Init snakePath array
35+
for (var i = 0; i <= numSnakeSections * snakeSpacer; i++)
36+
{
37+
snakePath[i] = new Phaser.Point(400, 300);
38+
}
39+
40+
}
41+
42+
function update() {
43+
44+
snakeHead.body.velocity.setTo(0, 0);
45+
snakeHead.body.angularVelocity = 0;
46+
47+
if (cursors.up.isDown)
48+
{
49+
snakeHead.body.velocity.copyFrom(game.physics.velocityFromAngle(snakeHead.angle, 300));
50+
51+
// Everytime the snake head moves, insert the new location at the start of the array,
52+
// and knock the last position off the end
53+
54+
var part = snakePath.pop();
55+
56+
part.setTo(snakeHead.x, snakeHead.y);
57+
58+
snakePath.unshift(part);
59+
60+
for (var i = 1; i <= numSnakeSections - 1; i++)
61+
{
62+
snakeSection[i].x = (snakePath[i * snakeSpacer]).x;
63+
snakeSection[i].y = (snakePath[i * snakeSpacer]).y;
64+
}
65+
}
66+
67+
if (cursors.left.isDown)
68+
{
69+
snakeHead.body.angularVelocity = -300;
70+
}
71+
else if (cursors.right.isDown)
72+
{
73+
snakeHead.body.angularVelocity = 300;
74+
}
75+
76+
}
77+
78+
function render() {
79+
80+
game.debug.renderSpriteInfo(snakeHead, 32, 32);
81+
82+
}

0 commit comments

Comments
 (0)