PDA

View Full Version : classic platform problem



ryrocks
February 22nd, 2009, 08:06 PM
Hey,
I have a character on the stage (_ball_mc) which you control with the arrow keys. I also have a rectangle movieclip on the stage (this). I want this rectangle movieclip to act as a block platform. ie. When the character jumps ontop of it he can walk across it, if he walks into it from the left he cant move left any further, same with the right side.

These movieclips are not being created at runtime, they are on the stage.

My registration point of the character is bottom right,
my registration point of the block is top left

I've used the following ENTERFRAME with not much luck. Stopping the character from moving any further when he walks into the block seems to be ok. I'm having trouble getting him to land on the block without conflicting with the leftDown and rightDown.

As this is such a common gaming element, I figure there must be some documentation on this ?



if (_findStageInstances)
{
_ball_mc = parent.getChildByName("ball_mc") as MovieClip;
_findStageInstances = false;
}

if (_ball_mc.hitTestObject(this) && _ball_mc.x >this.x)
{
_ball_mc.rightDown = false;
}
if (_ball_mc.hitTestObject(this) && _ball_mc.x > (this.x + this.width))
{
_ball_mc.leftDown = false;
_ball_mc.x +=1; //move ball away from block, otherwise Rdown and Ldown are disabled and it gets stuck
}
if (_ball_mc.hitTestObject(this) && _ball_mc.y < this.y)
{
_ball_mc.falling = false;
_ball_mc.jump = false;
_ball_mc.y = (this.y);
}


Any help would be much appreciated!

therobot
February 22nd, 2009, 09:39 PM
I'm not trying to brush you off, but you should probably consider taking a tile based approach. The best stepping stone out there ( for flash gaming ) is here: http://www.tonypa.pri.ee/tbw/start.html

rather than dealing with one platform at a time, you can develop a system for handling multiple platforms using the same basic structure.

Sorry if this doesn't answer your question, but it does, kind of :)

dandylion13
February 23rd, 2009, 01:50 AM
Hi :)

Yes, this is a classic problem!

Sadly, it's not well documented. The TonyPa (based on OutsideOfSocity aka Jail***** aka yellowman) solution works... but I'll leave it up to you whether you want to go the tile-based route. It might be more of a commitment than you want to make at this point.

The object-based route that you're working with is just fine. I wish I could share my current code I don't entirely own all of it, so I'll can't copy/paste the function I use :(

But here's the general idea:

1. Find the combined halfwidths and halfheights of the objects
2. Find out how far apart the objects are on the x and y axis
3. Check whether objects are intersecting on the x axis by comparing their combined half widths to the distance between them.
4. Check whether objects are intersecting on the y axis by comparing their combined half heights to the distance between them.
5. If this is true, the objects are colliding.
6. Figure out the overlap by comparing the difference between their distances and their widths and/or heights.
7. Push the objects back (give them new x and y positions) based on what the value of the overlap. This creates a blocking effect.

This is the general idea... it's really just about applying some logic to the problem. The main thing is that you need to know the distances between the objects, and how wide and high the objects are. If their distance is less than their combined widths or hieghts, they're overlapping. The amount of overlap is the difference. You need to check that on the x an y axis.

It's not *too* hard.. you'll get it with some trial and error :)

cjke.7777
February 23rd, 2009, 06:44 AM
I strongly agree with therobot, the tonypa tutorials are fantastic plus he is converting them to AS3. I would check these out first. Even if you don't make a tile based platform game the logic still applies.

ryrocks
February 27th, 2009, 06:31 PM
Thanks for all your advice :)

My project deadline is fast approaching so I don't really have time to take a detour into tile based games (as much as I'd like to ;) )

I'll use some more trial and error, I'll keep you posted!

cjke.7777
February 27th, 2009, 07:00 PM
What is the scope of this project? Will much else be happening or just the ball moving onto the box?

ryrocks
February 28th, 2009, 09:41 PM
What is the scope of this project? Will much else be happening or just the ball moving onto the box?

Its a basic 2D side scrolling platform game. It mostly works around controlling a character to run and jump onto platforms and avoid moving objects detected by the hittest function.