Forum Topic: Event inside a loop?

(77 views • 3 replies)

This topic is 1 page long.

<< < > >>
Questioning

Petwoip

Reply To Post Reply & Quote

Posted at: 4/16/09 01:10 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 281

My stage has 30 MC's scattered around. How can I detect an onRelease for all 30 of these MC's using a for loop. Here is an idea of what I want:

_root.onEnterFrame = function()
{
     for(i = 1; i <= 30; i++)
     {
          _root["mc" + i].onRelease = function()
          {
               trace("You clicked MC: " + i)
          }
      }
}

Obviously, this code does not work, I just wrote it to illustrate my problem. What should I do? (These 30 MC's have to be movie clips, not buttons)

.

BBS Signature

None

Shinki

Reply To Post Reply & Quote

Posted at: 4/16/09 01:17 PM

Shinki DARK LEVEL 10

Sign-Up: 02/14/05

Posts: 1,571

This will suffice.

for(i = 1; i <= 30; i++)
{
  _root["mc" + i].onRelease = function()
  {
  trace("You clicked MC: " + i)
  }
}

It's important to realise that when you define an onRelease or onEnterFrame etc that you are telling the movieclip what to do when that event occurs. So in the code you posted, you are telling each movieclip what to do when it is clicked correctly, but you are doing that every frame which is overkill as they only need to be told once.

If a picture is worth a thousand words, a game is worth a play.

BBS Signature

None

Shinki

Reply To Post Reply & Quote

Posted at: 4/16/09 01:20 PM

Shinki DARK LEVEL 10

Sign-Up: 02/14/05

Posts: 1,571

Actually, looking at it again the i variable (in the trace) will not function correclty in that example, but the code itself is perfectly fine. If you HAVE to trace, just trace the _name, or store the i value on the movieclip.

If a picture is worth a thousand words, a game is worth a play.

BBS Signature

Elated

Petwoip

Reply To Post Reply & Quote

Posted at: 4/16/09 01:21 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 281

It's important to realise that when you define an onRelease or onEnterFrame etc that you are telling the movieclip what to do when that event occurs. So in the code you posted, you are telling each movieclip what to do when it is clicked correctly, but you are doing that every frame which is overkill as they only need to be told once.

Hah, what's funny is I just wrote that code on my own, and it didn't work. I realized my MC's were all empty in the middle, and had a thin line on the outside, so I couldn't detect a click. After fixing that the code worked!

.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 12:55 PM

<< 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!