Be a Supporter!

As: Collisions - Move To Contact

  • 3,940 Views
  • 9 Replies
New Topic Respond to this Topic
TelaFerrum
TelaFerrum
  • Member since: Feb. 21, 2005
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
As: Collisions - Move To Contact Jan. 12th, 2006 @ 05:59 PM Reply

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.

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Collisions - Move To Contact Jan. 12th, 2006 @ 06:01 PM Reply

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.

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to As: Collisions - Move To Contact Jan. 12th, 2006 @ 06:02 PM Reply

can we see a sample

TelaFerrum
TelaFerrum
  • Member since: Feb. 21, 2005
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to As: Collisions - Move To Contact Jan. 12th, 2006 @ 06:06 PM Reply

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.

PyroGreg
PyroGreg
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 24
Programmer
Response to As: Collisions - Move To Contact Aug. 6th, 2006 @ 12:58 AM Reply

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

FatKidWitAJetPak
FatKidWitAJetPak
  • Member since: Jul. 28, 2007
  • Offline.
Forum Stats
Member
Level 29
Musician
Response to As: Collisions - Move To Contact Jan. 8th, 2009 @ 10:07 PM Reply

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.

FatKidWitAJetPak
FatKidWitAJetPak
  • Member since: Jul. 28, 2007
  • Offline.
Forum Stats
Member
Level 29
Musician
Response to As: Collisions - Move To Contact Jan. 8th, 2009 @ 10:16 PM Reply

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. ;)

X-Labs
X-Labs
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Collisions - Move To Contact Jan. 12th, 2009 @ 08:56 PM Reply

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.

X-Labs
X-Labs
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Collisions - Move To Contact Jan. 12th, 2009 @ 08:58 PM Reply

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);

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to As: Collisions - Move To Contact Jan. 12th, 2009 @ 09:15 PM Reply

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.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature