AS: Collisions - Move to Contact Position by Tela Ferrum
Reccomended reading:
AS: Collisions - Boundaries by Spamburger
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.
onClipEvent(enterFrame){
function move(xmove,ymove){
_x+=xmove;
_y+=ymove;
//See comment 1 for above code
if(hitTest(_root.wall)){
if(xmove>0){
_x-=getBounds(_root).xMax-_root.wall.getBo
unds(_root).xMin+0.1;
}
if(xmove<0){
_x-=getBounds(_root).xMin-_root.wall.getBo
unds(_root).xMax-0.1;
}
if(ymove>0){
_y-=getBounds(_root).yMax-_root.wall.getBo
unds(_root).yMin+0.1;
}
if(ymove<0){
_y-=getBounds(_root).yMin-_root.wall.getBo
unds(_root).yMax-0.1;
}
}
//See comment 2 for code in between the last comment and this
}
if(Key.isDown(Key.LEFT)){
move(-5,0);
}
if(Key.isDown(Key.RIGHT)){
move(5,0);
}
if(Key.isDown(Key.UP)){
move(0,-5);
}
if(Key.isDown(Key.DOWN)){
move(0,5);
}
//See comment 3 for code in between the last comment and this
}
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.