Search code examples
actionscript-3air

Count how many instance in a specific area (AS3 code)


I've made this little game in AS3 code (AIR), where the user can drag and drop stars in different areas. The purpose is to put a certain number of stars in each area.

How can I do to count the number of stars the user puts in a area ?

On stage I've got : area1, area2 and area3

and

star1,star2,... to star10

Here's my code so far :

function dragObject(e:MouseEvent):void { getPosition(e.target);

        e.target.startDrag(true);
    }



         function stopDragObject(e:MouseEvent):void
            {
            if (e.target.name == "area1" &&
    //I think I should put something here to say area1 has been hit 4 times (hitTestObject for example ?)){
    trace("Sucess");
}

Any advice ?

EDIT

I was thinking something like that maybe :

         function stopDragObject(e:MouseEvent):void{

            if (e.target && e.target.hitTestObject(getChildByName("area1"))){

        e.target.stopDrag();
        count++;

        }
            else{
            e.target.stopDrag();
        }
    }

So now, when I'm placing an instance in area1, it's adding "1". But how can I remove instance from area 1 and tell it to do count-- when it's removed from area 1 ?


Solution

  • I think, You should try this make count-- when you drag object and count++ on droped area.

    function dragObject(e:MouseEvent):void 
    { 
        getPosition(e.target);
        e.target.startDrag(true);
        count--;
    }
    
    
    function stopDragObject(e:MouseEvent):void
    {
        if (e.target && e.target.hitTestObject(getChildByName("area1")))
        {
            e.target.stopDrag();
            count++;
        }
        else
        {
            e.target.stopDrag();
        }
    }
    

    I am not sure but might it works.