PDA

View Full Version : hitTest basic help



dec27
November 22nd, 2007, 02:30 AM
hi everybody,

I'm experimenting and learning... please be patient!

There are 25 objects moving randomly on stage.
The hero moves up and down

What I want is to know is when the hero is being hit by the intruders, so far this is what I have:

onClipEvent (enterFrame) {
for (i=0; i<=25; i++) {
var dots = _root["dot"+i];
trace (dots)
if (_root.box_mc, hitTest(dots)) {
_root.text = "Collision Detected";
} else {
_root.text = "No Collision";
}
}
}


thanks in advanced.

:pac:

ArmoredSandwich
November 22nd, 2007, 02:44 AM
[quote=dec27;2247249]hi everybody,

I'm experimenting and learning... please be patient!

ActionScript Code:

onClipEvent (enterFrame) {
for (i=0; i<25; i++) {
if (_root.box_mc.hitTest(_root["dot"+i])) {
_root.text = "Collision Detected";
} else {
_root.text = "No Collision";
}
}
}






I think you got most of it right. Might wanna check the actionscript dictionary how it exactly should work. You can still put the dot in a var, but if you dont really do much with it theres no point imo.. (cant hurt either).

dec27
November 22nd, 2007, 02:50 AM
you mean something like this:


onClipEvent (enterFrame) {
for (i=0; i<=25; i++) {
var dots = _root["dot"+i];
if (_root.box_mc.hitTest(dots)) {
_root.text = "Collision Detected";
} else {
_root.text = "No Collision";
}
}
}

ArmoredSandwich
November 22nd, 2007, 02:53 AM
yes, exactly.

dec27
November 22nd, 2007, 02:58 AM
not working!
also tried it before posting...

:sigh:

ArmoredSandwich
November 22nd, 2007, 03:07 AM
not working!
also tried it before posting...

:sigh:

hmmm, im not home atm so i cant test it out.

Maybe your text stuff isnt working, try trace first...

Also i dont know what you coded this on. But if this is on the hero movieclip you can also use this.hitTest(_root["dot" + i])

Same for the dots. If (this.hitTect(_root.box_mc)){

Anyway, i can help you more when i get home. Maybe some one else on this forum can do more in the mean time, fe see the type we didnt see. :P

ArmoredSandwich
November 22nd, 2007, 07:56 AM
okay. I figured it out. Your code is correct! But there is something you didnt thought of. You check for 25 dots to hittest. If you hittest the first dot your textfield changes to "collision detected". However if dot nr. 2 isnt hittesting the text field changes to "No collision".

So you shouldnt have the else statement or its ****ed up. Instead do something like this:



_root.stat_txt = "no collision detected";
for (i = 0; i < totalDots; i++)
{
if (_root.unit.hitTest(_root["dot" + i]))
{
_root.stat_txt = "collision detected";
}
}


That works perfectly! I have an example .fla if youd like to see it yourself ;) Just ask for it :)

dec27
November 22nd, 2007, 01:29 PM
Thanks!

the .fla would be great, could you post it for me?

:nerd:

ArmoredSandwich
November 23rd, 2007, 09:33 AM
www.wonima.demon.nl/other/dec27.zip

dec27
November 24th, 2007, 11:37 PM
thanks for the .fla :)

are you using as3? :cantlook:

ArmoredSandwich
November 25th, 2007, 08:42 AM
thanks for the .fla :)

are you using as3? :cantlook:

no :P

but i kinda think i know why you ask that, I made a class to get a random number. And since you dont have that class the code wont really work.

change
RandomNumber.getRandomNumber(min, max)
into
(Math.floor(Math.random()*(max-min+1)) + min);

sorry for not fixing this :(

dec27
November 26th, 2007, 12:52 AM
I appreciate very much your help!
:)

it works perfectly fine now :lol:
thanks a lot.