ForumsProgramming ForumMy boundaries are all screwed up - help

8 2980
MrM
offline
MrM
12 posts
Nomad

alright, so I am working on my first platformer, and have been working on the walls, floors and platforms. I have my character successfully staying on the floor, but that's where the success ends. On one wall, the character goes halfway into it before stopping, on the other he has about a half a wall of space before the wall. And it just plain doesn't recognize platforms. Here is my code, can you tell me if anything is wrong with it?

onClipEvent(load){
var ground:MovieClip = _root.ground;
var grav:Number = 0;
var gravity:Number = 2;
var speed:Number = 5;
var maxjump:Number = -18;
var touchingGround:Boolean = false;
}

onClipEvent(enterFrame){
_y += grav;
grav += gravity;
while (ground.hitTest(_x, _y, true)) {
_y-= gravity;
grav = 0;
}
if (ground.hitTest(_x, _y+5, true)) {
touchingGround = true;
} else {
touchingGround = false;
}

if(Key.isDown (Key.RIGHT)) {
_x += speed;
}
if(Key.isDown (Key.LEFT)) {
_x -= speed;
}
if(Key.isDown (Key.UP)&&
touchingGround) {
grav = maxjump;
}


if(ground.hitTest(_x+(_width/2),_y-
(_height/2),true)) {
_x -= speed;
}
if(ground.hitTest(_x-(_width/2),_y-
(_height/2),true)) {
_x += speed;
}
if(ground.hitTest(_x,_y-(_height),
true)){
grav = 3;
}
}

  • 8 Replies
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

How many FPS is your game running?

MrM
offline
MrM
12 posts
Nomad

its at 30 fps, but i have also tried it at 12, 20 and 60. 30 looks the best, but i get the problem with all of them. Would the FPS affect the code?

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

Yeah, the FPS determine the speed of your "onClipEvent" loop, so a faster loop means more precise detection

hitTest itself is not very precise, there's this code I call "future X", which instead of checking if a colision happened, you check if a colision will happen

If you walking forward at a speed of X + 5, ou can check if your current X + 5 will go inside another object's X left border, so you stop it a loop before it actually goes in, so you can react, making it stop and positioning it to the limit of the object's X left border

MrM
offline
MrM
12 posts
Nomad

ah, ok. Well, I'm rather new at actionscript, so i will probably stick with the code I currently have. I guess I can screw around with it until it fixes itself.

LonLonRanch
offline
LonLonRanch
172 posts
Nomad

if the character runs halfway into a wall and stops half a body before the other wall my guess would be that the characters registration point is not centered. Go inside your character movie clip and make sure the registration point (the little + symbol) is in the center, you can use the align tools (->Windows->Align) to do this perfectly.

MrM
offline
MrM
12 posts
Nomad

thanks lon lon, that helped a bunch. Kudos

- mrm

PixelSmash
offline
PixelSmash
566 posts
Nomad

God damnit, sorry about that. My gf is in a weird mood today, which includes messing up my posts. If there's a mod watching, just delete that one.

Anyway, about your code. First off, are your platforms also embedded in the 'ground' movieclip? If they are, it's pretty weird that you wouldn't be able to walk on them.
Also, your code for managing the gravity is pretty weird... why use the 'while' you have there? Also, if you're going for speed, try and keep the hittests down to a minimum... you could try and find an alternative to the five you have there, and use just one or two.

If I have the time, I'll see if I can rewrite some stuff for ya

MrM
offline
MrM
12 posts
Nomad

Hey PixelSmash, thanks for the response. I finally fixed my platforms, and the character, so it's all working smoothly now. If you feel the urge to rewrite the code though, feel free, It would be really helpful.

Showing 1-8 of 8