开发者

ActionScript 3: how do you compare if the object where you clicked on is that type of object

开发者 https://www.devze.com 2023-01-04 16:31 出处:网络
I have a red box called mc1_mc and every time when you drag on it you get a new little blue box added to the stage. Yhe idea is that you can drag those blue boxes too. however I dont know how 开发者_开

I have a red box called mc1_mc and every time when you drag on it you get a new little blue box added to the stage. Yhe idea is that you can drag those blue boxes too. however I dont know how 开发者_开发百科to detect them.

this is the code:

var newBlok:Boolean;
var blokIndex:int = 0;
var blokje:blok;
var huidigBlok:DisplayObject;
var prullenBak:DisplayObject = getChildByName("groen_mc");

stage.addEventListener(MouseEvent.MOUSE_DOWN,pickUp);
stage.addEventListener(MouseEvent.MOUSE_UP,dropIt);

function pickUp(event:MouseEvent):void
{
    trace(event.currentTarget);
    trace(event.target);
    trace(event.target.name);

    if (event.target.name == "mc1_mc")
    {
        trace("hoi");

        blokje = new blok;  
        blokje.name = "blokje" + blokIndex;
        blokIndex++;

        addChild(blokje);
        blokje.startDrag(true);

    }

    if (event.target.type == blok)
    {
        trace("blok");
    }

    //blokjeVast = blokje;
}

function dropIt(event:MouseEvent):void
{
    event.target.stopDrag();
}

he wont ever come to the line: trace("blok"); even when the object i clicked on gives:

[object Stage]
[object blok]
blokje0

for the lines.

trace(event.currentTarget);
trace(event.target);
trace(event.target.name);

does anyone know how to do check if its a object of type "blok"?


To check whether an object is of a certain type, you can use the is operator.

So, you should change this:

if (event.target.type == blok)
{
    trace("blok");
}

To this:

if(event.target is blok) 
{
    trace("blok");
}

And if the target is of type blok, you should see the trace.

There's one caveat here. ìs tells you if an object is of a certain type. Since a class can extend other classes and implement interfaces, you should check for the most derived or specific one first (if you want to distinguish between, say, Sprite and MovieClip).

var mc:MovieClip = new MovieClip();

if(mc is MovieClip) {
    trace("is MovieClip");
} else if(mc is Sprite) {
    trace("is Sprite");
}

// even if mc is a MovieClip, your code will never get in the else block
if(mc is Sprite) {
    trace("is Sprite");
} else if(mc is MovieClip) {
    trace("is MovieClip");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消