How do I move an object and stop it when it reaches another?

0 favourites
  • 12 posts
From the Asset Store
This is a code so you can see the same skin on all screens (peers). Each peer gets a skin, in addition to 4 directions t
  • Hey guys. I'm really new to Construct 2 (Just started yesterday). I'm still getting the hang of the event system and the flow of things. I'm used to coding so this level of abstraction is a bit hard for me to get used to.

    I'm currently working on spawning objects at the top of the screen, having them drop down until they reach another object, and then stop.

    So far I have tried bullet behavior, custom movement, and the plain "set angle toward point" method to no avail.

    My current logic is setting a distance variable, moving the object toward the target, then when the object reaches a certain threshold (ie, < 32 pixel distance from the target), I set the object's position to the target point's coordinates.

    For some context, I have a 6x6 hex grid system where each cell has a cell object that I can reference.

    I'm working on spawning "Dot" objects from "spawn point" sprites, then the Dots fall into the first row of the grid.

    This is what my event sheet looks like:

    I know there's some sort of flow issue going on, I have the condition that checks if dist <= 32, but it doesn't seem to be checking for it.

    The Dots simply fall down forever and don't stop.

  • Have you tried the variable "dist" in static?

  • Have you tried the variable "dist" in static?

    Just tried that now, didn't seem to fix the problem.

  • Try to remove the "dist <= 32" out of the function along with the variable.

    Do you see a green arrow near the function name? This means that this event has only occurred once it is called.

    In my understanding how the function can check if "dist <= 32" if it was called before?

  • Hi kellehk ,

    This is probably over simplified, but what about the pathfinding behavior ?

    On Dot spawned, make it pathfind to wherever you need to (even if thats a random cell, as you do have "cell" objects)

    On path found, move along path

    and that's it ?

  • Hi kellehk ,

    This is probably over simplified, but what about the pathfinding behavior ?

    On Dot spawned, make it pathfind to wherever you need to (even if thats a random cell, as you do have "cell" objects)

    On path found, move along path

    and that's it ?

    Here's what I tried:

    I don't know if I'm missing anything, but the dots don't move at all now.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Try to remove the "dist <= 32" out of the function along with the variable.

    Do you see a green arrow near the function name? This means that this event has only occurred once it is called.

    In my understanding how the function can check if "dist <= 32" if it was called before?

    Sorry. I'm still trying to wrap my head around how the events flow.

    What I want to do is:

    -While the distance between dot and target is > 32

    ---move dot toward target.

    ---update distance variable.

    -(after exiting while loop) Set dot's position to target position.

  • I don't get where the "function" usefulness comes from ? Instead of using a function trigger to start your event, just use the "on object created" trigger, what do you think ?

    In order to have a function triggered event to run, you need to have something trigger this function in the first place. I'm almost certain you know that and probably have another event somewhere that calls your "SpawnDot" function, but it seems over complicated to me, as seen.

    Also, in order for your object to move, you need to have another event that goes like this :

    Dot => on path found => move along path

    at the moment, your object calculates the path to point A to point B, but is not told to move along it properly.

  • I don't get where the "function" usefulness comes from ? Instead of using a function trigger to start your event, just use the "on object created" trigger, what do you think ?

    In order to have a function triggered event to run, you need to have something trigger this function in the first place. I'm almost certain you know that and probably have another event somewhere that calls your "SpawnDot" function, but it seems over complicated to me, as seen.

    Also, in order for your object to move, you need to have another event that goes like this :

    Dot => on path found => move along path

    at the moment, your object calculates the path to point A to point B, but is not told to move along it properly.

    Thanks. And yeah, I call the function in another part of the event list that I didn't show because it wasn't relevant. I'm just used to organizing things into functions so that's why I did that.

    A lot of my confusion comes from being new to the engine. I'll try out your advice. Thanks again!

  • Small bump, I updated my last post with an image, should be useful I hope.

  • The condition "If dist <= 32" must be OUTSIDE the function. Right now it just checks the dist at the moment the objects are spawned and, obviously, it's bigger than 32 at that moment.

    Something like this: https://www.dropbox.com/s/kg0jiaacdq90i ... .capx?dl=0

    Hope this helps. Cheers!

  • The condition "If dist <= 32" must be OUTSIDE the function. Right now it just checks the dist at the moment the objects are spawned and, obviously, it's bigger than 32 at that moment.

    Something like this: dropbox.com/s/kg0jiaacdq90i ... .capx?dl=0

    Hope this helps. Cheers!

    Thanks! This clears things up a lot.

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