mirror of
https://github.com/wassname/phaser.git
synced 2026-08-12 12:20:38 +08:00
Tilemap.createCollisionObjects will parse Tiled data for objectgroups and convert polyline instances into physics objects you can collide with in the world.
After defining tiles that collide on a Tilemap, you need to call Tilemap.generateCollisionData(layer) to populate the physics world with the data required. Debug.renderPhysicsBody updated to take camera location and body rotation into account. Body movement functions put back to velocity :) Updated to latest dev version of pixi and latest p2.js Updated docs
This commit is contained in:
+32
-19
@@ -22,6 +22,7 @@ var p;
|
||||
var b;
|
||||
var cursors;
|
||||
var box2;
|
||||
var dump;
|
||||
|
||||
function create() {
|
||||
|
||||
@@ -34,14 +35,22 @@ function create() {
|
||||
map.addTilesetImage('tiles2');
|
||||
|
||||
layer = map.createLayer('Tile Layer 1');
|
||||
|
||||
layer.resizeWorld();
|
||||
|
||||
map.setCollisionBetween(1, 12);
|
||||
|
||||
// layer.debug = true;
|
||||
|
||||
dump = map.generateCollisionData(layer);
|
||||
|
||||
// dump = map.createCollisionObjects('collision');
|
||||
|
||||
// console.log(dump);
|
||||
// console.log(dump[7]);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ship = game.add.sprite(200, 200, 'ship');
|
||||
// ship = game.add.sprite(200, 200, 'ship');
|
||||
// ship.physicsEnabled = true;
|
||||
|
||||
|
||||
@@ -60,14 +69,13 @@ function create() {
|
||||
// b.body.fixedRotation = true;
|
||||
// b.body.static = true;
|
||||
|
||||
var data = [[0,0] , [0, -32] , [32, -64] , [64, -64] , [64,-96], [128,-96], [160,-128], [192,-128], [192,-96], [160,-64], [96,-64], [96,-32], [32,-32], [32,0]];
|
||||
// var data = [[0,0] , [0, -32] , [32, -64] , [64, -64] , [64,-96], [128,-96], [160,-128], [192,-128], [192,-96], [160,-64], [96,-64], [96,-32], [32,-32], [32,0], [0,0]];
|
||||
// b = game.physics.createBody(192, 352, 0, true, { removeCollinearPoints: true }, data);
|
||||
// b = game.physics.createBody(32, 32, 0, true, { removeCollinearPoints: true }, data);
|
||||
// b = game.physics.createBody(256, 480, 0, true, { removeCollinearPoints: true }, data);
|
||||
|
||||
// "x":192,
|
||||
// "y":352
|
||||
|
||||
b = game.physics.createBody(192, 352, 0, true, {}, data);
|
||||
|
||||
// console.log(output);
|
||||
// console.log('DATA');
|
||||
// console.log(data);
|
||||
|
||||
// var re = b.body.addPolygon({removeCollinearPoints:true}, data);
|
||||
// var re = b.body.addPolygon({removeCollinearPoints:true}, data);
|
||||
@@ -78,14 +86,12 @@ function create() {
|
||||
// console.log(b.body.data.shapes[0].vertices);
|
||||
|
||||
box2 = game.add.sprite(200, 200, 'box');
|
||||
box2.name = 'bob';
|
||||
box2.anchor.set(0.5);
|
||||
// box2.name = 'bob';
|
||||
// box2.anchor.set(0.5);
|
||||
box2.physicsEnabled = true;
|
||||
// box2.body.mass = 1;
|
||||
// box2.body.data.motionState = 1;
|
||||
box2.body.fixedRotation = true;
|
||||
|
||||
// game.camera.follow(p);
|
||||
game.camera.follow(box2);
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
@@ -150,13 +156,20 @@ function render() {
|
||||
|
||||
// game.debug.renderCameraInfo(game.camera, 420, 320);
|
||||
|
||||
game.debug.renderText(box2.body.velocity.x, 32, 32);
|
||||
game.debug.renderText(box2.body.velocity.y, 32, 64);
|
||||
// game.debug.renderText(box2.body.velocity.x, 32, 32);
|
||||
// game.debug.renderText(box2.body.velocity.y, 32, 64);
|
||||
|
||||
// game.debug.renderText(b.body.velocity.x, 32, 32);
|
||||
// game.debug.renderText(b.body.velocity.y, 32, 64);
|
||||
|
||||
game.debug.renderPhysicsBody(b);
|
||||
for (var i = 0, len = dump.length; i < len; i++)
|
||||
{
|
||||
game.debug.renderPhysicsBody(dump[i]);
|
||||
}
|
||||
|
||||
// game.debug.renderPhysicsBody(dump[0]);
|
||||
// game.debug.renderPhysicsBody(b);
|
||||
|
||||
// game.debug.renderPhysicsBody(ship.body);
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
// var game = new Phaser.Game(800, 600, Phaser.AUTO, '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('tiles2', 'assets/tilemaps/tiles/tiles2.png');
|
||||
game.load.image('ufo', 'assets/sprites/ufo.png');
|
||||
game.load.image('ship', 'assets/sprites/thrust_ship2.png');
|
||||
|
||||
}
|
||||
|
||||
var ship;
|
||||
var map;
|
||||
var layer;
|
||||
var cursors;
|
||||
var dump;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
map = game.add.tilemap('map');
|
||||
|
||||
map.addTilesetImage('ground_1x1');
|
||||
map.addTilesetImage('tiles2');
|
||||
|
||||
map.setCollisionBetween(1, 12);
|
||||
|
||||
layer = map.createLayer('Tile Layer 1');
|
||||
|
||||
layer.resizeWorld();
|
||||
|
||||
dump = map.generateCollisionData(layer);
|
||||
|
||||
ship = game.add.sprite(200, 200, 'ship');
|
||||
ship.physicsEnabled = true;
|
||||
// We do this because our ship is shaped like a triangle, not a square :)
|
||||
ship.body.addPolygon({}, 29, 23 , 0, 23 , 14, 1);
|
||||
|
||||
game.camera.follow(ship);
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
ship.body.rotateLeft(100);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
ship.body.rotateRight(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
ship.body.setZeroRotation();
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
ship.body.thrust(400);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
ship.body.reverse(400);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
for (var i = 0, len = dump.length; i < len; i++)
|
||||
{
|
||||
// game.debug.renderPhysicsBody(dump[i]);
|
||||
}
|
||||
|
||||
// game.debug.renderPhysicsBody(ship.body);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user