economist Posted December 10, 2013 Share Posted December 10, 2013 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 More sharing options...
rich Posted December 10, 2013 Share Posted December 10, 2013 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);} shawnbless and turzifer 2 Link to comment Share on other sites More sharing options...
economist Posted December 11, 2013 Author Share Posted December 11, 2013 Thank you very much, Rich! Link to comment Share on other sites More sharing options...
babcock Posted April 9, 2014 Share Posted April 9, 2014 Does this example work with Phaser 2.0? I get the arrow but no movement.movetoclick_nw.html Link to comment Share on other sites More sharing options...
rich Posted April 10, 2014 Share Posted April 10, 2014 It will work if you change:game.physics.angleToPointerto:game.physics.arcade.angleToPointer(and anywhere else it has just 'game.physics' it now needs to be 'game.physics.arcade'. Link to comment Share on other sites More sharing options...
P4nch0 Posted December 30, 2014 Share Posted December 30, 2014 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.plMy 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. OscarBraindeaD 1 Link to comment Share on other sites More sharing options...
Recommended Posts