With that one, you'd have to do
if (point_distance(x, y, x2, y2) < 150) {
with (instance_nearest(x, y, sell)) {
visible = true;
}
}
else {
with (instance_nearest(x, y, sell)) {
visible = false;
}
}
OR, instead of
with(instance_nearest() ) blah blah, you could probably get away with setting the nearest
sell to a variable, like
sell_nearest or something and just setting
sell_nearest.visible = true/false;.
Okay, now, since I can tell you're struggling with this a bit, I'm going to go into a bit of detail with this.
First off, if you're going to use city and ship like that,
YOU ABSOLUTELY HAVE TO MAKE SURE THAT SHIP AND CITY ARE VARIABLES THAT YOU'VE SET TO STORE THE ID OF THE NEAREST SHIP/CITY, DEPENDING, LIKE I DESCRIBED ABOVE. OTHERWISE, IF SHIP OR CITY ARE OBJECTS, THEN IT WILL TAKE THE FIRST CITY/SHIP IN ORDER FROM 0 to TOTAL_AMOUNT_OF_THAT_OBJECT, WHICH INSTANCE_FIND WOULD USUALLY LOOK FOR.I would
HIGHLY recommend naming all of your objects
obj_<name here> and your sprites
spr_<name here> to avoid any kind of conflicts between object/sprite/variable names that would normally break the game.
Secondly, if that's in the city's step event, then you don't need to make it
city.x or
city.y. When referring to an object's
x/y within its own events, you just type
x or
y. In fact, if you have it set up like you have it currently, and your city/ship variables are set correctly, it'll look for the nearest city to IT, meaning the next city over, rather than THAT city.
Third,
with() is a type of statement, like
if() or
for() that allows the instance in question to be controlled from another instance as if it were within that instance. Almost like coding outside of an instance.
The only caveat is that while within the brackets describing the with() loop, you CANNOT access the variables or anything else of the object that you're currently coding in, meaning you have to be really careful of how you're using your with() statements. As Jack stated below, you can use the err...built-in(?) variable
other to access the variables of your current object again.
Fourth, to get the instance nearest to the instance you're coding in, you absolutely have to set it up
instance_nearest(x, y, object). If you put in a number for either of those instead of x and y, it will return the nearest instance to THAT POSITION, meaning that unless the cities start moving, it will always return the same city which may even be entirely across the map from the ship.
Fifth, I'd like to warn you that if you don't set up the
sell objects to ignore input unless they're visible, you'll have issues with every one of the sell objects being controlled all of the time.
I think I've covered everything. If you have more questions, just ask.
Edited by johnki, 12 May 2012 - 10:56 PM.