Warning: This version has a new ArcadePhysics handler in it. Don't upgrade if you need this for live game code, wait until we go to master. Otherwise, this commit contains lots of new physics demos and a new updateMotion and Body class to try and fix, once and for all, the physics issues with applied forces.

This commit is contained in:
photonstorm
2014-01-02 23:28:22 +00:00
parent 902ffee808
commit 5526a7322b
17 changed files with 1167 additions and 136 deletions
+149
View File
@@ -0,0 +1,149 @@
// mods by Patrick OReilly
// twitter: @pato_reilly
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('background','assets/misc/starfield.jpg');
game.load.image('player','assets/sprites/phaser-dude.png');
game.load.image('analog', 'assets/tests/fusia.png');
game.load.image('arrow', 'assets/sprites/longarrow2.png');
}
var myTween;
var player;
var cursors;
var arrow;
var catchFlag = false;
var launchVelocity = 0;
var launched;
function create() {
game.world.setBounds(0, 0, 5000, 600);
game.add.tileSprite(0, 0, 5000, 600, 'background');
var graphics = game.add.graphics(0,0);
graphics.beginFill(0x049e0c);
graphics.drawRect(395, 400, 10, 250);
analog = game.add.sprite(400, 400, 'analog');
analog.width = 8;
analog.rotation = 220;
analog.alpha = 0;
analog.anchor.setTo(0.5, 0.0);
arrow = game.add.sprite(400, 400, 'arrow');
arrow.anchor.setTo(0.1, 0.5);
arrow.alpha = 0;
player = game.add.sprite(150, 320, 'player');
player.anchor.setTo(0.5, 0.5);
player.body.collideWorldBounds = true;
player.body.bounce.setTo(0.9, 0.9);
player.body.drag.setTo(100, 100);
player.body.gravity.setTo(0, 8);
// Enable input.
player.inputEnabled = true;
player.input.start(0, true);
player.events.onInputDown.add(set);
player.events.onInputUp.add(launch);
//player.events.onInputOut.add(launch);
// this tween is to make the camera return to left side of world when done launching
// so it is not used until then
myTween = game.add.tween(player).to({x: 150}, 5000, Phaser.Easing.Linear.None);
myTween.onComplete.add(reappear, this);
game.camera.follow(player, Phaser.Camera.FOLLOW_TOPDOWN);
}
function reappear() {
launched = false;
player.alpha = 1;
}
function set(player,pointer) {
//disallow launching until reset
if (!launched)
{
catchFlag = true;
game.camera.follow(null);
player.body.gravity.setTo(0,0);
player.body.velocity.setTo(0,0);
}
}
function launch() {
if (catchFlag)
{
catchFlag = false;
launched = true;
game.camera.follow(player, Phaser.Camera.FOLLOW_TOPDOWN);
arrow.alpha = 0;
analog.alpha = 0;
Xvector = (arrow.x - player.x)*3.8;
Yvector = (arrow.y - player.y)*3.8;
player.body.gravity.setTo(0,8);
player.body.velocity.setTo(Xvector,Yvector);
}
}
function update() {
arrow.rotation = game.physics.angleBetween(arrow, player);
// Track the player sprite to the mouse
if (catchFlag)
{
distance = game.physics.distanceToPointer(arrow);
theta = game.physics.angleToPointer(arrow);
// Govern the distance the sprite is dragged away from launch post
if (distance > 300)
{
distance = 300;
adjacentX = Math.cos(theta) * distance;
oppositeY = Math.sin(theta) * distance;
player.x = 400 + adjacentX;
player.y = 400 + oppositeY;
analog.height = distance;
}
else
{
player.x = game.input.activePointer.worldX;
player.y = game.input.activePointer.worldY;
analog.height = distance;
}
arrow.alpha = 1;
analog.alpha = 0.5;
analog.rotation = arrow.rotation - Math.PI/2;
launchVelocity = analog.height;
}
//check sprite motion and if done, return camera to left side of world
var tweening = myTween.isRunning;
if (!tweening && launched && (player.x >= game.world.width-20 || player.body.deltaX() == 0))
{
player.body.velocity.setTo(0, 0);
player.alpha = 0;
myTween.start();
}
}
function render() {
game.debug.renderText("Drag the sprite and release to launch", 32, 32, 'rgb(0,255,0)');
game.debug.renderCameraInfo(game.camera, 32, 64);
game.debug.renderSpriteCoords(player, 32, 150);
game.debug.renderText("Launch Velocity: " + parseInt(launchVelocity), 550, 32, 'rgb(0,255,0)');
}
+102
View File
@@ -0,0 +1,102 @@
// mods by Patrick OReilly
// twitter: @pato_reilly
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('background','assets/misc/starfield.jpg');
game.load.image('player','assets/sprites/phaser-dude.png');
game.load.image('analog', 'assets/tests/fusia.png');
game.load.image('arrow', 'assets/sprites/longarrow2.png');
}
var player;
var cursors;
var arrow;
var catchFlag = false;
var launchVelocity = 0;
function create() {
game.world.setBounds(0, 0, 3400, 1000);
game.add.tileSprite(0, 0, 3400, 1000, 'background');
analog = game.add.sprite(200, 450, 'analog');
analog.width = 8;
analog.rotation = 220;
analog.alpha = 0;
analog.anchor.setTo(0.5, 0.0);
arrow = game.add.sprite(200, 450, 'arrow');
arrow.anchor.setTo(0.1, 0.5);
arrow.alpha = 0;
player = game.add.sprite(150, 320, 'player');
player.anchor.setTo(0.5, 0.5);
player.body.collideWorldBounds = true;
player.body.bounce.setTo(0.9, 0.9);
player.body.drag.setTo(50, 50);
// Enable input.
player.inputEnabled = true;
player.input.start(0, true);
player.events.onInputDown.add(set);
player.events.onInputUp.add(launch);
game.camera.follow(player, Phaser.Camera.FOLLOW_TOPDOWN);
}
function set(player,pointer) {
catchFlag = true;
game.camera.follow(null);
player.body.velocity.setTo(0, 0);
arrow.reset(player.x, player.y);
analog.reset(player.x, player.y);
}
function launch() {
catchFlag = false;
game.camera.follow(player, Phaser.Camera.FOLLOW_TOPDOWN);
arrow.alpha = 0;
analog.alpha = 0;
Xvector = (arrow.x - player.x) * 4.1;
Yvector = (arrow.y - player.y) * 4.1;
player.body.velocity.setTo(Xvector,Yvector);
}
function update() {
arrow.rotation = game.physics.angleBetween(arrow, player);
if (catchFlag == true)
{
// Track the ball sprite to the mouse
player.x = game.input.activePointer.worldX;
player.y = game.input.activePointer.worldY;
arrow.alpha = 1;
analog.alpha = 0.5;
analog.rotation = arrow.rotation - 3.14 / 2;
analog.height = game.physics.distanceBetween(arrow, player);
launchVelocity = analog.height;
}
}
function render() {
game.debug.renderText("Drag the sprite and release to launch", 32, 32, 'rgb(0,255,0)');
game.debug.renderCameraInfo(game.camera, 32, 64);
game.debug.renderSpriteCoords(player, 32, 150);
game.debug.renderText("Launch Velocity: " + parseInt(launchVelocity), 550, 32, 'rgb(0,255,0)');
}
+101
View File
@@ -0,0 +1,101 @@
// mods by Patrick OReilly
// twitter: @pato_reilly
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('analog', 'assets/tests/fusia.png');
game.load.image('arrow', 'assets/sprites/longarrow2.png');
game.load.image('ball', 'assets/sprites/pangball.png');
}
var arrow;
var ball;
var catchFlag = false;
var launchVelocity = 0;
function create() {
// set global gravity
game.physics.gravity.setTo(0,8);
game.stage.backgroundColor = '#0072bc';
var graphics = game.add.graphics(0,0);
graphics.beginFill(0x049e0c);
graphics.drawRect(395, 350, 10, 250);
analog = game.add.sprite(400, 350, 'analog');
analog.body.allowGravity = false;
analog.width = 8;
analog.rotation = 220;
analog.alpha = 0;
analog.anchor.setTo(0.5, 0.0);
arrow = game.add.sprite(400, 350, 'arrow');
arrow.anchor.setTo(0.1, 0.5);
arrow.body.allowGravity = false;
arrow.alpha = 0;
ball = game.add.sprite(100, 400, 'ball');
ball.anchor.setTo(0.5, 0.5);
ball.body.collideWorldBounds = true;
ball.body.bounce.setTo(0.9, 0.9);
ball.body.drag.setTo(50, 50);
// Enable input.
ball.inputEnabled = true;
ball.input.start(0, true);
ball.events.onInputDown.add(set);
ball.events.onInputUp.add(launch);
}
function set(ball, pointer) {
ball.body.velocity.setTo(0, 0);
ball.body.allowGravity = false;
catchFlag = true;
}
function launch() {
catchFlag = false;
arrow.alpha = 0;
analog.alpha = 0;
Xvector = (arrow.x - ball.x) * 4.1;
Yvector = (arrow.y - ball.y) * 4.1;
ball.body.allowGravity = true;
ball.body.velocity.setTo(Xvector,Yvector);
}
function update() {
arrow.rotation = game.physics.angleBetween(arrow, ball);
if (catchFlag == true)
{
// Track the ball sprite to the mouse
ball.x = game.input.activePointer.worldX;
ball.y = game.input.activePointer.worldY;
arrow.alpha = 1;
analog.alpha = 0.5;
analog.rotation = arrow.rotation - 3.14/2;
analog.height = game.physics.distanceToPointer(arrow);
launchVelocity = analog.height;
}
}
function render() {
game.debug.renderText("Drag the ball and release to launch", 32, 32);
game.debug.renderSpriteInfo(ball, 32, 64);
game.debug.renderText("Launch Velocity: " + parseInt(launchVelocity), 32, 250);
}