Be a Supporter!

For loop/hittest not working

  • 327 Views
  • 5 Replies
New Topic Respond to this Topic
andy70707
andy70707
  • Member since: Sep. 30, 2007
  • Offline.
Forum Stats
Member
Level 25
Game Developer
For loop/hittest not working 2008-09-27 13:47:22 Reply

What the hell, this dosen't work:

for(i=0; i<100; i++){
		if (_root.square.hitTest(_root["b"+boxcount])) {
			_root.yspeed = 5;
			trace ("meh")
		}
	}

and I have another code to generate MCs with the instance name b and then a letter according to when its generated (b1, b2, b3 etc) And I want to hittest them all. So my above code should make a new hitTest with b(currentnumberofboxcount) until it gets to 100 (I would idealy like it to go on forever, or atleast when it gets to about 15 and object number 1 is offscreen, to thenhittest 2-16 and so forth) But this code at the moment does NOTHING. If it helps, im amking a pushies style game, and it has to be under 1kb. Sorry for bieng a total noob. I did everything else on my own, but ive never used multiple object hittests with randomly generated objects before.


My websites: MayesMods | FireStorm | I'm also on almost every other website in existence, mostly under the username: andy70707 (youtube: brainiac777, eBay: 10andy70707).

BBS Signature
xedon
xedon
  • Member since: Sep. 6, 2007
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to For loop/hittest not working 2008-09-27 13:52:46 Reply

At 9/27/08 01:47 PM, andy70707 wrote: What the hell, this dosen't work:

for(i=0; i<100; i++){
if (_root.square.hitTest(_root["b"+boxcount ])) {
_root.yspeed = 5;
trace ("meh")
}
}

You have used i so you will probably need

for(i=0; i<100; i++){
if (_root.square.hitTest(_root[i])) {
_root.yspeed = 5;
trace ("meh")
}
}

idk if it will work


lol

andy70707
andy70707
  • Member since: Sep. 30, 2007
  • Offline.
Forum Stats
Member
Level 25
Game Developer
Response to For loop/hittest not working 2008-09-27 13:54:44 Reply

no, i is the variable for the for loop, and boxcount is to add a number to the next randomly generated box.


My websites: MayesMods | FireStorm | I'm also on almost every other website in existence, mostly under the username: andy70707 (youtube: brainiac777, eBay: 10andy70707).

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to For loop/hittest not working 2008-09-27 14:51:16 Reply

At 9/27/08 01:54 PM, andy70707 wrote: no, i is the variable for the for loop

Which you aren't actually using at all.

You're code is kind of the equivalent of giving someone directions to town, and then simply tell them to go around a roundabout 100 times then stop.

I'm not going to tell you how to fix your code, because it is a horrible method of doing what you are intending; to iterate over clips in this manner, when generating the clips you should be storing them an array to be iterated over; it's much cleaner, better practice, and also runs faster.


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Musician
Musician
  • Member since: May. 19, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to For loop/hittest not working 2008-09-27 15:19:27 Reply

for(i=0; i<boxcount; i++){
		if (_root.square.hitTest(_root["b"+i])) {
			_root.yspeed = 5;
			trace ("meh")
		}
	}

Should work.

The problem with your code is that you referenced boxcount instead of i. boxcount is the variable that you used to to set the movie clips on the stage I'm assuming, but if you want to make effective use of the for loop, you have to reference the changing variable "i".

I'm not sure if I'm being very coherent so I'll explain in more detail. When you declare a for loop, you specify 3 parameters.

(i=0;i<100;i++)
i=0

is the parameter that defines where the variable "i" starts. so in this example "i" will start at 0.

i<100

is the parameter that sets what conditions must be met for the for loop to continue. so in this example, "i" will end when "i" is no longer less than 100.

i++

is the action that is performed to "i" at the end of the loop. so in this example, at the end of the loop, the variable "i" will be increased by one at the end of each loop.

now that you understand what these parameters are for, this is how a for loop works.

in this example:

for(i=0; i<boxcount; i++){
		if (_root.square.hitTest(_root["b"+i])) {
			_root.yspeed = 5;
			trace ("meh")
		}
	}

i will loop through this code several times, depending on how high boxcount is. since i starts at zero, the first cycle the loop performs will be the equivelent of this:

if (_root.square.hitTest(_root.b0)) {
	_root.yspeed = 5;
	trace ("meh")
}

at the end of this cycle, the for loop will perform the action stated in the third parameter (i++), and add one to the variable i. it will then run through the loop again using the new variable of i. thus the second cycle will look like this:

if (_root.square.hitTest(_root.b1)) {
	_root.yspeed = 5;
	trace ("meh")
}

The for loop will continue like this until the second parameter (i<boxcount) is no longer satistfied.

Understanding that, the error in your original code:

for(i=0; i<100; i++){
		if (_root.square.hitTest(_root["b"+boxcount])) {
			_root.yspeed = 5;
			trace ("meh")
		}
	}

was simply that you did not reference i inside of your for loop. so instead of checking hitTest for every bullet, it only checked the hitTest for 1 bullet 100 times (the 1 bullet presumably being the last on being placed on the stage).


I have no country to fight for; my country is the earth; I am a citizen of the world
-- Eugene Debs

andy70707
andy70707
  • Member since: Sep. 30, 2007
  • Offline.
Forum Stats
Member
Level 25
Game Developer
Response to For loop/hittest not working 2008-09-27 16:42:07 Reply

OMG! Thanks ever so much! I thought i'd never fix that problem. Atleast I know how yo use for loops now!


My websites: MayesMods | FireStorm | I'm also on almost every other website in existence, mostly under the username: andy70707 (youtube: brainiac777, eBay: 10andy70707).

BBS Signature