Be a Supporter!

collision detection problem

  • 507 Views
  • 7 Replies
New Topic Respond to this Topic
curkas
curkas
  • Member since: Aug. 10, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
collision detection problem 2006-08-10 08:29:00 Reply

Hey. This is my first post here. I've been visiting Newgrounds for a while now, but have never signed up; the traffic is just seemed too busy for this country bloke.

Anyway, my problem is, I'm fairly new to actionscript and I'm wanting to make a platformer. I've worked out things such as jumping and running for my character but my problem is, my character keep intersecting with the platform, cutting 1/4 of his body. Does anyone know why this would be? I've had a look around but everything seems too confusing and doesn't seem to answer exactly what my problem would be.

Here's my code:

onClipEvent (load) {
step = 5;
stance = 1;
gravity = 1.5;
velocity = 0;
falling = true;
stop();
}
onClipEvent (enterFrame) {
//MOVE RIGHT
if (Key.isDown(Key.RIGHT)) {
stance = 1;
this._x += step;
_root.blarg_move.gotoAndPlay("runright");
} else if (Key.isDown(Key.LEFT)) {
stance = 0;
this._x -= step;
_root.blarg_move.gotoAndPlay("runleft");
} else {
if (stance == 1) {
gotoAndStop("standing");
} else {
gotoAndStop("standing");
}
}
//STANDING STILL
if (falling == true) {
velocity += gravity;
_y += velocity;
}
//PLATFORMS
if (this.hitTest(_root.ground)) {
if (falling == true) {
_y = platform._y;
velocity = 0;
falling = false;
_y += velocity;
}
} else {
falling = true;
}
//JUMPING, CHANGE THE VELOCITY NUMBER
//TO SET THE HEIGHT OF JUMP
if (Key.isDown(Key.UP)) {
if (falling == false) {
velocity -= 15;
falling = true;
}
}
}

I would appreciate it heaps if someone could help!

Edvin
Edvin
  • Member since: Feb. 3, 2004
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to collision detection problem 2006-08-10 08:31:01 Reply

I think it's a registration point problem. Make sure the registration point is located around/below the feet..

West-End-Pro
West-End-Pro
  • Member since: Feb. 15, 2006
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to collision detection problem 2006-08-10 08:32:36 Reply

onClipEvent (load) {
step = 5;
stance = 1;
gravity = 1.5;
velocity = 0;
falling = true;
stop();
}

Well, you've set falling to "true" here which means that when the frame loads, he begins falling. Also, have you checked that the little "crosses" within the movieclip are at the botton of his feet?

Cybex
Cybex
  • Member since: Mar. 4, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to collision detection problem 2006-08-10 08:40:36 Reply

If what i'm thinking is what you mean, its because flash checks whether the character is hitting the platform and if its not, moves it down a certain number of pixels, the next time flash checks whether the character is hitting the platform, the character could be halfway through the platform already. Sorry, i'm not very good at explaining things. Something similar to this should fix it:

while(char._y+1.hitTest(platform)){
char._y--;
}
This basically makes sure that the character is on top of the platform rather than inside it. If you dont already know about while loops, i suggest you use AS: Main or the flash helpfiles to learn about them.

curkas
curkas
  • Member since: Aug. 10, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to collision detection problem 2006-08-10 08:40:46 Reply

Thanks for your quick responses! This is going to sound silly but the registration point is the circle in the middle of the mc? I have moved all of these down to the base of the character but it still does it.

I thought that maybe it might have something to do with my character? I have drawn his animations frame by frame and have markers going to each action I want him to perform... although I used the same code with a circle and the circle doesn't detect the collision either.

Any more help is heaps appreciated :D

curkas
curkas
  • Member since: Aug. 10, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to collision detection problem 2006-08-10 08:45:38 Reply

At 8/10/06 08:40 AM, Cybex wrote:
while(char._y+1.hitTest(platform)){
char._y--;
}

.
Thanks. I'll look into that. I hadn't heard of the while loop before.

blanblan
blanblan
  • Member since: Jan. 21, 2006
  • Offline.
Forum Stats
Member
Level 17
Voice Actor
Response to collision detection problem 2006-08-10 09:52:13 Reply

At 8/10/06 08:40 AM, curkas wrote: Thanks for your quick responses! This is going to sound silly but the registration point is the circle in the middle of the mc? I have moved all of these down to the base of the character but it still does it.

No, the registration point is the plus sign thing.. just double click your character to edit it, then move it away from the center of the movie clip, and you should see a plus sign. Make sure your character's feet are at that plus sign.

I thought that maybe it might have something to do with my character? I have drawn his animations frame by frame and have markers going to each action I want him to perform... although I used the same code with a circle and the circle doesn't detect the collision either.

Nothing to do with your character. :)

Any more help is heaps appreciated :D

You're welcome! :D

Dizimz
Dizimz
  • Member since: Jan. 3, 2003
  • Offline.
Forum Stats
Member
Level 07
Artist
Response to collision detection problem 2006-08-11 18:53:21 Reply

Best detection is the flag hitest

if(_root.ground.hitTest(_x , _y+Height, true) && falling==true){
falling=false;
}while(_root.ground.hitTest(_x , _y+Height, true)){
_y-=0.1;

Something like that never fails you gotta tweak it , and theres bound to be a few errors ,I wrote that on the spot but it always works for me

Dont use norm hittest with platforms , since flash doesnt run like real life you should know that when an object accelerates falling it displaces by so many pixels so its actually possible to fall through it