Jump to content

moveToXY in conjunction with a mouse click


economist
 Share

Recommended Posts

Hello,

 

I am quiet new to javascript and, in particular, to the phaser-framework. I would like to develop a time management game where the token has to move to a certain point on the screen. This movement as well as the destination should be determined by a mouse click (not holding down, just a single click).

 

My initial guess was to use moveToXY. However, I have not been able to start the movement of the sprite, when the player marks a destination on the screen by clicking on it. I have just been able to make the sprite following the pointer.

 

I hope I did a sufficient search and did not miss an answer.

 

 

Many thanks in advance for your help!

Link to comment
Share on other sites

Here you go, drop this into the examples/wip folder and run it in a browser:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });function preload() {    game.load.image('arrow', 'assets/sprites/arrow.png');}var sprite;var tween;function create() {    sprite = game.add.sprite(32, 32, 'arrow');    sprite.anchor.setTo(0.5, 0.5);    game.input.onDown.add(moveSprite, this);}function moveSprite (pointer) {    if (tween && tween.isRunning)    {        tween.stop();    }    sprite.rotation = game.physics.angleToPointer(sprite, pointer);    //  300 = 300 pixels per second = the speed the sprite will move at, regardless of the distance it has to travel    var duration = (game.physics.distanceToPointer(sprite, pointer) / 300) * 1000;    tween = game.add.tween(sprite).to({ x: pointer.x, y: pointer.y }, duration, Phaser.Easing.Linear.None, true);}
Link to comment
Share on other sites

  • 3 months later...
  • 8 months later...

Hello, i am trying to use this function but i have a problem.
Sprite moves after clicking but do not go to the target what i click and does not colid physically with other objects.
Can anyone help me to do it? I try and I try and not working..

You can check how it work on my page: http://www.lifetime.cba.pl

My code looks like this:
 

var TopDownGame = TopDownGame || {};TopDownGame.level = 'podworko';//title screen var tween;TopDownGame.Game = function(){};TopDownGame.Game.prototype = { create: function() {       .........       ........var result = this.findObjectsByType('playerStart', this.map, 'objectsLayer')    this.player = this.game.add.sprite(result[0].x, result[0].y, 'player');    this.game.physics.arcade.enable(this.player);    this.player.anchor.setTo(0.5, 0.5);        this.game.input.onDown.add(moveSprite, this);},update: function(){.....},moveSprite: function(pointer) {    if (tween && tween.isRunning)    {        tween.stop();    }    this.player.rotation = game.physics.angleToPointer(this.player, pointer);    //  300 = 300 pixels per second = the speed the sprite will move at, regardless of the distance it has to travel    var duration = (game.physics.distanceToPointer(this.player, pointer) / 300) * 1000;    tween = game.add.tween(this.player).to({ x: pointer.x, y: pointer.y }, duration, Phaser.Easing.Linear.None, true);},

I found one definition i other topic but i dont kno how to use it.:

lewster32 wrote:

moveToXY sets the velocity of an Arcade physics enabled body so that it moves towards the specified point. It does not try to stop the object, but it is a convenience function so that you don't have to do the maths to make an object move somewhere via velocity.

 

If you set body.moves to false, you can instead move the sprite by tweens which is highly performant, 100% accurate and will stop exactly where you tell it to. The drawback is that an object that uses physics for its movement relies on its velocity to interact with other physical objects, so collisions and so on get a bit weird. Also, if you try to move a physics-enabled Sprite without setting body.moves to false, you'll get all kinds of odd effects such as vibrating and warping as the two different systems fight over where the Sprite should be in any given frame.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...