P4nch0 Posted January 3, 2015 Share Posted January 3, 2015 Hello, i am trying to do mouse move in ma game. I tried using 2 functions moveToXY and tweens. In both functions 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 on tween function: 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(){.....},tween : false,moveSprite : function(pointer) { if (this.tween && this.tween.isRunning) { this.tween.stop(); } this.tween = this.game.add.tween(this.player).to({ x: pointer.x, y: pointer.y }, 500, Phaser.Easing.Linear.None, true);},And like this in movetoXY function : 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) { this.game.physics.arcade.moveToXY(this.player, pointer.x, pointer.y, 60, 0); },Anyone have an idea how i can do mouse movement? I can't find a clear method. Thanks in Advance! Link to comment Share on other sites More sharing options...
P4nch0 Posted January 3, 2015 Author Share Posted January 3, 2015 My friends Written founded solution. Its moving sprite to point where we click, but dont colide with objects, any idea why?Must change :this.tween = this.game.add.tween(this.player).to({ x: pointer.x, y: pointer.y }, 500, Phaser.Easing.Linear.None, true);to this: this.tween = this.game.add.tween(this.player).to({ x: pointer.x + this.game.camera.x, y: pointer.y + this.game.camera.y }, 500, Phaser.Easing.Linear.None, true); Link to comment Share on other sites More sharing options...
P4nch0 Posted January 4, 2015 Author Share Posted January 4, 2015 Hi, i read about tween sprite dont collide with other objects and i dont know why..Really anyone dont know? Meybe i can to tell more about problem? Have any idea? Please help, i am trying a lot of time to solve this problem and i cant move on.. I will be very grateful Link to comment Share on other sites More sharing options...
spencerTL Posted January 4, 2015 Share Posted January 4, 2015 Tween movement isn't compatible with physics based movement - it either won't work or you'll get strange effects. To move in a way that physics would work you need to move things by giving them a velocity - they must move within the physics engine not outside of it as tweens do. I could be wrong, and others can correct me on this, but I think for the sort of game shown in your demo you'd be better off using a tile based movement. For example click on a tile, if it is a valid destination use a path finding algorithm to move the sprite to it. You're already using tiles for the background so it is just a case of working out which ones represent a non valid square. This aspect is shown by this example: http://examples.phaser.io/_site/view_full.html?d=tilemaps&f=csv+map+collide.js&t=csv%20map%20collide there are some others also showing how to handle collisions in slightly different ways. Link to comment Share on other sites More sharing options...
P4nch0 Posted January 4, 2015 Author Share Posted January 4, 2015 Thanks for answer, Yes i know aobout move using velocity, But I really want to do mouse movement in my game and i dont have idea how.When i try normal method movetoXY my player sprite dont go to he point where i click. Meybe you know hwat is the best way to do mouse movement i my game? Meybe You Can tell me how to use this movetoXY method? Thanks in advance. Link to comment Share on other sites More sharing options...
spencerTL Posted January 4, 2015 Share Posted January 4, 2015 I'm not able to try out at the moment to see but could it be that you need to use pointer.world.x (same for y) as the destination in the moveToXY function? Link to comment Share on other sites More sharing options...
P4nch0 Posted January 4, 2015 Author Share Posted January 4, 2015 nope, when i do that it isnt working. Now on my page I have uploaded the movetoXY of my first post and You can try working. I dont have idea why it isnt go to the point.On the other hand can You tell me where we can use tween when it ist working with normal collide? meybe i can change something to use this tween method or do something to make method movetoxy working. I dont have any idea but i really want to do it.. Link to comment Share on other sites More sharing options...
pwnies Posted January 8, 2015 Share Posted January 8, 2015 You probably want some sort of grid based movement rather than using tweens or moveToXY. Look into A* algorithms. https://github.com/appsbu-de/phaser_plugin_pathfinding looks like it provides what you need. This will allow characters to find correct paths around objects. Link to comment Share on other sites More sharing options...
Recommended Posts