How do I prevent diagonal movement?

0 favourites
  • 6 posts
From the Asset Store
Run and Jump in 3 Dimensions! Take your platformer to the next level!
  • Hi,

    I am unable to restrict diagonal movements. I am attaching part of the project below so that you can access and give me some ideas.

    Thanks

    https://www.dropbox.com/s/ry61lrpa1a0h324/puzzle_test.capx

    Obs.: This project use LiteTween plugin

  • I downloaded your project and took a look. After the player selects the second tile, you are not doing any validation at all to see if the second is valid to select. If you want to disallow diagonal movement, you need to test the second selection to see if it valid by adding a condition on the event that moves the tiles. This could be as easy as adding a test like this:

    if (selected1.x = selected2.x and selected1.y != selected2.y)

    or

    (selected1.y = selected2.y and selected1.x != selected2.x)

    The entire above block of pseudo code needs to be set as a condition to move the block. Basically it says only move the pieces if either the x values or the y values or different but not both (XOR). You would of course have to figure out how to model it in Construct. This block of code will also work even if the size of the puzzle was larger than 2 by 2.

    Of course this is only 1 way to handle this kind of situation. As it is, you have colored blocks you are placing along the vertical and horizontal lines. You could make sure at least 1 of your tiles is on a block which also contains the color of the other block. This would also have to be set as a condition on the move event. You don't need to check both selected tiles because if 1 is on a color the other will always be as well. This is a rather ugly way to do it, but may work better for you. Especially if you intend to extend the board out and only allow movement to the square directly adjacent to the selected tile.

    I hope one of these solutions works for you and good luck with your project.

  • Hi ,

    Thanks for helping. I understood the logic but I need some help to implement it.I think it is okay to use variables that hold position values (Pos1X/Pos1Y/Pos2X/Pos2Y) to test the second selection, right? But I do not know what event I need to use.

    Other question: is possible gather AND and OR in the same event?

    Thanks

  • Please, any more suggestions or explanations concerning the 's suggestion? Thanks.

  • OK, so I took some time to modify your project and add what I had suggested. To be honest, I don't completely follow all of your logic, so I didn't do any optimizations like I usually would. I did however, have to move around a few events to get it working. Take a look at the changes and test it out. You may be able to run with it and make it much more streamlined with your methods.

    As for your question... Yes, you can combine and/or logic in 1 event. Take a look at event 5. I embedded a simple if/else statement to a set of conditions and wrapped them with an or block. If you don't understand the statement, do a search for "ternary operator" without the quotes. This is a common programming operation and can be used in Construct to simplify a set of if/else events into a single event. Simply put, the first condition says:

    if Pos1X equals Pos2X and Pos1Y does not equal Pos2Y then return "True" if not return "False"

    The value returned is then tested against the "True" string in the first value. There are many other ways this could have been accomplished but, I have been on a ternary operator kick in my examples lately, so that is what you get.

    I hope that clears things up for you and good luck with your project.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hi

    Thank you very much. It's exactly that I need.

    I tried to use "compare two values" event but I didn't know how to organize the code. It's really simple after someone does, mainly with this excellent explanation.

    I believe many people will benefit from this solution.

    Thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)