mirror of
https://github.com/wassname/phaser.git
synced 2026-07-01 16:50:43 +08:00
New labs demo. Added moveForward and moveBackward to Body.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
// game.load.image('arrow', 'assets/sprites/arrow.png');
|
||||
game.load.image('arrow', 'assets/sprites/thrust_ship2.png');
|
||||
game.load.image('chunk', 'assets/sprites/chunk.png');
|
||||
game.load.spritesheet('bullets', 'assets/sprites/balls.png', 17, 17);
|
||||
|
||||
}
|
||||
|
||||
var cannon;
|
||||
var bullets;
|
||||
var angle = 0;
|
||||
var fireRate = 100;
|
||||
var nextFire = 0;
|
||||
var cursors;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
game.physics.gravity[1] = -8.5;
|
||||
game.physics.defaultRestitution = 0.8;
|
||||
game.physics.defaultFriction = 0.1;
|
||||
|
||||
bullets = game.add.group();
|
||||
bullets.createMultiple(250, 'bullets', 0, false);
|
||||
|
||||
cannon = game.add.sprite(50, 500, 'arrow');
|
||||
cannon.physicsEnabled = true;
|
||||
cannon.body.kinematic = true;
|
||||
cannon.body.mass = 4;
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function fire() {
|
||||
|
||||
if (game.time.now > nextFire)
|
||||
{
|
||||
nextFire = game.time.now + fireRate;
|
||||
|
||||
var bullet = bullets.getFirstExists(false);
|
||||
|
||||
if (bullet)
|
||||
{
|
||||
bullet.frame = game.rnd.integerInRange(0,6);
|
||||
bullet.exists = true;
|
||||
bullet.position.set(cannon.x, cannon.y);
|
||||
bullet.physicsEnabled = true;
|
||||
|
||||
bullet.body.rotation = cannon.rotation + game.math.degToRad(90);
|
||||
|
||||
var magnitude = game.math.px2p(-500);
|
||||
var angle = bullet.body.rotation + Math.PI / 2;
|
||||
|
||||
bullet.body.velocity.x = magnitude * Math.cos(angle);
|
||||
bullet.body.velocity.y = magnitude * Math.sin(angle);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
cannon.body.rotateLeft(100);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
cannon.body.rotateRight(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
cannon.body.setZeroRotation();
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
cannon.body.moveForward(200);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
cannon.body.moveBackward(200);
|
||||
}
|
||||
|
||||
var dx = game.input.activePointer.worldX - cannon.x;
|
||||
var dy = game.input.activePointer.worldY - cannon.y;
|
||||
cannon.rotation = Math.atan2(dy, dx);
|
||||
|
||||
if (game.input.activePointer.isDown)
|
||||
{
|
||||
fire();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
// game.load.image('arrow', 'assets/sprites/arrow.png');
|
||||
game.load.image('arrow', 'assets/sprites/thrust_ship2.png');
|
||||
game.load.image('chunk', 'assets/sprites/chunk.png');
|
||||
game.load.spritesheet('bullets', 'assets/sprites/balls.png', 17, 17);
|
||||
|
||||
}
|
||||
|
||||
var cannon;
|
||||
var bullets;
|
||||
var angle = 0;
|
||||
var fireRate = 100;
|
||||
var nextFire = 0;
|
||||
var cursors;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
game.physics.gravity[1] = -8.5;
|
||||
game.physics.defaultRestitution = 0.8;
|
||||
game.physics.defaultFriction = 0.1;
|
||||
|
||||
bullets = game.add.group();
|
||||
bullets.createMultiple(250, 'bullets', 0, false);
|
||||
|
||||
cannon = game.add.sprite(50, 500, 'arrow');
|
||||
cannon.physicsEnabled = true;
|
||||
cannon.body.kinematic = true;
|
||||
cannon.body.mass = 4;
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function fire() {
|
||||
|
||||
if (game.time.now > nextFire)
|
||||
{
|
||||
nextFire = game.time.now + fireRate;
|
||||
|
||||
var bullet = bullets.getFirstExists(false);
|
||||
|
||||
if (bullet)
|
||||
{
|
||||
bullet.frame = game.rnd.integerInRange(0,6);
|
||||
bullet.exists = true;
|
||||
bullet.position.set(cannon.x, cannon.y);
|
||||
bullet.physicsEnabled = true;
|
||||
|
||||
bullet.body.rotation = cannon.rotation + game.math.degToRad(90);
|
||||
|
||||
var magnitude = game.math.px2p(-500);
|
||||
var angle = bullet.body.rotation + Math.PI / 2;
|
||||
|
||||
bullet.body.velocity.x = magnitude * Math.cos(angle);
|
||||
bullet.body.velocity.y = magnitude * Math.sin(angle);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
cannon.body.rotateLeft(100);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
cannon.body.rotateRight(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
cannon.body.setZeroRotation();
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
cannon.body.moveForward(200);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
cannon.body.moveBackward(200);
|
||||
}
|
||||
|
||||
var dx = game.input.activePointer.worldX - cannon.x;
|
||||
var dy = game.input.activePointer.worldY - cannon.y;
|
||||
cannon.rotation = Math.atan2(dy, dx);
|
||||
|
||||
if (game.input.activePointer.isDown)
|
||||
{
|
||||
fire();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('ship', 'assets/sprites/thrust_ship2.png');
|
||||
game.load.image('chunk', 'assets/sprites/chunk.png');
|
||||
|
||||
}
|
||||
|
||||
var ship;
|
||||
var sprite2;
|
||||
var body;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
game.physics.gravity[1] = -1;
|
||||
|
||||
ship = game.add.sprite(200, 200, 'ship');
|
||||
ship.physicsEnabled = true;
|
||||
// We do this because our ship is shaped like a triangle, not a square :)
|
||||
ship.body.addPolygon({}, 29, 23 , 0, 23 , 14, 1);
|
||||
|
||||
sprite2 = game.add.sprite(300, 300, 'chunk');
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
game.input.onDown.add(spawn, this);
|
||||
|
||||
}
|
||||
|
||||
function spawn() {
|
||||
|
||||
body = new Phaser.Physics.Body(game, null, 300, 100, 1);
|
||||
|
||||
body.addParticle(4, 4, 0, 0, 0);
|
||||
|
||||
game.physics.addBody(body.data);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (body)
|
||||
{
|
||||
sprite2.x = body.x;
|
||||
sprite2.y = body.y;
|
||||
}
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
ship.body.rotateLeft(100);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
ship.body.rotateRight(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
ship.body.setZeroRotation();
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
ship.body.thrust(400);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
ship.body.reverse(400);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('arrow', 'assets/sprites/arrow.png');
|
||||
game.load.image('chunk', 'assets/sprites/chunk.png');
|
||||
game.load.spritesheet('bullets', 'assets/sprites/balls.png', 17, 17);
|
||||
|
||||
}
|
||||
|
||||
var cannon;
|
||||
var bullets;
|
||||
var angle = 0;
|
||||
var fireRate = 100;
|
||||
var nextFire = 0;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
game.physics.gravity[1] = -8.5;
|
||||
game.physics.defaultRestitution = 0.8;
|
||||
game.physics.defaultFriction = 0.1;
|
||||
|
||||
bullets = game.add.group();
|
||||
bullets.createMultiple(500, 'bullets', 0, false);
|
||||
|
||||
cannon = game.add.sprite(50, 500, 'arrow');
|
||||
cannon.anchor.set(0, 0.5);
|
||||
|
||||
}
|
||||
|
||||
function fire() {
|
||||
|
||||
if (game.time.now > nextFire)
|
||||
{
|
||||
nextFire = game.time.now + fireRate;
|
||||
|
||||
var bullet = bullets.getFirstExists(false);
|
||||
|
||||
if (bullet)
|
||||
{
|
||||
bullet.frame = game.rnd.integerInRange(0,6);
|
||||
bullet.exists = true;
|
||||
bullet.position.set(cannon.x, cannon.y);
|
||||
bullet.physicsEnabled = true;
|
||||
|
||||
bullet.body.rotation = cannon.rotation + game.math.degToRad(90);
|
||||
|
||||
var magnitude = game.math.px2p(-500);
|
||||
var angle = bullet.body.rotation + Math.PI / 2;
|
||||
|
||||
bullet.body.velocity.x = magnitude * Math.cos(angle);
|
||||
bullet.body.velocity.y = magnitude * Math.sin(angle);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
var dx = game.input.activePointer.worldX - cannon.x;
|
||||
var dy = game.input.activePointer.worldY - cannon.y;
|
||||
cannon.rotation = Math.atan2(dy, dx);
|
||||
|
||||
if (game.input.activePointer.isDown)
|
||||
{
|
||||
fire();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
}
|
||||
@@ -205,6 +205,40 @@ Phaser.Physics.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Moves the Body forwards based on its current angle and the given speed.
|
||||
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
|
||||
*
|
||||
* @method Phaser.Physics.Body#moveForward
|
||||
* @param {number} speed - The speed at which it should move forwards.
|
||||
*/
|
||||
moveForward: function (speed) {
|
||||
|
||||
var magnitude = this.px2p(-speed);
|
||||
var angle = this.data.angle + Math.PI / 2;
|
||||
|
||||
this.data.velocity[0] = magnitude * Math.cos(angle);
|
||||
this.data.velocity[1] = magnitude * Math.sin(angle);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Moves the Body backwards based on its current angle and the given speed.
|
||||
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
|
||||
*
|
||||
* @method Phaser.Physics.Body#moveBackward
|
||||
* @param {number} speed - The speed at which it should move backwards.
|
||||
*/
|
||||
moveBackward: function (speed) {
|
||||
|
||||
var magnitude = this.px2p(-speed);
|
||||
var angle = this.data.angle + Math.PI / 2;
|
||||
|
||||
this.data.velocity[0] = -(magnitude * Math.cos(angle));
|
||||
this.data.velocity[1] = -(magnitude * Math.sin(angle));
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Applies a force to the Body that causes it to 'thrust' forwards, based on its current angle and the given speed.
|
||||
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
|
||||
@@ -859,6 +893,7 @@ Object.defineProperty(Phaser.Physics.Body.prototype, "kinematic", {
|
||||
if (value && this.data.motionState !== Phaser.KINEMATIC)
|
||||
{
|
||||
this.data.motionState = Phaser.KINEMATIC;
|
||||
this.mass = 4;
|
||||
}
|
||||
else if (!value && this.data.motionState === Phaser.KINEMATIC)
|
||||
{
|
||||
|
||||
@@ -30,6 +30,11 @@ Phaser.Physics.World = function (game) {
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {Phaser.PointProxy} force - The force applied to the body.
|
||||
*/
|
||||
// this.gravity = new Phaser.Physics.PointProxy(this.data.force);
|
||||
|
||||
this.onBodyAdded = new Phaser.Signal();
|
||||
this.onBodyRemoved = new Phaser.Signal();
|
||||
|
||||
@@ -271,3 +276,42 @@ Phaser.Physics.World.prototype.createBody = function (x, y, mass, addToWorld, op
|
||||
return body;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @method Phaser.Physics.World.prototype.createBody
|
||||
* @param {number} x - The x coordinate of Body.
|
||||
* @param {number} y - The y coordinate of Body.
|
||||
* @param {number} mass - The mass of the Body. A mass of 0 means a 'static' Body is created.
|
||||
* @param {boolean} [addToWorld=false] - Automatically add this Body to the world? (usually false as it won't have any shapes on construction).
|
||||
* @param {object} options - An object containing the build options:
|
||||
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
|
||||
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
|
||||
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
|
||||
* @param {(number[]|...number)} points - An array of 2d vectors that form the convex or concave polygon.
|
||||
* Either [[0,0], [0,1],...] or a flat array of numbers that will be interpreted as [x,y, x,y, ...],
|
||||
* or the arguments passed can be flat x,y values e.g. `setPolygon(options, x,y, x,y, x,y, ...)` where `x` and `y` are numbers.
|
||||
*/
|
||||
Phaser.Physics.World.prototype.createParticle = function (x, y, mass, addToWorld, options, data) {
|
||||
|
||||
if (typeof addToWorld === 'undefined') { addToWorld = false; }
|
||||
|
||||
var body = new Phaser.Physics.Body(this.game, null, x, y, mass);
|
||||
|
||||
if (data)
|
||||
{
|
||||
var result = body.addPolygon(options, data);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (addToWorld)
|
||||
{
|
||||
this.addBody(body.data);
|
||||
}
|
||||
|
||||
return body;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user