0

Please forgive my terminology, Im not educated on the proper.

Lets say I have multiple movieclip variables

var rblock1:MovieClip = new Rblock();
var rblock2:MovieClip = new Rblock();
var rblock3:MovieClip = new Rblock();
var yblock1:MovieClip = new Yblock();
var yblock2:MovieClip = new Yblock();
var yblock3:MovieClip = new Yblock();

I have them added to an array

var blockarray:Array = new Array(rblock1, rblock2, rblock3, yblock1, yblock2, yblock3);
var block

I want to create a for loop with an if statement that triggers if a variable is Rblock and not Yblock, for example

for each (block in blockarray)
{ 

    if (block==Rblock)
    { 
         trace("rblock");
    }

}

The issue is that obviously "if (block==Rblock)" doesnt work. How should this be written?

4
  • Case sensitivity. Commented Nov 26, 2018 at 2:38
  • no im not trying to declare the clip, im trying to declare the class, the class is Rblock, the clip is rblock Commented Nov 26, 2018 at 2:40
  • i have edited the question to avoid the confusion that you were right to assume Commented Nov 26, 2018 at 2:42
  • 1
    To check if an instance belongs to a class you use if (block is Rblock) statement. Commented Nov 26, 2018 at 6:06

2 Answers 2

1

You apparently want to check if a block is red or yellow by checking against its class name. You can do it with this:

if (block is Rblock) {...} // yes, red
Sign up to request clarification or add additional context in comments.

Comments

0

I have figured out a work around not really a perfect solution, which will only work for certain scenarios...

if each class has a unique trait you can identify it that way, for example...

if all variables defined by the Rblock class are wider than the Yblock class you could say

if (block.width>x) { trace(Rblock); }

Like I said this is only a work around though and only works for movieclip variables defined by classes that are different, if anyone has the actual solution please post

1 Comment

There are traits, namely class names, or also a common name of a superclass, you can perform checks against class name with if (block is Rblock) (in fact you check if this block is either Rblock or a descendant of Rblock this way, but normally you only check against a leaf class, so this is irrelevant).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.