forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef359e8
commit 6b55fea
Showing
7 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); | ||
|
||
function preload() { | ||
|
||
game.load.image('atari', 'assets/sprites/cokecan.png'); | ||
game.load.image('ball', 'assets/sprites/red_ball.png'); | ||
game.load.image('sky', 'assets/skies/cavern2.png'); | ||
|
||
} | ||
|
||
var sprite1; | ||
var sprite2; | ||
var cursors; | ||
|
||
function create() { | ||
|
||
game.add.image(0, 0, 'sky'); | ||
|
||
// Enable p2 physics | ||
game.physics.startSystem(Phaser.Physics.P2JS); | ||
|
||
// Add 2 sprites which we'll join with a spring | ||
sprite1 = game.add.sprite(400, 300, 'ball'); | ||
sprite2 = game.add.sprite(400, 400, 'atari'); | ||
|
||
game.physics.p2.enable([sprite1, sprite2]); | ||
|
||
var constraint = game.physics.p2.createDistanceConstraint(sprite1, sprite2, 150); | ||
|
||
text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' }); | ||
|
||
cursors = game.input.keyboard.createCursorKeys(); | ||
|
||
} | ||
|
||
function update() { | ||
|
||
sprite1.body.setZeroVelocity(); | ||
|
||
if (cursors.left.isDown) | ||
{ | ||
sprite1.body.moveLeft(400); | ||
} | ||
else if (cursors.right.isDown) | ||
{ | ||
sprite1.body.moveRight(400); | ||
} | ||
|
||
if (cursors.up.isDown) | ||
{ | ||
sprite1.body.moveUp(400); | ||
} | ||
else if (cursors.down.isDown) | ||
{ | ||
sprite1.body.moveDown(400); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); | ||
|
||
function preload() { | ||
|
||
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png'); | ||
game.load.image('ball', 'assets/sprites/arrow.png'); | ||
game.load.image('sky', 'assets/skies/cavern2.png'); | ||
|
||
} | ||
|
||
var sprite; | ||
var cursors; | ||
|
||
function create() { | ||
|
||
game.add.image(0, 0, 'sky'); | ||
|
||
// Enable p2 physics | ||
game.physics.startSystem(Phaser.Physics.P2JS); | ||
|
||
// Add 2 sprites which we'll join with a constraint | ||
sprite = game.add.sprite(400, 200, 'ball'); | ||
|
||
var sonic1 = game.add.sprite(200, 400, 'sonic'); | ||
var sonic2 = game.add.sprite(600, 400, 'sonic'); | ||
|
||
game.physics.p2.enable([sprite, sonic1, sonic2]); | ||
|
||
// This constraint will make sure that as sprite rotates, sonic1 matches that rotation | ||
var constraint1 = game.physics.p2.createGearConstraint(sprite, sonic1, 0, 1); | ||
|
||
// This constraint will make sure that as sprite rotates, sonic2 matches that rotation at half the ratio | ||
var constraint2 = game.physics.p2.createGearConstraint(sprite, sonic2, 0, 0.5); | ||
|
||
text = game.add.text(20, 20, 'rotate with arrow keys', { fill: '#ffffff' }); | ||
|
||
cursors = game.input.keyboard.createCursorKeys(); | ||
|
||
} | ||
|
||
function update() { | ||
|
||
if (cursors.left.isDown) | ||
{ | ||
sprite.body.rotateLeft(50); | ||
} | ||
else if (cursors.right.isDown) | ||
{ | ||
sprite.body.rotateRight(50); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @author Richard Davey <[email protected]> | ||
* @copyright 2014 Photon Storm Ltd. | ||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} | ||
*/ | ||
|
||
/** | ||
* A constraint that tries to keep the distance between two bodies constant. | ||
* | ||
* @class Phaser.Physics.P2.DistanceConstraint | ||
* @classdesc Physics DistanceConstraint Constructor | ||
* @constructor | ||
* @param {Phaser.Physics.P2} world - A reference to the P2 World. | ||
* @param {p2.Body} bodyA - First connected body. | ||
* @param {p2.Body} bodyB - Second connected body. | ||
* @param {number} distance - The distance to keep between the bodies. | ||
* @param {number} [maxForce] - The maximum force to apply to the constraint | ||
*/ | ||
Phaser.Physics.P2.DistanceConstraint = function (world, bodyA, bodyB, distance, maxForce) { | ||
|
||
/** | ||
* @property {Phaser.Game} game - Local reference to game. | ||
*/ | ||
this.game = world.game; | ||
|
||
/** | ||
* @property {Phaser.Physics.P2} world - Local reference to P2 World. | ||
*/ | ||
this.world = world; | ||
|
||
if (typeof distance === 'undefined') { distance = 100; } | ||
|
||
distance = world.pxm(distance); | ||
|
||
p2.DistanceConstraint.call(this, bodyA, bodyB, distance, maxForce); | ||
|
||
} | ||
|
||
Phaser.Physics.P2.DistanceConstraint.prototype = Object.create(p2.DistanceConstraint.prototype); | ||
Phaser.Physics.P2.DistanceConstraint.prototype.constructor = Phaser.Physics.P2.DistanceConstraint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @author Richard Davey <[email protected]> | ||
* @copyright 2014 Photon Storm Ltd. | ||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} | ||
*/ | ||
|
||
/** | ||
* Connects two bodies at given offset points, letting them rotate relative to each other around this point. | ||
* | ||
* @class Phaser.Physics.P2.GearConstraint | ||
* @classdesc Physics GearConstraint Constructor | ||
* @constructor | ||
* @param {Phaser.Physics.P2} world - A reference to the P2 World. | ||
* @param {p2.Body} bodyA - First connected body. | ||
* @param {p2.Body} bodyB - Second connected body. | ||
* @param {number} [angle=0] - The relative angle | ||
* @param {number} [ratio=1] - The gear ratio. | ||
*/ | ||
Phaser.Physics.P2.GearConstraint = function (world, bodyA, bodyB, angle, ratio) { | ||
|
||
if (typeof angle === 'undefined') { angle = 0; } | ||
if (typeof ratio === 'undefined') { ratio = 1; } | ||
|
||
/** | ||
* @property {Phaser.Game} game - Local reference to game. | ||
*/ | ||
this.game = world.game; | ||
|
||
/** | ||
* @property {Phaser.Physics.P2} world - Local reference to P2 World. | ||
*/ | ||
this.world = world; | ||
|
||
var options = { angle: angle, ratio: ratio }; | ||
|
||
p2.GearConstraint.call(this, bodyA, bodyB, options); | ||
|
||
} | ||
|
||
Phaser.Physics.P2.GearConstraint.prototype = Object.create(p2.GearConstraint.prototype); | ||
Phaser.Physics.P2.GearConstraint.prototype.constructor = Phaser.Physics.P2.GearConstraint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters