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
+20
View File
@@ -254,6 +254,10 @@
"file": "matching+pairs.js",
"title": "matching pairs"
},
{
"file": "simon.js",
"title": "simon"
},
{
"file": "starstruck.js",
"title": "starstruck"
@@ -261,6 +265,10 @@
{
"file": "tanks.js",
"title": "tanks"
},
{
"file": "wabbits.js",
"title": "wabbits"
}
],
"geometry": [
@@ -608,6 +616,18 @@
"file": "framerate+independence.js",
"title": "framerate independence"
},
{
"file": "launcher+follow+world.js",
"title": "launcher follow world"
},
{
"file": "launcher+follow.js",
"title": "launcher follow"
},
{
"file": "launcher.js",
"title": "launcher"
},
{
"file": "mass+velocity+test.js",
"title": "mass velocity test"
Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 710 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

+214
View File
@@ -0,0 +1,214 @@
// mods by Patrick OReilly
// Twitter: @pato_reilly Web: http://patricko.byethost9.com
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('item', 'assets/buttons/number-buttons.png', 160, 160);
}
var simon;
var N = 1;
var userCount = 0;
var currentCount = 0;
var sequenceCount = 16;
var sequenceList = [];
var simonSez = false;
var timeCheck;
var litSquare;
var winner;
var loser;
var intro;
function create() {
simon = game.add.group();
var item;
for (var i = 0; i < 3; i++)
{
item = simon.create(150 + 168 * i, 150, 'item', i);
// Enable input.
item.input.start(0, true);
item.events.onInputDown.add(select);
item.events.onInputUp.add(release);
item.events.onInputOut.add(moveOff);
simon.getAt(i).alpha = 0;
}
for (var i = 0; i < 3; i++)
{
item = simon.create(150 + 168 * i, 318, 'item', i + 3);
// Enable input.
item.input.start(0, true);
item.events.onInputDown.add(select);
item.events.onInputUp.add(release);
item.events.onInputOut.add(moveOff);
simon.getAt(i + 3).alpha = 0;
}
introTween();
setUp();
setTimeout(function(){simonSequence(); intro = false;}, 5000);
}
function restart() {
N = 1;
userCount = 0;
currentCount = 0;
sequenceList = [];
winner = false;
loser = false;
introTween();
setUp();
setTimeout(function(){simonSequence(); intro=false;}, 5000);
}
function introTween() {
intro = true;
for (var i = 0; i < 6; i++)
{
game.add.tween(simon.getAt(i)).to( { alpha: 1 }, 500, Phaser.Easing.Linear.None, true, 0, 4, true).to( { alpha: .25 }, 500, Phaser.Easing.Linear.None, true);
}
}
function update() {
if (simonSez)
{
if (game.time.now - timeCheck >700-N*40)
{
simon.getAt(litSquare).alpha = .25;
game.paused = true;
setTimeout(function()
{
if ( currentCount< N)
{
game.paused = false;
simonSequence();
}
else
{
simonSez = false;
game.paused = false;
}
}, 400 - N * 20);
}
}
}
function playerSequence(selected) {
correctSquare = sequenceList[userCount];
userCount++;
thisSquare = simon.getIndex(selected);
if (thisSquare == correctSquare)
{
if (userCount == N)
{
if (N == sequenceCount)
{
winner = true;
setTimeout(function(){restart();}, 3000);
}
else
{
userCount = 0;
currentCount = 0;
N++;
simonSez = true;
}
}
}
else
{
loser = true;
setTimeout(function(){restart();}, 3000);
}
}
function simonSequence () {
simonSez = true;
litSquare = sequenceList[currentCount];
simon.getAt(litSquare).alpha = 1;
timeCheck = game.time.now;
currentCount++;
}
function setUp() {
for (var i = 0; i < sequenceCount; i++)
{
thisSquare = game.rnd.integerInRange(0,6);
sequenceList.push(thisSquare);
}
}
function select(item, pointer) {
if (!simonSez && !intro && !loser && !winner)
{
item.alpha = 1;
}
}
function release(item, pointer) {
if (!simonSez && !intro && !loser && !winner)
{
item.alpha = .25;
playerSequence(item);
}
}
function moveOff(item, pointer) {
if (!simonSez && !intro && !loser && !winner)
{
item.alpha = .25;
}
}
function render() {
if (!intro)
{
if (simonSez)
{
game.debug.renderText('Simon Sez', 360, 96, 'rgb(255,0,0)');
}
else
{
game.debug.renderText('Your Turn', 360, 96, 'rgb(0,255,0)');
}
}
else
{
game.debug.renderText('Get Ready', 360, 96, 'rgb(0,0,255)');
}
if (winner)
{
game.debug.renderText('You Win!', 360, 32, 'rgb(0,0,255)');
}
else if (loser)
{
game.debug.renderText('You Lose!', 360, 32, 'rgb(0,0,255)');
}
}
+162
View File
@@ -0,0 +1,162 @@
// 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');
game.load.image('hill', 'assets/pics/atari_fujilogo.png');
game.load.image('block', 'assets/sprites/block.png');
game.load.image('wabbit', 'assets/sprites/wabbit.png');
}
var arrow;
var ball;
var catchFlag = false;
var wabbits;
var wab;
var launchVelocity = 0;
var shotCount = 0;
var wabbitCount = 0;
function create() {
// set global gravity
game.physics.gravity.setTo(0, 4);
game.stage.backgroundColor = '#0072bc';
var graphics = game.add.graphics(0, 0);
graphics.beginFill(0x049e0c);
graphics.drawRect(195, 450, 10, 150);
wabbits = game.add.group();
for (var i = 0; i < 5; i++)
{
wab = wabbits.create(game.rnd.integerInRange(575, 650), game.rnd.integerInRange(150, 260), 'wabbit');
wab.name = 'wab' + i;
wab.body.collideWorldBounds = true;
wab.body.mass = 0.1;
wab.body.drag.setTo(50, 50);
}
hill = game.add.sprite(450, 400, 'hill');
hill.body.collideWorldBounds = true;
hill.body.mass = 10;
hill.body.immovable = true;
block = game.add.sprite(575, 300, 'block');
block.body.collideWorldBounds = true;
block.body.mass = 5;
block.body.drag.setTo(100, 100);
analog = game.add.sprite(200, 450, '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(200, 450, '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.inputEnabled = true;
ball.body.collideWorldBounds = true;
ball.body.bounce.setTo(0.9, 0.9);
ball.body.drag.setTo(100, 100);
// Enable input.
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;
shotCount++;
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;
}
game.physics.collide(ball, wabbits);
game.physics.collide(wabbits, wabbits);
game.physics.collide(wabbits, hill);
game.physics.collide(ball, hill);
game.physics.collide(block, hill);
game.physics.collide(block, ball);
game.physics.collide(block, wabbits);
// check wabbits
var wabs = 0;
for (var i = 0; i < 5; i++)
{
thisWab = wabbits.getAt(i);
if (thisWab.x > 770 && thisWab.y > 400)
{
thisWab.alive = false;
wabs++;
}
}
wabbitCount = 5 - wabs;
console.log(ball.body.y, ball.body.motionVelocity.y);
}
function render() {
game.debug.renderSpriteCollision(ball, 32, 32);
// var graphics = game.add.graphics(0, 0);
// game.context.fillStyle = 'rgba(0,0,255,0.5)';
// game.context.fillRect(770, 400, 30, 200);
// game.debug.renderText("Drag the ball to launch", 32, 32);
// game.debug.renderText("Try to get all 5 wabbits in the blue area with the least number of shots", 32, 64);
// game.debug.renderText("Shot Count: " + shotCount, 32, 96);
// game.debug.renderText("Wabbits Left: " + wabbitCount, 32, 128);
}
+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);
}
+57
View File
@@ -0,0 +1,57 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('chunk', 'assets/sprites/chunk.png');
}
var sprite;
var bmd;
function create() {
game.stage.backgroundColor = '#2384e7';
bmd = game.add.bitmapData(800, 600);
bmd.fillStyle('#ffffff');
game.add.sprite(0, 0, bmd);
sprite = game.add.sprite(32, 600, 'chunk');
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.5, 0.5);
sprite.body.drag.setTo(0, -20);
// sprite.body.drag.setTo(2, 0);
// sprite.body.sleepMin.setTo(-50, -20);
// sprite.body.sleepMax.setTo(50, 20);
// sprite.body.sleepDuration = 1000;
sprite.body.sleepMin.setTo(0, -5);
sprite.body.sleepMax.setTo(0, 5);
sprite.body.sleepDuration = 1000;
game.input.onDown.add(launch, this);
}
function launch() {
sprite.body.velocity.setTo(0, -300);
// sprite.body.gravity.setTo(0, 200);
}
function update() {
bmd.fillRect(sprite.x, sprite.y, 2, 2);
}
function render() {
game.debug.renderBodyInfo(sprite, 16, 16);
}