-
cake!
[AS2][F8]Collision detection not working correctly
Ok so I'm trying to make my hero stop when he hits the walls of the ground MC, named "g". So I wrote this up:
Code:
if (Key.isDown(Left)) {
if (!g.hitTest(this._x-(hXbuff+xSpeed), this._y-20, true) ) {
dir = "left";
this._x -= xSpeed;
}else{
heroStatus = "stand";
}
}else if (Key.isDown(Right)) {
if (!g.hitTest(this._x+(hXbuff+xSpeed), this._y-20, true) ) {
dir = "right";
this._x += xSpeed;
}else{
heroStatus = "stand";
}
} else {
heroStatus = "stand";
}
It tests for the heros X, plus hXbuff which is equal to 20, and xSpeed which is equal to 7. If the heros X, plus these variables, is not hitting a wall, then the hero may move, otherwise the hero goes back to standing; this prevents the running animation being played when hitting a wall.
This works, but the problem is that it doesn't work all the time. Randomly you will be able to move to a closer position between the heros X and where it is meant to have stopped; hXbuff+xSpeed. (pictured below)
NOTE: This is what is meant to happen. The green lines indicate hXbuff+xSpeed.
NOTE: This is what should NOT be happening.
Is there any reason the code above would cause this to happen? It looks fine to me. Or can you think of any other reason this would be happening?
Thanks!
Skribble
Its not that im lazy, I just dont care.
-
Senior Member
Could be caused by rounding errors in _x and _y. Try tracing them to see if they suddenly change from 100 into 99.999999999
-
cake!
I traced the heros x and y and they were whole numbers the entire time, no decimals at all.
I also had another attempt at rewriting the hitest function, and included a for loop to test from the heros _x + i; having it check if i was less than hXbuff+xSpeed, which is the distance away from the hero I want to check.
It runs from 0-27 and tests the heros x plus that variable, so technically it should be testing every point, but for some reason its STILL doing the same thing!
Heres the modified code, Im not sure whether or not it was the best way to go about it, but it was an attempt none the less. Any more help would be greatly appreciated! Thanks
Code:
Xbuff = hXbuff+xSpeed;
if (Key.isDown(Left)) {
for(i=0;i<Xbuff;i++){
if (!g.hitTest((this._x-i), this._y-20, true) ) {
dir = "left";
}else{
heroStatus = "stand";
}
if(i>=Xbuff){
i=0;
}
}
if(dir == "left"){
this._x -= xSpeed;
}
}else if (Key.isDown(Right)) {
for(i=0;i<Xbuff;i++){
if (!g.hitTest((this._x+i), this._y-20, true) ) {
dir = "right";
}else{
this._x -= xSpeed;
heroStatus = "stand";
}
if(i>=Xbuff){
i=0;
}
}
if(dir == "right"){
this._x += xSpeed;
}
}else{
heroStatus = "stand";
}
Last edited by Skribble_Style; 05-20-2009 at 03:11 AM.
Skribble
Its not that im lazy, I just dont care.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|