JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdn.jsdelivr.net/npm/phaser-ce@2.8.0/build/phaser.js"></script>
JavaScript
var game = new Phaser.Game(900, 900, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image("player","https://www.gravatar.com/avatar/017fed8cf1a108757768777abb722c60?s=32&d=identicon&r=PG");
game.load.image("box","https://www.gravatar.com/avatar/6678edff0ca08a68d194aec960d894d2?s=32&d=identicon&r=PG&f=1");
}
var player;
var cursors;
function create() {
game.physics.startSystem(Phaser.Physics.P2JS);
game.physics.p2.restitution = 0;
player = game.add.sprite(150,150,'player');
box = game.add.sprite(200, 200,'box');
box.width = 100;
box.height = 50;
game.physics.p2.world.setGlobalStiffness(1e7);
game.physics.p2.world.solver.tolerance = 0;
game.physics.p2.enable([ player,box ], false);
player.body.fixedRotation = true;
box.body.mass = 0.3;
box.body.damping = 1;
box.body.fixedRotation = true;
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
player.body.setZeroVelocity();
if (cursors.left.isDown)
{
player.body.moveLeft(200);
}
else if (cursors.right.isDown)
{
player.body.moveRight(200);
}
if (cursors.up.isDown)
{
player.body.moveUp(200);
}
else if (cursors.down.isDown)
{
player.body.moveDown(200);
}
}
function render() {
//game.debug.spriteInfo(box, 50, 50);
//game.debug.spriteBounds(player);
}