Forum Topic: As: Collisions - Move To Contact

(3,070 views • 9 replies)

This topic is 1 page long.

<< < > >>
None

TelaFerrum

Reply To Post Reply & Quote

Posted at: 1/12/06 05:59 PM

TelaFerrum NEUTRAL LEVEL 06

Sign-Up: 02/21/05

Posts: 404

AS: Collisions - Move to Contact Position by Tela Ferrum

AS: Main

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.


None

T-H

Reply To Post Reply & Quote

Posted at: 1/12/06 06:01 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

Interesting idea I'll admit, but there are far more clean ways of doing it than a load if if / else evaluations. Post an imageshacked example too please.


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 1/12/06 06:02 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

can we see a sample

Some times my "L" key decides not to work.


None

TelaFerrum

Reply To Post Reply & Quote

Posted at: 1/12/06 06:06 PM

TelaFerrum NEUTRAL LEVEL 06

Sign-Up: 02/21/05

Posts: 404

http://img366.images..=hittestproof0dk.swf

Here you go. I did this to prove to my friend that hitTest actually works.

What do you mean there's a more efficient way? Are you talking about how I named the wall? I would've used onLoad before but then I read something about how onLoad works and got a bit confused by it.

What I thought at first was that when the instance is generated onLoad is run, and later after reading some stuff I thought that it runs when the instance is loaded.


None

Hornby

Reply To Post Reply & Quote

Posted at: 8/6/06 12:58 AM

Hornby DARK LEVEL 21

Sign-Up: 06/22/06

Posts: 4,396

It doesn't work. It just goes right through.


None

FatKidWitAJetPak

Reply To Post Reply & Quote

Posted at: 1/8/09 10:07 PM

FatKidWitAJetPak LIGHT LEVEL 24

Sign-Up: 07/28/07

Posts: 3,767

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. :|
}
}

So here is the code without explanations.

onClipEvent(enterFrame)
{
if(this.hitTest(_root.form))
if(Key.isDown(68))
this._y -=1.3;
{
if(Key.isDown(65))
this._y+=1.3;
}
}

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.

The Fallout 3 Fan Club. Join Today!
Voice Acting TUTORIAL!
Click My Sig Below If You Need A Voice Actor! Oh And I Love Pancakes. ROOOOOOOOOAAAAAAAAARRRRRRRRRRR!!!

BBS Signature

None

FatKidWitAJetPak

Reply To Post Reply & Quote

Posted at: 1/8/09 10:16 PM

FatKidWitAJetPak LIGHT LEVEL 24

Sign-Up: 07/28/07

Posts: 3,767

Oh dear.. I am very sorry but add a collision line where it is added here.

onClipEvent(enterFrame)
{
if(this.hitTest(_root.form))
if(Key.isDown(68))
this._y -=1.3;
{
if(this.hitTest(_root.form))
//Sorry folks.. I forgot to add the line above. XD
if(Key.isDown(65))
this._y+=1.3;
}
}

Ok NOW the character should go up and down when a certain key is pressed ONLY when it is touching the movie clip. ;)

The Fallout 3 Fan Club. Join Today!
Voice Acting TUTORIAL!
Click My Sig Below If You Need A Voice Actor! Oh And I Love Pancakes. ROOOOOOOOOAAAAAAAAARRRRRRRRRRR!!!

BBS Signature

Happy

X-Labs

Reply To Post Reply & Quote

Posted at: 1/12/09 08:56 PM

X-Labs DARK LEVEL 03

Sign-Up: 01/04/09

Posts: 2

A better way for actionscripting for this:
if(!this.hitTest(_root.wall)){

would look like this:
javascript:MakeSmileySelection(15);
if(this.hitTest(_root.wall) == false){

The actionscript isn't wrong, I've been using it in AS2 for a week now and so far no probs.


None

X-Labs

Reply To Post Reply & Quote

Posted at: 1/12/09 08:58 PM

X-Labs DARK LEVEL 03

Sign-Up: 01/04/09

Posts: 2

At 1/12/09 08:56 PM, X-Labs wrote: A better way for actionscripting for this:
if(!this.hitTest(_root.wall)){

would look like this:
if(this.hitTest(_root.wall) == false){

The actionscript isn't wrong, I've been using it in AS2 for a week now and so far no probs

Yeah there's the change it wasn't supposed to say javascript:MakeSmileySelection(1);


None

Nano256

Reply To Post Reply & Quote

Posted at: 1/12/09 09:15 PM

Nano256 DARK LEVEL 13

Sign-Up: 02/12/05

Posts: 1,474

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.

Move on to ActionScript 3.0 already!
The third post below this one is a lie.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 01:40 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!