Forum Topic: As3: Basic Hittest And If And Else

(1,269 views • 12 replies)

This topic is 1 page long.

<< < > >>
None

hashbrown

Reply To Post Reply & Quote

Posted at: 5/9/07 12:16 AM

hashbrown LIGHT LEVEL 16

Sign-Up: 07/07/05

Posts: 3,077

As3: Main

ok, well someone has to supply some easy things for some total begginers, ill start with some stuff ive recently learned.

First off it would be cool if you could follow along by doing some stuff i tell you, or you could just read it but if your an animator you probably could get more out by following along.

For the first example draw 2 objects, make it easy to tell the difference between them, for instance a stragth line and a curved line.

give your first object the instance name of object 1, instances are found in the properties panel after converting your creation into a movie clip.

next give your second object the instance name of object 2.
instance names are needed so that when you create the code flash knows which objects to use. imagine flash is a blind person, and there are 2 people, but flash doesnt know their names. he doesnt want to be rude so he wont call out hey you. his only chance is to call out random names which is totally useless, so instance names are names for the blind person. the the blind person has names he can communicate with the other people.

now for the script. type in
if (object1.hitTestObject(object2)){
trace("i am touching you");

}

now you probably dont know what that script means, so i will divide it up for you

if (object1.hitTestObject(object2)){

First lets explain the if statement. that tells flash that if this is happening..... and the second part is what comes next. the this part is

(object1.hitTestObject(object2)

so lets continue our sentence. if _______ is happening..., the _____ is what is above. what that means is if object one, is touching object 2 then.....
the { means to start a commandamabob.

trace("i am touching you");

}

so now lets keep going, now we are getting to the commandamabob, which is trace. trace basically means output this text, or say this. this is only visible when you export from flash so it is good to see if things are working properly while making a game.
our completed sentence is
If object one is touching object 2 then say "i am touching you". then } which means close the command.

BUT WAIT HASH!!! what if object one isnt touching object 2

well my child you will use an else command

else {
trace("let me touch you");
}

well hopefully you can guess what that means but i will explain it anyways.

else {

this means if ____ isnt happening then do this...

trace("let me touch you");
}

so if ______ isnt happening then say "let me touch you"

so heres the full code

if (object1.hitTestObject(object2)){
trace("i am touching you");

} else {
trace("let me touch you");
}

i hope this helped people who are starting out.

i might explain hittests in more depths but td is still teaching me the stuff

FREE MMO RPG! PLAY NOW!
"The Elven Prophecy" is the copyright and trademark of Runewaker Entertainment

BBS Signature

None

Alphabit

Reply To Post Reply & Quote

Posted at: 5/9/07 04:14 AM

Alphabit NEUTRAL LEVEL 09

Sign-Up: 02/14/06

Posts: 4,066

Hehe, I got a giggle out of this...
Very nice tutorial nevertheless.


None

KaynSlamdyke

Reply To Post Reply & Quote

Posted at: 5/9/07 06:10 AM

KaynSlamdyke LIGHT LEVEL 16

Sign-Up: 06/25/04

Posts: 4,926

Sounded like an advert for the Nintendo DS

touch me! touch me!

Can you also cover the point hittest that they introduced in AS3 to make this tutorial complete?

Current build for ThreedeeTiles : Monkey
Previous: Lamprey, Mountain Goat (Dead Fork)


None

hashbrown

Reply To Post Reply & Quote

Posted at: 5/9/07 09:24 AM

hashbrown LIGHT LEVEL 16

Sign-Up: 07/07/05

Posts: 3,077

At 5/9/07 06:10 AM, KaynSlamdyke wrote: Sounded like an advert for the Nintendo DS
touch me! touch me!
Can you also cover the point hittest that they introduced in AS3 to make this tutorial complete?

ill learn that today, havent heard about it before

FREE MMO RPG! PLAY NOW!
"The Elven Prophecy" is the copyright and trademark of Runewaker Entertainment

BBS Signature

None

PastryMan

Reply To Post Reply & Quote

Posted at: 6/7/07 10:42 PM

PastryMan FAB LEVEL 12

Sign-Up: 08/19/06

Posts: 931

very nice tut. Humor is the best way to learn.


None

thespectre1

Reply To Post Reply & Quote

Posted at: 6/18/09 09:06 AM

thespectre1 DARK LEVEL 16

Sign-Up: 05/11/07

Posts: 230

So i got a question about HitTesting.
let's say i got a couple copies of "Object2" laying around waiting to be touched.
if i touch them with object1 only 1 copy works, the others don't.
How do I apply 1 hitTest to more than one copy of the object?


None

Paranoia

Reply To Post Reply & Quote

Posted at: 6/18/09 09:25 AM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,699

At 5/9/07 09:24 AM, hashbrown wrote:
At 5/9/07 06:10 AM, KaynSlamdyke wrote: Sounded like an advert for the Nintendo DS
touch me! touch me!
Can you also cover the point hittest that they introduced in AS3 to make this tutorial complete?
ill learn that today, havent heard about it before

It's not complicated and it wasn't introduced in AS3. Basically the old hitTest function just got split into two:

//  AS2
a.hitTest(b);
a.hitTest(b._x, b._x, true);

//  AS3
a.hitTestObject(b);
a.hitTestPoint(b.x, b.y, true);

Which clearly makes far more sense.

BBS Signature

None

Paranoia

Reply To Post Reply & Quote

Posted at: 6/18/09 09:26 AM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,699

Oopsie, completely forgot to check the dates there.

BBS Signature

None

thespectre1

Reply To Post Reply & Quote

Posted at: 6/18/09 09:30 AM

thespectre1 DARK LEVEL 16

Sign-Up: 05/11/07

Posts: 230

At 6/18/09 09:26 AM, Paranoia wrote: Oopsie, completely forgot to check the dates there.

I found it useless to make a new thread, so i revived an old one.
Also to see if people actually read the whole thing.
Seems not tho.
Got an answer for my question?


Angry

thespectre1

Reply To Post Reply & Quote

Posted at: 6/18/09 06:09 PM

thespectre1 DARK LEVEL 16

Sign-Up: 05/11/07

Posts: 230

So, yeah, that means every coder here on NG uses a different hitTest for every single symbol on his flash, OR no one reads a thread till the end.
Pick one NG


Shouting

ajerick

Reply To Post Reply & Quote

Posted at: 7/2/09 11:00 PM

ajerick FAB LEVEL 09

Sign-Up: 03/23/08

Posts: 1

I've a doubt here, I want to check the collision between 2 movieclips, how can I check it in a perfect "pixel" way?, it seems that every movieclip is inside an invisible rectangle that is the same height and weight of the movievlip, even if the movieclip has blank areas the hittest return a true value of everything that collides with the invisible rectangle. Here's an image example xD Pic Here! (the red line represents the invisible rectangle).

I hope you can help me, thanks!


None

grafik2d

Reply To Post Reply & Quote

Posted at: 10/24/09 06:11 PM

grafik2d NEUTRAL LEVEL 10

Sign-Up: 06/29/04

Posts: 164

At 7/2/09 11:00 PM, ajerick wrote: I've a doubt here, I want to check the collision between 2 movieclips, how can I check it in a perfect "pixel" way?, it seems that every movieclip is inside an invisible rectangle that is the same height and weight of the movievlip, even if the movieclip has blank areas the hittest return a true value of everything that collides with the invisible rectangle. Here's an image example xD Pic Here! (the red line represents the invisible rectangle).

the hitTestObject will test the whole movie clip box, the hitTestPoint will check if a single x and y coordinate touches another object. Checking the colision of every pixel of a movie clip would be useless and would slow your game alot. What you can do is use multiple reference point to check for collision.

ex: assumin youre hero as is registration point set in the middle bottom and you want to check if walking on the ground

if(ground.hitTestPoint(myHero.x,myHero.y ,true) || ground.hitTestPoint(myHero.x+myHero.widt h,myHero.y,true) || ground.hitTestPoint(myHero.x-myHero.widt h,myHero.y,true)){
myHero.lowCollision=true
}else{
myHero.lowCollision=false
}


None

grafik2d

Reply To Post Reply & Quote

Posted at: 10/24/09 06:12 PM

grafik2d NEUTRAL LEVEL 10

Sign-Up: 06/29/04

Posts: 164

oh yeah I forgot, divide the with by 2on the second and tird collision check


All times are Eastern Standard Time (GMT -5) | Current Time: 07:23 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!