Skip to content

Commit 12a2a2e

Browse files
committed
Testing map collision
1 parent 22b1ce9 commit 12a2a2e

3 files changed

Lines changed: 615 additions & 331 deletions

File tree

examples/wip/sci-fly2.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.tilemap('level3', 'assets/tilemaps/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
7+
game.load.image('tiles', 'assets/tilemaps/tiles/cybernoid.png', 16, 16);
8+
game.load.image('phaser', 'assets/sprites/phaser-ship.png');
9+
game.load.image('chunk', 'assets/sprites/chunk.png');
10+
11+
}
12+
13+
var map;
14+
var layer;
15+
var cursors;
16+
var sprite;
17+
var emitter;
18+
19+
function create() {
20+
21+
// 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).
22+
// 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.
23+
map = game.add.tilemap('level3');
24+
25+
map.addTilesetImage('CybernoidMap3BG_bank.png', 'tiles');
26+
27+
layer = map.createLayer(0);
28+
29+
// Basically this sets EVERY SINGLE tile to fully collide on all faces
30+
map.setCollisionByExclusion([7, 32, 35, 36, 47]);
31+
32+
layer.debug = true;
33+
34+
layer.resizeWorld();
35+
36+
cursors = game.input.keyboard.createCursorKeys();
37+
38+
/*
39+
emitter = game.add.emitter(0, 0, 200);
40+
41+
emitter.makeParticles('chunk');
42+
emitter.minRotation = 0;
43+
emitter.maxRotation = 0;
44+
emitter.gravity = 150;
45+
emitter.bounce.setTo(0.5, 0.5);
46+
*/
47+
48+
sprite = game.add.sprite(200, 70, 'phaser');
49+
sprite.anchor.setTo(0.5, 0.5);
50+
51+
game.physics.enable(sprite);
52+
53+
game.camera.follow(sprite);
54+
55+
}
56+
57+
function particleBurst() {
58+
59+
// emitter.x = sprite.x;
60+
// emitter.y = sprite.y;
61+
// emitter.start(true, 2000, null, 1);
62+
63+
}
64+
65+
function update() {
66+
67+
game.physics.arcade.collide(sprite, layer);
68+
game.physics.arcade.collide(emitter, layer);
69+
70+
sprite.body.velocity.x = 0;
71+
sprite.body.velocity.y = 0;
72+
73+
if (cursors.up.isDown)
74+
{
75+
sprite.body.velocity.y = -150;
76+
// particleBurst();
77+
}
78+
else if (cursors.down.isDown)
79+
{
80+
sprite.body.velocity.y = 150;
81+
// particleBurst();
82+
}
83+
84+
if (cursors.left.isDown)
85+
{
86+
sprite.body.velocity.x = -150;
87+
sprite.scale.x = -1;
88+
// particleBurst();
89+
}
90+
else if (cursors.right.isDown)
91+
{
92+
sprite.body.velocity.x = 150;
93+
sprite.scale.x = 1;
94+
// particleBurst();
95+
}
96+
97+
}

src/physics/arcade/Body.js

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
188188
*/
189189
this.skipQuadTree = false;
190190

191-
// Allow collision
192-
193-
/**
194-
* Set the allowCollision properties to control which directions collision is processed for this Body.
195-
* For example allowCollision.up = false means it won't collide when the collision happened while moving up.
196-
* @property {object} allowCollision - An object containing allowed collision.
197-
*/
198-
// This would be faster as an array
199-
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
200-
201-
/**
202-
* This object is populated with boolean values when the Body collides with another.
203-
* touching.up = true means the collision happened to the top of this Body for example.
204-
* @property {object} touching - An object containing touching results.
205-
*/
206-
// This would be faster as an array
207-
this.touching = { none: true, up: false, down: false, left: false, right: false };
208-
209-
/**
210-
* This object is populated with previous touching values from the bodies previous collision.
211-
* @property {object} wasTouching - An object containing previous touching results.
212-
*/
213-
// This would be faster as an array
214-
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
215-
216191
/**
217192
* @property {number} facing - A const reference to the direction the Body is traveling or facing.
218193
* @default
@@ -301,7 +276,32 @@ Phaser.Physics.Arcade.Body = function (sprite) {
301276
*/
302277
this.collideWorldBounds = false;
303278

304-
this.blocked = { up: false, down: false, left: false, right: false };
279+
/**
280+
* Set the checkCollision properties to control which directions collision is processed for this Body.
281+
* For example checkCollision.up = false means it won't collide when the collision happened while moving up.
282+
* @property {object} checkCollision - An object containing allowed collision.
283+
*/
284+
this.checkCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
285+
286+
/**
287+
* This object is populated with boolean values when the Body collides with another.
288+
* touching.up = true means the collision happened to the top of this Body for example.
289+
* @property {object} touching - An object containing touching results.
290+
*/
291+
this.touching = { none: true, up: false, down: false, left: false, right: false };
292+
293+
/**
294+
* This object is populated with previous touching values from the bodies previous collision.
295+
* @property {object} wasTouching - An object containing previous touching results.
296+
*/
297+
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
298+
299+
/**
300+
* This object is populated with boolean values when the Body collides with the World bounds or a Tile.
301+
* For example if blocked.up is true then the Body cannot move up.
302+
* @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any.
303+
*/
304+
this.blocked = { x: 0, y: 0, up: false, down: false, left: false, right: false };
305305

306306
};
307307

@@ -363,8 +363,8 @@ Phaser.Physics.Arcade.Body.prototype = {
363363
this.y = this.preY;
364364
this.rotation = this.preRotation;
365365

366-
// this.overlapX = 0;
367-
// this.overlapY = 0;
366+
this.overlapX = 0;
367+
this.overlapY = 0;
368368

369369
this.blocked.up = false;
370370
this.blocked.down = false;
@@ -391,16 +391,6 @@ Phaser.Physics.Arcade.Body.prototype = {
391391
*/
392392
postUpdate: function () {
393393

394-
// if (this.overlapX !== 0)
395-
// {
396-
// this.x -= this.overlapX;
397-
// }
398-
399-
// if (this.overlapY !== 0)
400-
// {
401-
// this.y -= this.overlapY;
402-
// }
403-
404394
if (this.deltaX() < 0 && this.blocked.left === false)
405395
{
406396
this.facing = Phaser.LEFT;

0 commit comments

Comments
 (0)