Phaser MouseFollow
by paynterc
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.24.1/phaser.min.js"></script>
<div id="test"></div>
JavaScript
// To access game: this.sys.game
const MAX_SPEED = 500; // pixels/second
const DRAG = 1200; // pixels/second
const GRAVITY = 2600; // pixels/second/second
const JUMP_SPEED = -1000; // pixels/second (negative y is up)
const ACCELERATION = 600; // pixels/second/second
const PLATFORM_DELAY = 3200;// milliseconds
const W = 800;
const H = 400;
var config = {
type: Phaser.AUTO,
width: W,
height: H,
parent: 'test',
scene: {
preload: preload,
create: create,
update: update
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: true
}
},
};
var game = new Phaser.Game(config);
function preload ()
{
this.load.image('dude', 'https://i.postimg.cc/tCMhjzhF/Snowman.png');
this.load.image('block', 'https://i.postimg.cc/TPcRgfJG/Block.png');
}
function create ()
{
/* gameWidth = this.sys.game.scale.gameSize._width;
gameHeight = this.sys.game.scale.gameSize._height; */
this.player = this.physics.add.sprite(W/2, H/2, 'dude');
this.player.setBounce(0.2);
this.player.body.maxVelocity.setTo(MAX_SPEED, MAX_SPEED * 10); // x, y
this.player.body.drag.setTo(DRAG, 0); // x, y
//break;
let r2 = this.add.rectangle(W/2, H/2, W, H);
r2.setStrokeStyle(7, 0xefc53f);
this.cameras.main.startFollow(this.player);
this.angleText = this.add.text(this.player.x, this.player.y-32, "GAME OVER", { fontSize: '16px'});
this.angleText.setOrigin(0.5);
this.input.on('pointermove', function (pointer) {
//converting canvas coordinates to world coordinates
const transformedPoint = this.cameras.main.getWorldPoint(pointer.x, pointer.y);
//slime follows the mouse pointer
//this.physics.moveToObject(this.player, transformedPoint, 240);
let Rad = Phaser.Math.Angle.BetweenPoints(this.player, transformedPoint);
//let RadNorm = Phaser.Math.Angle.Normalize(Rad);
let Deg = Phaser.Math.RadToDeg(Rad);
if(Deg >= -70 && Deg <= 70 )
{
...