mirror of
https://github.com/wassname/phaser.git
synced 2026-06-27 16:10:15 +08:00
Testing map collision
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.tilemap('level3', 'assets/tilemaps/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
|
||||
game.load.image('tiles', 'assets/tilemaps/tiles/cybernoid.png', 16, 16);
|
||||
game.load.image('phaser', 'assets/sprites/phaser-ship.png');
|
||||
game.load.image('chunk', 'assets/sprites/chunk.png');
|
||||
|
||||
}
|
||||
|
||||
var map;
|
||||
var layer;
|
||||
var cursors;
|
||||
var sprite;
|
||||
var emitter;
|
||||
|
||||
function create() {
|
||||
|
||||
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
|
||||
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
|
||||
map = game.add.tilemap('level3');
|
||||
|
||||
map.addTilesetImage('CybernoidMap3BG_bank.png', 'tiles');
|
||||
|
||||
layer = map.createLayer(0);
|
||||
|
||||
// Basically this sets EVERY SINGLE tile to fully collide on all faces
|
||||
map.setCollisionByExclusion([7, 32, 35, 36, 47]);
|
||||
|
||||
layer.debug = true;
|
||||
|
||||
layer.resizeWorld();
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
/*
|
||||
emitter = game.add.emitter(0, 0, 200);
|
||||
|
||||
emitter.makeParticles('chunk');
|
||||
emitter.minRotation = 0;
|
||||
emitter.maxRotation = 0;
|
||||
emitter.gravity = 150;
|
||||
emitter.bounce.setTo(0.5, 0.5);
|
||||
*/
|
||||
|
||||
sprite = game.add.sprite(200, 70, 'phaser');
|
||||
sprite.anchor.setTo(0.5, 0.5);
|
||||
|
||||
game.physics.enable(sprite);
|
||||
|
||||
game.camera.follow(sprite);
|
||||
|
||||
}
|
||||
|
||||
function particleBurst() {
|
||||
|
||||
// emitter.x = sprite.x;
|
||||
// emitter.y = sprite.y;
|
||||
// emitter.start(true, 2000, null, 1);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
game.physics.arcade.collide(sprite, layer);
|
||||
game.physics.arcade.collide(emitter, layer);
|
||||
|
||||
sprite.body.velocity.x = 0;
|
||||
sprite.body.velocity.y = 0;
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
sprite.body.velocity.y = -150;
|
||||
// particleBurst();
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
sprite.body.velocity.y = 150;
|
||||
// particleBurst();
|
||||
}
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
sprite.body.velocity.x = -150;
|
||||
sprite.scale.x = -1;
|
||||
// particleBurst();
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
sprite.body.velocity.x = 150;
|
||||
sprite.scale.x = 1;
|
||||
// particleBurst();
|
||||
}
|
||||
|
||||
}
|
||||
+28
-38
@@ -188,31 +188,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
*/
|
||||
this.skipQuadTree = false;
|
||||
|
||||
// Allow collision
|
||||
|
||||
/**
|
||||
* Set the allowCollision properties to control which directions collision is processed for this Body.
|
||||
* For example allowCollision.up = false means it won't collide when the collision happened while moving up.
|
||||
* @property {object} allowCollision - An object containing allowed collision.
|
||||
*/
|
||||
// This would be faster as an array
|
||||
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
|
||||
|
||||
/**
|
||||
* This object is populated with boolean values when the Body collides with another.
|
||||
* touching.up = true means the collision happened to the top of this Body for example.
|
||||
* @property {object} touching - An object containing touching results.
|
||||
*/
|
||||
// This would be faster as an array
|
||||
this.touching = { none: true, up: false, down: false, left: false, right: false };
|
||||
|
||||
/**
|
||||
* This object is populated with previous touching values from the bodies previous collision.
|
||||
* @property {object} wasTouching - An object containing previous touching results.
|
||||
*/
|
||||
// This would be faster as an array
|
||||
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
|
||||
|
||||
/**
|
||||
* @property {number} facing - A const reference to the direction the Body is traveling or facing.
|
||||
* @default
|
||||
@@ -301,7 +276,32 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
*/
|
||||
this.collideWorldBounds = false;
|
||||
|
||||
this.blocked = { up: false, down: false, left: false, right: false };
|
||||
/**
|
||||
* Set the checkCollision properties to control which directions collision is processed for this Body.
|
||||
* For example checkCollision.up = false means it won't collide when the collision happened while moving up.
|
||||
* @property {object} checkCollision - An object containing allowed collision.
|
||||
*/
|
||||
this.checkCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
|
||||
|
||||
/**
|
||||
* This object is populated with boolean values when the Body collides with another.
|
||||
* touching.up = true means the collision happened to the top of this Body for example.
|
||||
* @property {object} touching - An object containing touching results.
|
||||
*/
|
||||
this.touching = { none: true, up: false, down: false, left: false, right: false };
|
||||
|
||||
/**
|
||||
* This object is populated with previous touching values from the bodies previous collision.
|
||||
* @property {object} wasTouching - An object containing previous touching results.
|
||||
*/
|
||||
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
|
||||
|
||||
/**
|
||||
* This object is populated with boolean values when the Body collides with the World bounds or a Tile.
|
||||
* For example if blocked.up is true then the Body cannot move up.
|
||||
* @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any.
|
||||
*/
|
||||
this.blocked = { x: 0, y: 0, up: false, down: false, left: false, right: false };
|
||||
|
||||
};
|
||||
|
||||
@@ -363,8 +363,8 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.y = this.preY;
|
||||
this.rotation = this.preRotation;
|
||||
|
||||
// this.overlapX = 0;
|
||||
// this.overlapY = 0;
|
||||
this.overlapX = 0;
|
||||
this.overlapY = 0;
|
||||
|
||||
this.blocked.up = false;
|
||||
this.blocked.down = false;
|
||||
@@ -391,16 +391,6 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
*/
|
||||
postUpdate: function () {
|
||||
|
||||
// if (this.overlapX !== 0)
|
||||
// {
|
||||
// this.x -= this.overlapX;
|
||||
// }
|
||||
|
||||
// if (this.overlapY !== 0)
|
||||
// {
|
||||
// this.y -= this.overlapY;
|
||||
// }
|
||||
|
||||
if (this.deltaX() < 0 && this.blocked.left === false)
|
||||
{
|
||||
this.facing = Phaser.LEFT;
|
||||
|
||||
+524
-327
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user