Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2008, 03:40 AM   PM User | #1
Shirk Deio
New Coder

 
Join Date: Mar 2007
Posts: 33
Thanks: 6
Thanked 0 Times in 0 Posts
Shirk Deio is an unknown quantity at this point
AS2 Platformer - Enemy Barrier Problem

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.
Shirk Deio is offline   Reply With Quote
Old 12-07-2008, 04:12 AM   PM User | #2
Shirk Deio
New Coder

 
Join Date: Mar 2007
Posts: 33
Thanks: 6
Thanked 0 Times in 0 Posts
Shirk Deio is an unknown quantity at this point
I just discovered that the code thinks that the enemy IS on the barrier???

Code:
		
trace(barrier.hitTest(clip.point.x,clip.point.y + height + 2,true));
That returns "true," even when the enemy is clearly hovering over the barrier. I tried placing the trace function at the beginning of gravity, or just before the mcsToMove section, or at the end of the function, and each time it returned true (however, it returned false while it was falling, and interestingly it also returned false when it goes slightly down into the barrier while the character falls back down from jumping).
Shirk Deio is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:06 PM.


Advertisement
Log in to turn off these ads.