Forum Topic: hit detection problem

(262 views • 4 replies)

This topic is 1 page long.

<< < > >>
None

ProphetofHAM

Reply To Post Reply & Quote

Posted at: 11/6/05 12:44 AM

ProphetofHAM NEUTRAL LEVEL 12

Sign-Up: 04/10/01

Posts: 70

ok, i'm using flash 5.

this is the very beggining stages of a game, trying to get the hit detection for walking around and what not working. but objects sometimes randomly can still move through walls. if anyone could help me out that would be rad.

here's what i have.

in the frame, the only code is:

_root.wallcount=0;
stop ();

the walls are simplys squares/rectangels. make as many as you want, dont name them, their script is simply:

onClipEvent(load) {
_root.wallcount++;
this._name=_root.wallcount;
}

your character who is controlld by the arrow keys should be a square with this script and be named "c":

onClipEvent(load) {
this.speed=5;

}

onClipEvent(enterFrame) {
if(Key.isDown(Key.RIGHT)) {this.xmove+=this.speed};
if(Key.isDown(Key.LEFT)) {this.xmove-=this.speed};
if(Key.isDown(Key.DOWN)) {this.ymove+=this.speed};
if(Key.isDown(Key.UP)) {this.ymove-=this.speed};
this._x+=this.xmove;
this._y+=this.ymove;
for(i=0;i<=_root.wallcount;i++) {
if(Math.abs(this._X-_root[i]._x)<(this._wi
dth/2+_root[i]._width/2)) {
if(Math.abs(this._y-_root[i]._y)<(this._he
ight/2+_root[i]._height/2)) {
if(Math.abs(this._X-this.xmove-_root[i]._x
)>=(this._width/2+_root[i]._width/2) and Math.abs(this._y-_root[i]._y)<(this._heigh
t/2+_root[i]._height/2)) {
this._x-=this.xmove;
this.dif=Math.abs(this._X-_root[i]._x)-(th
is._width/2+_root[i]._width/2);
if(this.xmove>0) {this._x+=this.dif};
if(this.xmove<0) {this._x-=this.dif-.03};
} else {
this._y-=this.ymove;
this.dif=Math.abs(this._y-_root[i]._y)-(th
is._height/2+_root[i]._height/2);
if(this.ymove>0) {this._y+=this.dif};
if(this.ymove<0) {this._y-=this.dif-.03};
}
_root.box._rotation+=5;
}
}
}
this.xmove=0;
this.ymove=0;

}

and finaly an "enemy" that follows the player. should be a square with this script:

onClipEvent(load) {
this.speed=3;

}

onClipEvent(enterFrame) {
this.xd=this._X-_root.c._x;
this.yd=this._y-_root.c._y;
this.h=Math.sqrt(((this._X-_root.c._x)*(th
is._X-_root.c._x))+((this._y-_root.c._y)*(
this._y-_root.c._y)));
this.xmove=this.xd*(this.speed/this.h)*-1;
this.ymove=this.yd*(this.speed/this.h)*-1;
this._x+=this.xmove;
this._y+=this.ymove;
for(i=0;i<=_root.wallcount;i++) {
if(Math.abs(this._X-_root[i]._x)<(this._wi
dth/2+_root[i]._width/2)) {
if(Math.abs(this._y-_root[i]._y)<(this._he
ight/2+_root[i]._height/2)) {
if(Math.abs(this._X-this.xmove-_root[i]._x
)>=(this._width/2+_root[i]._width/2) and Math.abs(this._y-_root[i]._y)<(this._heigh
t/2+_root[i]._height/2)) {
this._x-=this.xmove;
this.dif=Math.abs(this._X-_root[i]._x)-(th
is._width/2+_root[i]._width/2);
if(this.xmove>0) {this._x+=this.dif+.03};
if(this.xmove<0) {this._x-=this.dif-.03};
} else {
this._y-=this.ymove;
this.dif=Math.abs(this._y-_root[i]._y)-(th
is._height/2+_root[i]._height/2);
if(this.ymove>0) {this._y+=this.dif+.03};
if(this.ymove<0) {this._y-=this.dif-.03};
}
_root.box._rotation+=5;
}
}
}
this.xmove=0;
this.ymove=0;

}

or you could just download the FLA if it will let me atach it...
which it won't. rename the attached image to a .fla maybe?
nope... guess i'll just have to put it online...

get the FLA here http://chadicus.free..e.com/hitdetect2.fla

check out the SWF of what i'm talking about here. .http://chadicus.free..e.com/hitdetect2.sw
f

any help would be awesome in making these things not jump around when they touch walls, or go through the walls


None

TelaFerrum

Reply To Post Reply & Quote

Posted at: 11/6/05 01:26 AM

TelaFerrum NEUTRAL LEVEL 06

Sign-Up: 02/21/05

Posts: 396

So that you know, it says I don't have permission to acess either of your files. Is there somewhere else you could host them?

You're probably the one who should be helping me with my problems because I've never done code this complicated before, but it's a challenge and I want to try it out. Right now I'm trying to comprehend how your code works. I'm assuming Math.abs works similarily to rounding.


None

dieEVILsanta

Reply To Post Reply & Quote

Posted at: 11/6/05 03:19 AM

dieEVILsanta LIGHT LEVEL 07

Sign-Up: 05/26/05

Posts: 464

Math.abs(number) gets the absolute value of a number.


None

ProphetofHAM

Reply To Post Reply & Quote

Posted at: 11/6/05 02:06 PM

ProphetofHAM NEUTRAL LEVEL 12

Sign-Up: 04/10/01

Posts: 70

At 11/6/05 01:26 AM, Tela_Ferrum wrote: So that you know, it says I don't have permission to acess either of your files. Is there somewhere else you could host them?

crap, sorry. you should be able to get them from here

http://chadicus.free../newgroundshelp.html


None

TelaFerrum

Reply To Post Reply & Quote

Posted at: 11/6/05 04:34 PM

TelaFerrum NEUTRAL LEVEL 06

Sign-Up: 02/21/05

Posts: 396

Instead of:
(this._width/2+_root[i]._width/2)

Try:
(this._width/2+_root[i]._width/2)-1

I just tried it out and it seems to work fine now in my case, but I don't know if it will still work when the walls are moved around. I don't know why your original code didn't work. I used trace and found that the if statement was returning it as false even though when I traced the numbers you could clearly see they were equal. I'm gonna run some more tests to see if it still works now.


All times are Eastern Standard Time (GMT -5) | Current Time: 11:56 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!