phaser plugin to move an object and stop it at an exact position. Compatible with phaser >= 2.0.0 <3.0.0
As I didn't find a way in the phaser core functions to move an item to stop at a given position, I made this plugin. It just wrap the phaser core function moveToXY
to have the displayed object stop when the x/y position is reached.
$ npm install phaser-move-and-stop-plugin
First add the plugin to game
in your main create()
function of your phaser state:
import MoveAndStopPlugin from "phaser-move-and-stop-plugin";
export default game => ({
create: () => {
game.moveAndStop = game.plugins.add(MoveAndStopPlugin);
},
update: () => {
//...
}
});
Then call one the the following api:
It it same as phaserJS moveToXY except that the object will stop at the exact x/y position.
- [
displayObject(state, [ownProps]): any
]: The display object to move. - [
x: number
]: The x coordinate to reach. - [
y: number
]: The y coordinate to reach. - [
speed: number (optional)
]: The speed it will move, in pixels per second (default is 60 pixels/sec) - [
maxTime: number (optional)
]: Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms. - [
events: object (optional)
]: List of events to trigger.
game.moveAndStop.toXY(item, x, y, 5000, null, {
onStopped: () => {
console.log('stopped before reaching position!');
},
onPositionReached: () => {
console.log('position reached!');
}
});
It it same as phaserJS moveToObject except that the object will stop at the exact destination position.
- [
displayObject(state, [ownProps]): any
]: The display object to move. - [
destination: any
]: The display object to move towards. Can be any object but must have visible x/y properties. - [
speed: number (optional)
]: The speed it will move, in pixels per second (default is 60 pixels/sec) - [
maxTime: number (optional)
]: Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms. - [
events: object (optional)
]: List of events to trigger.
game.moveAndStop.toObject(item, dest, 5000, null, {
onPositionReached: () => {
console.log('position reached!');
}
});
Stop the object
return true
is the object is moving
Function called when the object stopped. This function is call if the object stopped at the targetted position or stopped before reaching it.
Function called when the object reach the targetted position