Alright, I've been working on a good platformer engine in AS2. I'm hung up right now with the code that affects vertical movement. I'm using one class ("Character") to control both the character and the enemies.
Here's the code that's being called in the timeline onEnterFrame:
Code:
character.gravity();
enemy.gravity();
And here's the code in the class for that function:
Code:
public function gravity():Void {
// Make it fall
if(jumpSpeed < maxGravity) {
jumpSpeed += gravityAmount;
};
y += jumpSpeed;
clip.point = {x:x,y:y};
parent.localToGlobal(clip.point);
// Check for ground below
if(barrier.hitTest(clip.point.x,clip.point.y + height,true) || barrier.hitTest(clip.point.x - width,clip.point.y + height,true) || barrier.hitTest(clip.point.x + width,clip.point.y + height,true)) {
if(barrier.hitTest(clip.point.x - width,clip.point.y,true) || barrier.hitTest(clip.point.x + width,clip.point.y,true)) {
if(!move(0,false,false,true)) {
sendHitToAI();
};
};
while(barrier.hitTest(clip.point.x,clip.point.y + height,true) || barrier.hitTest(clip.point.x - width,clip.point.y + height,true) || barrier.hitTest(clip.point.x + width,clip.point.y + height,true)) {
y--;
clip.point = {x:x,y:y};
parent.localToGlobal(clip.point);
};
jumpSpeed = 0;
jumping = false;
} else {
jumping = true;
};
// Check for ceiling above
if(barrier.hitTest(clip.point.x,clip.point.y - height,true) || barrier.hitTest(clip.point.x - width,clip.point.y - height,true) || barrier.hitTest(clip.point.x + width,clip.point.y - height,true)) {
while(barrier.hitTest(clip.point.x,clip.point.y - height,true) || barrier.hitTest(clip.point.x - width,clip.point.y - height,true) || barrier.hitTest(clip.point.x + width,clip.point.y - height,true)) {
y++;
clip.point = {x:x,y:y};
parent.localToGlobal(clip.point);
};
jumpSpeed = 0;
};
// Check for platform below
if(jumpSpeed > 0 && (platforms.hitTest(clip.point.x,clip.point.y + height,true) || platforms.hitTest(clip.point.x - width,clip.point.y + height,true) || platforms.hitTest(clip.point.x + width,clip.point.y + height,true))) {
while(platforms.hitTest(clip.point.x,clip.point.y + height,true) || platforms.hitTest(clip.point.x - width,clip.point.y + height,true) || platforms.hitTest(clip.point.x + width,clip.point.y + height,true)) {
y--;
clip.point = {x:x,y:y};
parent.localToGlobal(clip.point);
};
jumpSpeed = 0;
jumping = false;
};
// Move background or character
if(mcsToMove[0] != undefined && (clip.point.y < vcamBoundsY && (clip._y - clip.point.y) >= 0 || clip.point.y > (Stage.height - vcamBoundsY - 50) && (clip._y - clip.point.y) <= 0)) {
// Move the background
for(var i = 0;i < mcsToMove.length;i++) {
if(typeof(mcsToMove[i]) == "movieclip") {
// Just a movieclip
mcsToMove[i]._y += (clip._y - clip.point.y);
} else if(mcsToMove[i][1] != undefined) {
// It's an array with a speed multiplier
mcsToMove[i][0]._y += (clip._y - clip.point.y) * mcsToMove[i][1];
} ;
};
y = clip._y;
} else {
// Move the character
clip._y = y;
};
};
The mcsToMove array holds the movieclips needed to make the illusion of a vcam by moving them rather than the character. The main part of the array that affects my code is "level_mc," which holds the enemy (level_mc is the parent of enemy.clip).
The problem is that when I jump with the character, the enemy bounces around (as if the floor had dropped out from under it). That's understandable, but I can't think of a solution to fix it.
One alternative I tried was to move the enemy by adding enemy.clip to mcsToMove on the character, then adding a line of code to the beginning of gravity:
Code:
if(clip._y != y) {
y = clip._y;
return;
};
That causes two problems. First, if the enemy is in the air, it freezes him in the air because it doesn't get down to the falling code. Second, it can allow the enemy to fall through the floor.
I just can't seem to get around this...any help would be greatly appreciated.