Reputation: 13
Ok, i have a little problem. I have two object (and two buttons in each object, that are invisible), first object is called "crveno", second "zeleno". "zeleno" has hittest area that is called "zlhit", and "crveno" has hittest area that is called "chit", both are buttons.
I have 4 more objects with similar hittests, all work.
This is hittest for zeleno and crveno. I want to zeleno go back to its start position, when it hits crveno.chit, but it doesn't work. It stays on the curent position, and crveno goes to start position. On the other hand, hittest for crveno works. This is code from two hittests.
else if (zeleno.zlhit.hitTestObject(crveno.chit))
{
crveno.x = crveno.x;
crveno.y = crveno.y;
zeleno.x = zelenoStartX;
zeleno.y = zelenoStartY;
}
else if (crveno.chit.hitTestObject(zeleno.zlhit))
{
zeleno.x = zeleno.x;
zeleno.y = zeleno.y;
crveno.x = crvenoStartX;
crveno.y = crvenoStartY;
}
Upvotes: 0
Views: 102
Reputation: 315
Since you mentioned that crveno does change positions I am assuming that the objects containing the buttons is updating the buttons' positions when the objects' positions change. If that is not the case, then the buttons not changing position with their container objects could be the problem.
Otherwise, it seems that any time that zlhit overlaps with chit, I would expect the inverse to be true as well. Since your code contains else if statements, only the first condition that is true will trigger. If your goal is that each object moves back to its start position when their buttons overlap, then you could try the following:
if (zeleno.zlhit.hitTestObject(crveno.chit) &&
crveno.chit.hitTestObject(zeleno.zlhit)) //this 2nd collision check is probably unnecessary
{
crveno.x = crvenoStartX;
crveno.y = crvenoStartY;
zeleno.x = zelenoStartX;
zeleno.y = zelenoStartY;
}
Upvotes: 0
Reputation: 12431
You've got two different tests for what is effectively the same condition ie. the hit areas of the two instances are intersecting. As such the first else if
condition will always execute when hittest
returns true for those two instances, and your code will never reach the second else if
.
Without knowing more about what you're trying to do, it's difficult to offer on advice on how to resolve the issue, however if you want both objects to return to the start position when they intersect, you could combine that in one conditional:
// When crveno and zeleno hit, return both to their start positions
else if (zeleno.zlhit.hitTestObject(crveno.chit))
{
crveno.x = crvenoStartX;
crveno.y = crvenoStartY;
zeleno.x = zelenoStartX;
zeleno.y = zelenoStartY;
}
Upvotes: 1