Forum Topic: Multiple hitTest?

(147 views • 7 replies)

This topic is 1 page long.

<< < > >>
Questioning

jozojozojozo

Reply To Post Reply & Quote

Posted at: 6/21/08 05:26 PM

jozojozojozo EVIL LEVEL 14

Sign-Up: 09/05/05

Posts: 499

Okay, so i'm making a tank game, where you shoot missiles etc, but i'm having a problem..

How do you make it so that when it hits a object, the object will preform the script?

So far, the hitTest code is

onClipEvent (enterFrame) {
if (this.hitTest(_root.bullet)) {
_root.bullet.spd =0
}
}

Although that does nothing at all, I know its because there are multiple bullets, but what would I call the bullets? :S

Thanks.

LOL @ this review
Great i love it. A must if you love blowin peoples heads off.
For game http://www.newgrounds.com/portal/vi ew/283047


Goofy

zrb

Reply To Post Reply & Quote

Posted at: 6/21/08 06:07 PM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,549

Although that does nothing at all, I know its because there are multiple bullets, but what would I call the bullets? :S

Either:
- Use a for loop. This is the best way.
Or
- Make a hitTest for each bullet (bullet1,bullet2 etc).

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

None

Neo-13

Reply To Post Reply & Quote

Posted at: 6/21/08 06:33 PM

Neo-13 LIGHT LEVEL 21

Sign-Up: 06/09/07

Posts: 1,475

At 6/21/08 06:07 PM, zrb wrote:
Although that does nothing at all, I know its because there are multiple bullets, but what would I call the bullets? :S
Either:
- Use a for loop. This is the best way.
Or
- Make a hitTest for each bullet (bullet1,bullet2 etc).

Yeah, for loops are amazing. They make things so convenient.

for (init; condition; next){
	// stuff
}

Usually you would use a variable such as i.
e.g.

for (i = 0; i < 5; i++){
	trace(i);
}

What that essentially does, is make i = 0, then keeps incrementing (i++) the variable i while it satisfies i < 5.

If you place this code into a new flash document, in the output box, you should see:

0
1
2
3
4

BBS Signature

None

jozojozojozo

Reply To Post Reply & Quote

Posted at: 6/21/08 06:55 PM

jozojozojozo EVIL LEVEL 14

Sign-Up: 09/05/05

Posts: 499

Sorry, I'm not really sure what goes where..

Oh yea, instead of 5 i changed it to 500000, don't try it. :(

LOL @ this review
Great i love it. A must if you love blowin peoples heads off.
For game http://www.newgrounds.com/portal/vi ew/283047


None

Neo-13

Reply To Post Reply & Quote

Posted at: 6/21/08 06:59 PM

Neo-13 LIGHT LEVEL 21

Sign-Up: 06/09/07

Posts: 1,475

At 6/21/08 06:55 PM, jozojozojozo wrote: Sorry, I'm not really sure what goes where..

Oh yea, instead of 5 i changed it to 500000, don't try it. :(

Lol, yeah perhaps I should have mentioned the dangers.

Just don't try leaving the loop blank like this:

for ( ; ; ) {

}

That puts it in an infite loop and will crash Flash. If you feel the need to try it out, save first!!!

BBS Signature

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 6/21/08 07:08 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,008

At 6/21/08 06:55 PM, jozojozojozo wrote: Oh yea, instead of 5 i changed it to 500000, don't try it. :(

Unless you're using ActionScript 3.0 ;)

BBS Signature

None

jozojozojozo

Reply To Post Reply & Quote

Posted at: 6/21/08 07:09 PM

jozojozojozo EVIL LEVEL 14

Sign-Up: 09/05/05

Posts: 499

At 6/21/08 06:59 PM, Neo-13 wrote:
At 6/21/08 06:55 PM, jozojozojozo wrote: Sorry, I'm not really sure what goes where..

Oh yea, instead of 5 i changed it to 500000, don't try it. :(
Lol, yeah perhaps I should have mentioned the dangers.

Just don't try leaving the loop blank like this:

for ( ; ; ) {

}

That puts it in an infite loop and will crash Flash. If you feel the need to try it out, save first!!!

Okay thanks, but I have no clue what to do lol :P

So far, I have bullets, and I added this code to a object:

onClipEvent (enterFrame) {
for (i=0; i<5; i++) {
trace(i);
if (this.hitTest(_root["bullet"+i])) {
_root["bullet"+i].gotoAndStop(2);
}
}
}

That is what I thought would work, but it didn't, please help ;C

LOL @ this review
Great i love it. A must if you love blowin peoples heads off.
For game http://www.newgrounds.com/portal/vi ew/283047


None

Neo-13

Reply To Post Reply & Quote

Posted at: 6/21/08 08:03 PM

Neo-13 LIGHT LEVEL 21

Sign-Up: 06/09/07

Posts: 1,475

At 6/21/08 07:09 PM, jozojozojozo wrote:
At 6/21/08 06:59 PM, Neo-13 wrote:
At 6/21/08 06:55 PM, jozojozojozo wrote: Sorry, I'm not really sure what goes where..

Oh yea, instead of 5 i changed it to 500000, don't try it. :(
Lol, yeah perhaps I should have mentioned the dangers.

Just don't try leaving the loop blank like this:

for ( ; ; ) {

}

That puts it in an infite loop and will crash Flash. If you feel the need to try it out, save first!!!
Okay thanks, but I have no clue what to do lol :P

So far, I have bullets, and I added this code to a object:

onClipEvent (enterFrame) {
for (i=0; i<5; i++) {
trace(i);
if (this.hitTest(_root["bullet"+i])) {
_root["bullet"+i].gotoAndStop(2);
}
}
}

That is what I thought would work, but it didn't, please help ;C

Your problem here is probably the instance names. You may want to be using attachMovie:

onClipEvent (enterFrame) {
	for (i=0; i<5; i++) {
		trace(i);
		bulletFire(i);
		if (this.hitTest(_root["bullet"+i])) {
			_root["bullet"+i].gotoAndStop(2);
		}
	}
	function bulletFire(n:Number):Void {
		this.attachMovie("BulletMC", "bullet"+n, this.getNextHighestDepth); // "BulletMC" is the name of the bullet movie clip which must be exported for actionscript in linkage. 
		// This will create a new instance of BulletMC at runtime and give each one an instance name of bullet+n (n is equal to i). This means you will have bullet0, bullet1, bullet2, etc.
		// Note: this alone will not do it, it's just an outline. Look up attachMovie in flash help (F1).
	}
}

Hopefully this will give you some hints (fingers crossed). ;D

BBS Signature

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