Simple collision testing...
in
Programming Questions
•
2 years ago
HI, I'm trying to do a simple square/square collision test (basically hitTest() in AS2) but I'm getting an error...
"a.xpos cannot be resolved or is not a field"
I found one way of getting rid of this error - I replaced "Object a" on the first line with "Ball a" (the class the object belongs to), but the thing is I want to do this between objects that originated from two different classes... isn't this possible? Do I have to pass on each objects value, i.e. something like hitTest(xpos1,ypos1,width1,height1,xpos2,ypos2,width2,height2) ? That'd be a nightmare...
"a.xpos cannot be resolved or is not a field"
I found one way of getting rid of this error - I replaced "Object a" on the first line with "Ball a" (the class the object belongs to), but the thing is I want to do this between objects that originated from two different classes... isn't this possible? Do I have to pass on each objects value, i.e. something like hitTest(xpos1,ypos1,width1,height1,xpos2,ypos2,width2,height2) ? That'd be a nightmare...
- Boolean hitTest(Object a, Object b) {
Boolean xhit,yhit;
if ((a.xpos>b.xpos && a.xpos<b.xpos+b.width) || (b.xpos>a.xpos && b.xpos<a.xpos+a.width)) {
xhit=true;
}
else {
xhit=false;
}
if ((a.ypos>b.xpos && a.xpos<b.xpos+b.height) || (b.ypos>a.ypos && b.ypos<a.ypos+a.height)) {
yhit=true;
}
else {
yhit=false;
}
if (xhit && yhit) {
return true;
}
else {
return false;
}
}
1