View Full Version : Platforming Type Issue
eatmorchikin6464
02-23-2008, 02:24 AM
So I'm making a game that involves lots of little platforms scrolling across the screen and it's very important that it all runs very smoothly.
However, right now, I have a problem and luckily enough I know why it happens I just don't know how to fix it.
So, right now I have the jumper and the platforms. If the jumper is not on a platform then the jumper will fall down until it hits a platform and will stop following because it has just been caught by the platform. But here's the problem, my code to make the jumper fall is somewhere along the lines of:
jumper._y += 10;
So, even though a hit is detected with the platform, the jumper isn't directly on top of the platform and is instead usually a few pixels too low so that it overlaps the platforms.
The reason for this is because if the jumper is only 5px above the platform, it will move down 10, detect a collision with the platform and stop, but it's still already gone the extra 5px and is now in the platform.
So, anyone have any ideas on how I could fix this issue?
If I jack up the FPS to 120 and decrease the speed of the jumper to 1px then it works beautifully, except for the large slow down and varying speeds of the game because of the cpu usage to do all of that.
truimagz
02-23-2008, 04:04 AM
when the jumper lands on a platform do this
var platform : MovieClip = //the platform the player hit
player._y = platform._y
since its a side scroller im assuming you have your player aligned bottom, so this should work fine.
MOOGLEFIGHTER
02-23-2008, 04:10 AM
Never have your FPS above 50.
eatmorchikin6464
02-23-2008, 10:43 AM
when the jumper lands on a platform do this
var platform : MovieClip = //the platform the player hit
player._y = platform._y
since its a side scroller im assuming you have your player aligned bottom, so this should work fine.
Well, I originally did have this in mind and when I tried to do it, it doesn't quite work. Well, it used to work alot worse but now I got it working MUCH better, there's just one teensy problem.
The platforms are all duplicated movie clips of a single platform. For some reason, this makes something with the _y coordinates different for the duplicates. So, the jumper goes smoothly over the duplicates but jumps up for the original platform, then drops back down. I was able to add some code to minimize this jump but if I make the jump any smaller it screws up and there's some sort of infinite loop going on or something because Flash has to end the script because it just keeps running and stuff.
So, here's an example. The original MC is the lighter one:
http://img90.imageshack.us/my.php?image=gametester1nc6.swf
And here's the code that is of importance:
//Variables
var platformName:String = "";
var newY:Number = 0;
var noFall:Boolean = false;
var currPlatform:MovieClip;
var yChange = 21;
platform0._alpha = 50;
//Returns true if the jet is in contact with a platform, false otherwise
contact = function():Boolean
{
//Loops through each of the numbered platforms
for(i:Number = 0; i < 12; i++)
{
//Sets the platform name based on i
platformName = "platform" + i;
//Gets the platform MovieClip from the platformName reference
currPlatform = eval(platformName);
//If the jet is touching either the platform's top border or just the platform
if((jet.hitTest(currPlatform.topLine)) || jet.hitTest(currPlatform))
{
if(i == 0)
{
yChange = 12;
}
//Don't allow the jet to fall because it is on a platform
noFall = true;
//Set the new y coordinate to newY
newY = (currPlatform._y) - yChange;
yChange = 21;
//Move the jet
jet._y = newY;
return true;
}
}
//If there are no hits
//Allow the jet to fall
noFall = false;
return false;
}
eatmorchikin6464
02-23-2008, 12:35 PM
Alright, so after I put a bunch of work in, I mainly fixed that whole problem, but it's still there, just in a different way. So, in this game, you have to land on the platforms, if you hit the left sid of one then you die but since for a brief second the jumper sometimes lower than the top of the platform before the _y coordinate is corrected. That means that often times when this correction needs to be made the jumper will hit the left side of the platform and cause a mis-hit. Any ideas on a way around this?
Here is an example:
width=550 height=400
Just use a flag hittest such as:
if(ground.hitTest(hero._x,hero._y+10)){
//your code to stop falling
}
It'll detect hitting it before you do so you'll stay on top.
eatmorchikin6464
02-25-2008, 03:02 PM
Just use a flag hittest such as:
if(ground.hitTest(hero._x,hero._y+10)){
//your code to stop falling
}
It'll detect hitting it before you do so you'll stay on top.
But there's still the same issue. If the orange block is 11px above a platform, it will lower 5 more and be at 6px above the block and it will stop falling. But maybe on another block it stops falling at 9px above it. It's just the uneveness that's driving me crazy.
Is there any way I can avoid this all together and have the block always stop in the right spot?
This is when you use a gravity var and have it so that he not only stops falling when on a block but moves up too.
while(ground.hitTest(hero._x,hero._y+grav)){
this._y-=grav
}
//If he's below the surface he'll move up.
I've always just made all the platforms into one MC the ground and when you're on the ground move you up so you don't stay below the ground. If you're above the ground you'll fall onto it. Simple and effective.
I want to make a new platformer engine now... <.< *opens flash*
eatmorchikin6464
02-25-2008, 10:12 PM
This is when you use a gravity var and have it so that he not only stops falling when on a block but moves up too.
while(ground.hitTest(hero._x,hero._y+grav)){
this._y-=grav
}
//If he's below the surface he'll move up.
I've always just made all the platforms into one MC the ground and when you're on the ground move you up so you don't stay below the ground. If you're above the ground you'll fall onto it. Simple and effective.
I want to make a new platformer engine now... <.< *opens flash*
But that's what I have done and that part works great. The problem is that in this game, the jumper is supposed to die if it hits a platform rather than landing on it. But the hitTest screws up because even if the jumper is technically "landing" on the platform, many times it's actually going below it before being moved up and therefore causing my game to think the jumper has crashed.
Malignus
02-25-2008, 10:40 PM
You could always give yourself a little breathing room by having a second, smaller rectangle movieclip nested inside the platform movieclip that the game uses solely for hittesting a crash. Like a little russian doll of death. :)
eatmorchikin6464
02-26-2008, 02:30 PM
But how would that solve any of my problems? The hitTest would still return true because the jumper will be hitting the special MC for a brief moment until it is repositioned.
But, I think I came up with a new idea using what Matt suggested. Rather than reposition the jumper once it's too low, I should reposition the jumper when it gets close to the top of the platform. That's what I'm gunna try.
Do it like this:
if (ground.hitTest(this._x, this._y + grav, true)) {
this._y += grav;
grav = 0;
}
That way he'll hit the platform before then move down so that he is perfectly on the platform then stops the gravity. tada!
eatmorchikin6464
02-26-2008, 07:33 PM
Well, I'm going to do something pretty close to that.
I'm going to do the hitTest 5px above the platform, then if it returns true, I will increment the jumper's _y depending on how high above it is. So something like:
jumper._y += (platform._y - jumper._y)
With your method, many times, unless the jumper is exactly 5px above the platform, the jumper will be placed below the platform.
I've made an engine, a pretty damn good one and it works perfect.
I'll upload it so far.
http://truimagz.com/hosting/matt/Games/platformer.swf
vBulletin® v3.7.3, Copyright ©2000-2010, Jelsoft Enterprises Ltd.