ForumsProgramming ForumPlatformers.

29 7187
Gfitz07
offline
Gfitz07
203 posts
Nomad

I know i have posted something like this before, but this time its different. I need to know how to make a character walk on rough terrain and jump on to more rough terrain and he doesnt float. I also need the screen to follow him. Please help me!

I am using AS2 by the way.

  • 29 Replies
IQAndreas
offline
IQAndreas
299 posts
Peasant

Do I understand this correctly? Is this what is happening?

Here is what you can do to make this game work better (at least in my opinion).

First, make one Platform movie clip (which should be a square, and more advanced platforms can be made by creating several platform squares close together) and only keep it in the library.

Then, make one "Level" movie clip, that acts as a container for the platform objects. You can have several Level movie clips stored in the library, such as "Level1", "Level2", etc. and load each one and remove the old one depending on what

In the Level movie clip, use this code:
onClipEvent (load) {
jumping = false;
jumpspeed = 7;
gravity = 0.5;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
jumping = true;
}
if (jumping == true) {
this._y += jumpspeed;
jumpspeed -= gravity;
} else {
jumpspeed = 7;
gravity = 0.5;
}
if (Key.isDown(Key.LEFT)) {
this._x += 5;
}
if (Key.isDown(Key.RIGHT)) {
this._x -= 5;
}
}


If the Platforms are all placed inside of the Level movie clip, they should move when the Level moves.

And in the Platform movie clip, place the following code:
onClipEvent (enterFrame) {
if (hitTest(_root.man)) {
jumping = false;
}
}

I'm not sure in what order the different onFrame events are executed, and you might get different results based on that order. I know how to fix this in AS3, but not AS2.

If you end up using this code, I can help you to make the code better, so that the Man is able to walk off of platforms and not keep floating in mid air, or be able to jump through platforms from underneath, but stop on the way down.

IQAndreas
offline
IQAndreas
299 posts
Peasant

If you want I can create another poorly drawn diagram of how this method works.

IQAndreas
offline
IQAndreas
299 posts
Peasant

no unfortunately i need it to be like irregular collision which means like crooked. i would post a picture but im a little busy.

Please upload a screenshot as soon as possible, and I will be able to help you much better.

Also, does anyone else know, there is something like hitTest that allows you to test if two movie clips are colliding, but checks each pixel instead of the bounding box. What is the name of this function?

The problem is that it runs slower than hitTest, but I have an idea how to overcome this problem. Or is this function only available in AS3?
Gfitz07
offline
Gfitz07
203 posts
Nomad

i was looking for iregular collision.i guess i could post a picture:

[IMG]http://i408.photobucket.com/albums/pp168/Gfitz07_photo/Thegamepic.jpg[/IMG]

Gfitz07
offline
Gfitz07
203 posts
Nomad

whoops how did that happen

http://i408.photobucket.com/albums/pp168/Gfitz07_photo/Thegamepic.jpg

Gfitz07
offline
Gfitz07
203 posts
Nomad

this is an in game pic and notice how he is floating up. And the irregular collision as in always stay on top of the platform even though its all wavy. get what im saying?

IQAndreas
offline
IQAndreas
299 posts
Peasant

Ah. I see. This can be solved too.

Change the Platform movie clip code to this:
onClipEvent (enterFrame) {
if (hitTest(_root.man)) {
jumping = false;
var i:Number = _y;
var hitFound:Boolean = False;

//This would be so much easier if I remembered the command for exiting a loop early
while (hitFound == False && i <= (y + height)) {
if (hitTest(_root.man._x, i, true) {
_root.Level._y = i;
hitFound = True;
}
i++;
}
}
}

This will also be much faster than running a point hitTest each frame.

This code works if the registration point is at the "feet" (or bottom center) of the character. If you have the registration point in the top left corner, change a line to this instead:
if (hitTest((_root.man._x+_root.man.width)/2, i, true) {


Then there is a problem if the character walks to the side into an area of the platform where it looks like he is walking inside of the platform.

Also, if he walks into two platforms at once, this might also cause problems.

I can modify the code and walk you through what to do with these problems if you like as well.

IQAndreas
offline
IQAndreas
299 posts
Peasant

Also, it might be a good idea, if your situation allows it, to switch to AS3. It runs up to 10 times faster, and even though it uses a bit more complicated syntax, it is worth the change.

Once you get the general technique down with AS3, it becomes much more beneficial. It shouldn't take too long to learn the new techniques since you already have an understanding of AS2.

Gfitz07
offline
Gfitz07
203 posts
Nomad

ok thanks i might switch to as3 after i finish this game.

Gfitz07
offline
Gfitz07
203 posts
Nomad

wait now it doesnt work at all... it says: A script in the movie is causing flash player to run slowly. If this continues to run your computer could become unresponsive. Do you want to abort the script?

IQAndreas
offline
IQAndreas
299 posts
Peasant

Oh. How many platforms do you have on the level? Perhaps this might make it run faster.

Perhaps this might fix it:
onClipEvent (enterFrame) {
//This is the new code, as well as the closing bracket.
//This allows you to skip the CPU consuming hitTest each frame, and instead skips each one that is too far right or left of the man.
if ((_x > _root.man._x + _root.man.width) = False && (_x + width < _root.man._x) = False)
if (hitTest(_root.man)) {
jumping = false;
var i:Number = _y;
var hitFound:Boolean = False;
//This would be so much easier if I remembered the command for exiting a loop early
while (hitFound == False && i <= (y + height)) {
if (hitTest(_root.man._x, i, true) {
_root.Level._y = i;
hitFound = True;
}
i++;
}
}
} //End bracket for the new code
}

But if I really sit down and think about it, I can come up with some better way that solves all the problems mentioned earlier.

First I need to finish installing CS4.

IQAndreas
offline
IQAndreas
299 posts
Peasant

Sorry. I have VB.net ingrained into my mind too deeply.

That is supposed to be == False, not = False.

Gfitz07
offline
Gfitz07
203 posts
Nomad

auuugh its still not working!!!!!! im just gonna change the platforms and make them sqaures because its much easier that way. thanks anyways.

IQAndreas
offline
IQAndreas
299 posts
Peasant

How many platforms do you have?

Are you getting this message when debugging or publishing?
Debugging might make it run slower.

Also, I might have written the while loop wrong.

Try putting in a few trace() statements to see if you are getting stuck there, perhaps using trace(time) (Or whatever the command for the amount of milliseconds is in AS2) to find out how much time elapses in certain places.

Showing 16-29 of 29