[Solved]How do I move an object between 3 different points?

0 favourites
  • 11 posts
From the Asset Store
Connect the dots in the correct order and draw happy animals!
  • So I'm struggling with this. I want an object to move between 3 different points on the X axis, like this: * * *

    The object starts in the middle, then if I press the left arrow key it will snap to the left point. If i press the right arrow key it will snap to the middle and if i press right again it will snap to the right and vice versa. It's a simple mechanic but I'm struggling to make this work properly. I tried using the MoveTo plugin by REX and it works between 2 points but I can't for the life of me, figure out how to make it work seamlessly between 3 points. I have no idea what the best pracises are for these kinds of mechanics.. Any help is greatly appreciated!

  • So I'm struggling with this. I want an object to move between 3 different points on the X axis, like this: * * *

    The object starts in the middle, then if I press the left arrow key it will snap to the left point. If i press the right arrow key it will snap to the middle and if i press right again it will snap to the right and vice versa. It's a simple mechanic but I'm struggling to make this work properly. I tried using the MoveTo plugin by REX and it works between 2 points but I can't for the life of me, figure out how to make it work seamlessly between 3 points. I have no idea what the best pracises are for these kinds of mechanics.. Any help is greatly appreciated!

    There are lots of ways to do this, but the easiest might be to simply make a variable in your object called "Position" or something. This variable then hold the current position of the object. Like so:

    (X = 1) (X = 2) (X = 3)

    Then in your object you simply check what value the position is, when you press a key and add or subtract based on the key pressed.

    So if the object starts in the middle the "Position" variable is 2. Pressing Left key you subtract 1 from Position and if its the Right key you add 1.

    Then you can make 1 small sprite object (Named location) and give it an index variable and make 3 instances of it, each with a different index that relate to the position, so it would be 1 for the left one, 2 for the middle and 3 for the right one, then you can use those to easily control the position of the object, and so you can move it around later if you need to without having to change any code.

    And then you add something like this:

    Function Update_Object_position
    Pick Location.Index = Object.Position
    Set Object.X/Y to Location.X/Y
    [/code:1h4ielcw]
    
    Then you ofc just have to add the keys to add and subtract from object.position and make sure that position cant go below 1 or above 3, and call the function each time one of the keys are pressed.
  • Thanks alot for taking the time to answer! This looks like a logical way to do it.

    I will try to figure this out as soon as I get home from work!

  • Bullet behavior is useful in like this situations

    https://drive.google.com/file/d/0BzXd1GMzUo9HT1JZUDNlYWM5UEk/view?usp=sharing

  • Hey thanks alot, this is almost the behavior I am looking for! This really helps me understand the logic behind it.

    How would I go about making them snap to the locations instantly instead of the bullet speed though?

    I tried implimenting nimos100 solution and looking at yours as a reference as to how things are set up, but I can't get it to work

    Forum had some problems so I couldn't post any reply. Seems to be working now!

  • Hey thanks alot, this is almost the behavior I am looking for! This really helps me understand the logic behind it.

    How would I go about making them snap to the locations instantly instead of the bullet speed though?

    I tried implimenting nimos100 solution and looking at yours as a reference as to how things are set up, but I can't get it to work

    Forum had some problems so I couldn't post any reply. Seems to be working now!

    The idea I posted doesn't use bullet behaviour, it just moves the object to another object position. You could do it without and just change the coordinate (X,Y) to where you want your object to go to. But using 3 small sprites and just move the object to one of those depending on the index, you can move them around screen and the object will always go to the correct position. So its simply so you can make the functionality one time and then you don't have to keep updating the X,Y whenever you want to move them. If you want it to move instantly as I understand you want, you don't need the bullet behaviour, think that will only mess things up.

  • No I know, the one mrcgkh provided is with the bullet behavior though and it's not entirely what I'm looking for. It should only set the coordinate like the solution you provided so I'm trying to do that now though I'm not sure how I go about making the function you provided.

    Thanks for your help, I will read up some more in the manual and try again with your solution.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • mrcgkh Hey thanks alot, this is exactly what I'm looking for. You didn't have to do it for me though but I really appreciate it!

    Cheers!

  • Sometimes Arrays can be confusing. Here's a non-array version

    https://drive.google.com/file/d/0BzXd1GMzUo9Ha0ZZM0Y5ckdPUWc/view?usp=sharing

  • mrcgkh

    Wow you are great. Thanks a bunch! Nice to see how things are laid out. I can see now how arrays can be useful

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