Skip to main content
Participant
February 5, 2011
Answered

MC placed multiple times and given same instance name?

  • February 5, 2011
  • 1 reply
  • 477 views

Hi everyone, I am pretty new to code in flash.

I am mostly used to use object oriented programming, but in flash I'm doing everything in one frame.

I am making a car game. Kinda GTA style, but very simple. You are a police car, and you chase a theif through a city.

The theif uses ground nodes to know where he can drive, and where he can turn.

basically, below is the stage. I have placed these objects directly onto the stage, and given some of them instance names.:

_________________________________

|      O

|  O ©

|            _______________________

|            |

|            |

|            |

|            |

|      ^     |

|      |     |

|     X     |

The © is a MC named node_switch.

The O's are the two objects of the same MC, which both are named node_stop

The X is the theif (mc), and he is driving upward along the road.

When theif hits node_switch, he will look up,down, left and right if any node_stop is placed.

This is done with hitTestObject. the theif has invisible "beams" in all 4 directions, inside it's MC.

The problem is as following:

When multiple nodes are placed on the stage, the theif only collides with one of them.

As I said, they have the same instance name.

Is there a way to be able to place them directly on the stage, give them all the same instance name, and make a for loop where it goes through all of them and checks collision?

Edit:

Or is it maybe possible to place them directly on the stage, give them instance names such as "node_switch01", "node_switch02", and then check for them all at the same time in a for loop, or in some other way? there will be lots of nodes, so...

This topic has been closed for replies.
Correct answer Ned Murphy

If you name them all differently you will have an easier time of it.  To loop thru them with the naming convention you suggested you can use the bracket notation, though it would be easier to get rid of the leading zeroes...

for (var i:uint=1; i<=numNodes; i++){

     if(thief.hitTestObject(this["node_switch"+String(i)]){

          // don't go this way

     }

}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 5, 2011

If you name them all differently you will have an easier time of it.  To loop thru them with the naming convention you suggested you can use the bracket notation, though it would be easier to get rid of the leading zeroes...

for (var i:uint=1; i<=numNodes; i++){

     if(thief.hitTestObject(this["node_switch"+String(i)]){

          // don't go this way

     }

}

Pandab0yAuthor
Participant
February 5, 2011

Thank you! I will try it, and then update this post wether I succeded or not!

edit:

no, not working.

You did some typos and missed some brackets in the code, and I don't think i corrected it correctly xD.

edit2:

I mean.. it's working !

I actually managed to fix the typos, but I did some typos of my own too . mixed some uppercase/lowecase letters in my instance names.

Ned Murphy
Legend
February 5, 2011

You're welcome