mirror of
https://github.com/wassname/phaser.git
synced 2026-08-20 12:40:12 +08:00
New split physics system is implemented. Still tidying-up, but ArcadePhysics, P2 and Ninja Physics are in and configured. Lots more examples required, and tilemap collision mostly broken in Arcade at the moment. Time to implement in Ninja.
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 272 KiB |
@@ -0,0 +1,122 @@
|
||||
|
||||
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.spritesheet('ninja-tiles', 'assets/physics/ninja-tiles.png', 128, 128, 34);
|
||||
game.load.image('a', 'assets/sprites/firstaid.png');
|
||||
|
||||
}
|
||||
|
||||
var sprite1;
|
||||
var cursors;
|
||||
|
||||
var tile1;
|
||||
var tile2;
|
||||
|
||||
var t;
|
||||
var running = false;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.smoothed = true;
|
||||
|
||||
// Activate the Ninja physics system
|
||||
game.physics.startSystem(Phaser.Physics.NINJA);
|
||||
|
||||
// game.physics.ninja.gravity = 0.1;
|
||||
|
||||
sprite1 = game.add.sprite(500, 200, 'a');
|
||||
|
||||
// Enable the physics body for the Ninja physics system
|
||||
// By default it will create an AABB body for the sprite
|
||||
game.physics.ninja.enableAABB(sprite1);
|
||||
|
||||
// But you can change it to either a Tile or a Circle
|
||||
tile1 = game.add.sprite(0, 550, 'ninja-tiles', 14);
|
||||
tile1.width = 100;
|
||||
tile1.height = 100;
|
||||
|
||||
game.physics.ninja.enableTile(tile1, 14);
|
||||
|
||||
// sprite1.body.aabb.friction = 0;
|
||||
|
||||
// tile1 = new Phaser.Physics.Ninja.Tile(game.physics.ninja, 100, 500, 100, 100, Phaser.Physics.Ninja.Tile.SLOPE_45DEGpn);
|
||||
// tile1 = new Phaser.Physics.Ninja.Tile(game.physics.ninja, 100, 500, 100, 100, 15);
|
||||
// tile2 = new Phaser.Physics.Ninja.Tile(game.physics.ninja, 300, 500, 100, 100, 7);
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler() {
|
||||
game.stage.backgroundColor = 0xff0000;
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
game.physics.ninja.collide(sprite1, tile1, collisionHandler, null, this);
|
||||
|
||||
tile1.body.moveRight(1);
|
||||
|
||||
/*
|
||||
if (cursors.up.isDown && !running)
|
||||
{
|
||||
running = true;
|
||||
t = Date.now();
|
||||
}
|
||||
|
||||
sprite1.body.setZeroVelocity();
|
||||
|
||||
if (running)
|
||||
{
|
||||
sprite1.body.moveRight(100);
|
||||
|
||||
if (sprite1.body.x >= 200)
|
||||
{
|
||||
var ms = Date.now() - t;
|
||||
console.log('100px in ', ms);
|
||||
running = false;
|
||||
sprite1.body.setZeroVelocity();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// sprite1.body.setZeroVelocity();
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
sprite1.body.moveLeft(20);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
sprite1.body.moveRight(20);
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
sprite1.body.moveUp(20);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
sprite1.body.moveUp(20);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.text(sprite1.body.shape.velocity.x, 32, 32);
|
||||
game.debug.text(sprite1.body.shape.velocity.y, 32, 64);
|
||||
game.debug.text(game.math.radToDeg(sprite1.body.angle), 32, 96);
|
||||
|
||||
// tile1.render(game.context, 'ninja-tiles');
|
||||
// tile2.render(game.context, 'ninja-tiles');
|
||||
|
||||
// game.debug.geom(sprite1.body, 'rgba(0,255,0,0.4)', true, 1);
|
||||
|
||||
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
|
||||
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ function create() {
|
||||
|
||||
sprite1 = game.add.sprite(50, 200, 'atari');
|
||||
sprite1.name = 'atari';
|
||||
// sprite1.anchor.set(0.5);
|
||||
|
||||
sprite2 = game.add.sprite(700, 220, 'mushroom');
|
||||
sprite2.name = 'mushroom';
|
||||
@@ -37,6 +38,9 @@ function update() {
|
||||
// object1, object2, collideCallback, processCallback, callbackContext
|
||||
game.physics.arcade.collide(sprite1, sprite2, collisionHandler, null, this);
|
||||
|
||||
// var b = sprite1.getBounds();
|
||||
// console.log(b);
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler (obj1, obj2) {
|
||||
@@ -46,9 +50,14 @@ function collisionHandler (obj1, obj2) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
function render() {
|
||||
|
||||
// game.debug.physicsBody(sprite1.body);
|
||||
// game.debug.physicsBody(sprite2.body);
|
||||
// var b = sprite1.getBounds();
|
||||
|
||||
// game.debug.geom(b, 'rgba(255,0,0,0.8)', true, 1);
|
||||
|
||||
game.debug.geom(sprite1.body, 'rgba(0,255,0,0.4)', true, 1);
|
||||
game.debug.geom(sprite2.body, 'rgba(0,255,0,0.4)', true, 1);
|
||||
|
||||
}
|
||||
@@ -52,7 +52,7 @@ function create() {
|
||||
|
||||
game.physics.enable(sprite);
|
||||
|
||||
sprite.body.setSize(14, 14, 2, 0);
|
||||
// sprite.body.setSize(14, 14, 2, 0);
|
||||
|
||||
console.log(sprite.body);
|
||||
|
||||
@@ -107,6 +107,8 @@ function render() {
|
||||
// game.debug.text(game.physics.arcade._intersection.width, 32, 32);
|
||||
// game.debug.text(game.physics.arcade._intersection.height, 32, 64);
|
||||
|
||||
game.debug.geom(sprite.body, 'rgba(0,255,0,0.4)', true, 1);
|
||||
|
||||
game.debug.text(sprite.body.overlapX, 32, 32);
|
||||
game.debug.text(sprite.body.overlapY, 32, 64);
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.tilemap('map', 'assets/tilemaps/maps/collision_test.json', null, Phaser.Tilemap.TILED_JSON);
|
||||
game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
|
||||
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
||||
// game.load.image('phaser', 'assets/sprites/phaser-ship.png');
|
||||
|
||||
}
|
||||
|
||||
var map;
|
||||
var layer;
|
||||
var cursors;
|
||||
var sprite;
|
||||
|
||||
function create() {
|
||||
|
||||
map = game.add.tilemap('map');
|
||||
|
||||
map.addTilesetImage('ground_1x1');
|
||||
|
||||
layer = map.createLayer('Tile Layer 1');
|
||||
|
||||
layer.resizeWorld();
|
||||
|
||||
map.setCollisionBetween(1, 12);
|
||||
|
||||
layer.debug = true;
|
||||
|
||||
sprite = game.add.sprite(260, 70, 'phaser');
|
||||
|
||||
game.physics.enable(sprite);
|
||||
|
||||
game.camera.follow(sprite);
|
||||
|
||||
// game.physics.arcade.gravity.y = 500;
|
||||
// sprite.body.velocity.x = 100;
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
game.physics.arcade.collide(sprite, layer);
|
||||
|
||||
sprite.body.velocity.x = 0;
|
||||
sprite.body.velocity.y = 0;
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
sprite.body.velocity.y = -100;
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
sprite.body.velocity.y = 100;
|
||||
}
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
sprite.body.velocity.x = -100;
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
sprite.body.velocity.x = 100;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.text(sprite.body.velocity.x, 32, 32);
|
||||
game.debug.text(sprite.body.velocity.y, 64, 32);
|
||||
|
||||
}
|
||||
@@ -21,6 +21,7 @@ function create() {
|
||||
// sprite = game.add.tileSprite(100, 100, 400, 300, 'starfield');
|
||||
|
||||
sprite = game.add.tileSprite(100, 100, 400, 300, 'mummy');
|
||||
sprite.pivot.setTo(200, 200);
|
||||
|
||||
sprite.animations.add('walk');
|
||||
|
||||
@@ -32,6 +33,8 @@ function create() {
|
||||
|
||||
function update() {
|
||||
|
||||
sprite.rotation += 0.01;
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
sprite.tilePosition.x += 8;
|
||||
|
||||
Reference in New Issue
Block a user