shtanton

Tweens overlapping bug

Recommended Posts

I'm making a top down game where the player moves smoothly around the plane but is confined to a 64x64 grid, I have achieved this by allowing them to move if they are exactly positioned on one of the 64x64 tiles and if they are, the game tweens them to the appropriate tile.  The problem is that despite my best efforts it is still possible to move diagonally which would cause a lot of bugs in my collision code.  This is the code I am using

update: function () {
    if (player.x%64 === 0 && player.y%64 === 0) {
        if (game.input.keyboard.isDown(Phaser.KeyCode.W)) {
            game.add.tween(player).to({y: player.y-64}, 250, Phaser.Easing.Linear.None, true);
        } else if (game.input.keyboard.isDown(Phaser.KeyCode.S)) {
            game.add.tween(player).to({y: player.y+64}, 250, Phaser.Easing.Linear.None, true);
        } else if (game.input.keyboard.isDown(Phaser.KeyCode.A)) {
            game.add.tween(player).to({x: player.x-64}, 250, Phaser.Easing.Linear.None, true);
        } else if (game.input.keyboard.isDown(Phaser.KeyCode.D)) {
            game.add.tween(player).to({x: player.x+64}, 250, Phaser.Easing.Linear.None, true);
        }
    }
},

Any help would be great! Thanks in advance

Share this post


Link to post
Share on other sites

I'd suggest add a state to the Player that checks if it's currently moving. Until it's finished don't allow any more Tweens to be added.

The tween.onComplete.add(function() {}) can be used to reset the player state.

Share this post


Link to post
Share on other sites
9 hours ago, VitaZheltyakov said:

It is bad practice to use animation instead of physics. Use this example:

http://phaser.io/examples/v2/arcade-physics/bounce-knock

I couldn't find a method that interacted with my map the way I intended it to, I have a layer in the json map called hitboxes that either has 0 or a value for whether the sprite should collide with it, if you have a way that phaser can do this with the built in physics and still stay in the 64x64 grid alignment then I'm open to suggestions, thanks!

Share this post


Link to post
Share on other sites
2 hours ago, shtanton said:

I couldn't find a method that interacted with my map the way I intended it to, I have a layer in the json map called hitboxes that either has 0 or a value for whether the sprite should collide with it, if you have a way that phaser can do this with the built in physics and still stay in the 64x64 grid alignment then I'm open to suggestions, thanks!

Look at the player moves as in the example. This is what you need.

Share this post


Link to post
Share on other sites
6 hours ago, VitaZheltyakov said:

Look at the player moves as in the example. This is what you need.

The example still allows the player to move diagonally which is what I'm trying to avoid, I don't know why the "else if" statements aren't doing this

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.