I'm working on a game, and it appears that a tutorial regarding this subject has not yet been created, and since I had to write the script anyways I decided to write up a tutorial for it.

For those of you who used Game Maker before Flash, you may remember something called more to contact position, or something like that. It made sure that when objects collided, there would not be a gap between the two objects and moved the objects so that they would contact each other. Flash does not have this feature, but it can be replicated.

First of all, you need a wall/boundary. Just draw a rectangle and make it into a movie symbol. Name it whatever you want because it really doesn't matter. Create an instance of this symbol on the main stage, and in the Actions panel, insert this script. Hopefully the comments in it will explain the code to you.

onClipEvent(enterFrame){
if (!named){//If this instance hasn't been named before
_name="wall";//Give it the name "wall".
named=true;//It's been named.
}
}
//Yes I could have just told you to name it, but if you ever want to make multiple copies of this wall this could come in handy.

Next, draw a small square of a different color so that the two can be distinguished and make it into a movie symbol, name it, make an instance on the stage, open the Actions panel, and insert this script.

My script for movement is probably pretty different from what you're used to.

Comment 1: Nothing complicated yet. This probably isn't how other tutorials you may have read tell you to do movement. Here, I've declared a function named move, with the parameters xmove and ymove that will say how much to add or subtract to the x and y coordinates.

Comment 2: Here, I have a hitTest against the wall made earlier. Here's where the move to contact stuff comes in. Inside the if statement for the hitTest, I have four other if statements. They're all basically the same with some modifications, so I'll only explain the first one.

First, it checks to see if the value of the xmove parameter is greater than 0, and if it is, that will confirm that the character is moving right. If so, it subtracts a certain amount of distance so that the character is not inside of the wall.

It does this by getting the boundaries for the character and wall. We use the xMax property for the character, which tells us the xcoorditate for the right side of the character. It subtracts the the xMin property of the wall from this, which is the xcoordinate for the left side of the wall. This gives the distance that needs to be subtracted from the x of the character, and does just that.

Something that may be confusing is the +0.1 at the end.This is because hitTest seems to return a true value even when the boundaries of the objects are touching but are not inside each other. The +0.1 adds just enough space between the two objects for the hitTest to return false, but so little that it will likely remain unnoticed.

Comment 3: Again this is simple. It just has an if statement for each of the four arrow keys to see if they are being pressed down, and if so, it calls the move function that was declared earlier.

End of comments.

Well, hopefully this was helpful to you. Or someone. It may have been too complicated for a begginer, but with too much explaination for someone experienced. I haven't tried writing a tutorial before, but if you don't like it, at least you can copy the code if you like that.

I have been experimenting with this lately. I have been trying to get my guy to move along a slope. So, for every different slope I have, I set the angle the guy walks along.

onClipEvent(enterFrame)
{
if(this.hitTest(_root.form))
//Change root to the instance name of your ramp.
if(Key.isDown(68))
//Key 68 is the D key. Change it to whatever key is used to move your guy.
this._y -=1.3;
//This sets the slope the guy walks at, making it seem like he is crawling up the ramp. Set it to whatever //slope looks the best. Experimentation will be required. :o
{
if(Key.isDown(65))
this._y+=1.3;
//This is the A key and makes him go down the ramp. (+ means down and - means up with Y values. :|
}
}

See? Its that simple! All you need is the patience to adjust the slope values accordingly. Of course, you will need your movement coding done... but that is a different tutorial. ;) I made this myself so there is probably an easier type of coding out there, but at least this is only a couple of lines.

WHAT??? This is from 2006? CRAP! Well, I dont want to erase all this typing.. oh well people will need to know this anyways. Sorry for the bump. This thread needs a good bump anyways.

At 1/8/09 10:07 PM, FatKidWitAJetPak wrote:
WHAT??? This is from 2006? CRAP! Well, I dont want to erase all this typing.. oh well people will need to know this anyways. Sorry for the bump. This thread needs a good bump anyways.

XD Wow I laughed my ass off when I read that, I didn't even realize this was from 2006 until your post.