Get current position and check if close to

  • Hi all,

    I've got a quick one here.

    A while ago i made a system where it checks if a objects position is close to a global variable. Now i want to write all in one line.

    What i want:

    1. On drop

    2. Check if x and y position is close to Global Variable 1(x) and Global Variable 2(y)

    3. If yes do actions.

    What i did previously to check if is it close to my global variables is:

    [Compare two variables] : abs(Objectname.X-p1X) Less than 22

    [Compare two variables] : abs(Objectname.Y-p1Y) Less than 22

    p1X and p1Y are global variables where i will move it to when you dragged it close enough. These are global variables i put in before so thats alright.

    In short.

    I want: On drop (drag and drop function) that Objectname.X AND Objectname.Y is within 22 pixels of p1x and p1y ( global variables ) it does my actions. If not there is nothing to do.

    All that in one line if possible?

    I hope i made myself clear.

    Thanks in advance!

  • What's wrong with the way you do it now?

    You want to check for two different things, but do it in 1 line?

    You could change the compare two values to

    if distance(objectname.x,objectname.y,p1x,p1y) < 22

    but that would make a circle of 22 pixels around your variables instead of the square you use now.

  • Hi Litte Stain :),

    Thanks! Well its for about 50+ objects so the less work the better :).

    What you say: if distance(objectname.x,objectname.y,p1x,p1y) < 22 is what i need but i can not find the action for this :( I don't know how to get that command into Construct 2

  • If it's for 50+ objects you should probably look into doing it with the function object and/or families. Also if many of those objects are instances of the same object you should look into picking instances.

    the distance(x1,y1,x2,y2) is just a value you can put into your system compare values event.

  • Hey thanks man! It works great <img src="smileys/smiley1.gif" border="0" align="middle" /> !

    Yes i am still learning alot and sometimes i find ways, than i try to fine-tune it so it costs less work and less actions.

    For all the objects the same check counts but the only difference are the x1,y1 / x2,y2 etc. So every object does the same but the pinned positions are different.

    Any way i could make it less work and better handling?

    Thanks for the solution! :)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Is it possible to make the p1x and p1y variables of the objects or is there any other way to create a reference between the objects and their desired pin-position? if so you could just make a function.

    on object dropped call function"CheckDistance" with parameters (object.x,object.y,object.pinlocationx,object.pinlocationy)

    on function "CheckDistance"

    (subevent) - Sytem compare values : distance(param(1),param(2),param(3),(param(4)) < 22

    and add your actions.

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