0

问题 1:当我尝试获取每个添加的 mc 的名称时,我会得到最后添加的 mc 的名称。为什么会这样?我处于初级水平。我在互联网上搜索了一段时间,但找不到解决方案。[解决了]

问题 2:我有 4 个位置,下次他们随机获得其他位置时,如何将每个项目(4 个 vruchtItem)随机放置在其中一个位置,例如(在位置 posUp 的 appel,在 posRight 的香蕉等)。

public function vruchtenItemKruis(e:Event):void
{       
    var vruchtArray:Array = new Array("Aardbei", "Appel", "Banaan", "Sinaasappel");

    var i:Number= 0;            
    while (i < vruchtArray.length)
    {   

        var vrucht = vruchtArray[i];                

        switch(vrucht)
        {
            case ("Aardbei"):

                vruchtItem = new Aardbei(kruisWeg.width / 2, 50, 50, 65);

                break;

            case ("Appel"):

                vruchtItem = new Appel(kruisWeg.width / 2,  kruisWeg.height - 50, 50, 60);

                break;

            case ("Banaan"):

                vruchtItem = new Banaan(50, kruisWeg.height / 2, 60, 40);

                break;

            case ("Sinaasappel"):

                vruchtItem = new Sinaasappel(kruisWeg.width - 50, kruisWeg.height / 2, 70, 45);

                break;  
        }

        removeEventListener(Event.ADDED_TO_STAGE, vruchtenItemKruis);

        i++;


        addChild(vruchtItem);
        vruchtItem.name = vrucht;

        trace(vruchtItem.name);
        vruchtItem.buttonMode = true;

        vruchtItem.addEventListener(MouseEvent.MOUSE_OVER, vruchtNaam); 

    }
}

public function randomPosXY(xPos:int, yPos:int):int
{
    xPosition = xPos;
    yPosition = yPos;

    // Select an random positie van 4
    var vruchtPos:Array = new Array("posUp",  "posRight", "posDown", "posLeft");
    var randomPos:int =  Math.floor(Math.random() * vruchtPos.length);
    var posXY:String = vruchtPos[randomPos];

    //trace ("random xy started");

    var tot:Number;

    if(posXY)
    {

        while (tot < vruchtPos.length) {
            var itemPos = vruchtPos[tot];

            switch(posXY)
            {
                case ("posUp"):

                    xPosition = kruisWeg.width / 2;
                    yPosition = 0 + 15;

                    break;

                case ("posDown"):

                    xPosition = kruisWeg.width / 2;
                    yPosition = kruisWeg.height - 15;   

                    break;

                case ("posLeft"):

                    xPosition = 0 + 15;
                    yPosition = kruisWeg.height / 2;

                    break;

                case ("posRight"):

                    xPosition = kruisWeg.width - 15;
                    yPosition = kruisWeg.height / 2;

                    break;
            }
        }       
    }


    return xPosition;
    //return yPosition;
}
4

2 回答 2

0

预先设置您的职位列表,然后在创建每个 MC 时,从列表中抓取一个职位并将其删除。当您添加下一个 MC 时,将只有 3 个位置,随机删除一个。等等。

于 2012-06-26T02:37:02.337 回答
0

You could do something like this for your random positioning function. This assumes your four assets are the only children of a parent container that you'd pass to the function, Super easy to have it work the same way with an array of your assets:

public function randomPosXY(itemParent:DisplayObjectContainer):void
{

    var vruchtPos:Array = new Array("posUp",  "posRight", "posDown", "posLeft");

    var xPosition:Number;
    var yPosition:Number;

    var i:int = itemParent.numChildren;
    while (i--) {
    var randomPos:int =  Math.round(Math.random() * (vruchtPos.length - 1));
    switch(vruchtPos[randomPos]) {
        case ("posUp"):
                xPosition = kruisWeg.width / 2;
        yPosition = 0 + 15;

                break;

            case ("posDown"):

                xPosition = kruisWeg.width / 2;
            yPosition = kruisWeg.height - 15;   
            break;

        case ("posLeft"):

            xPosition = 0 + 15;
            yPosition = kruisWeg.height / 2;

            break;

        case ("posRight"):

        xPosition = kruisWeg.width - 15;
        yPosition = kruisWeg.height / 2;
            break;
    }

    //assign position to current item
    itemParent.getChildAt(i).x = xPosition;
    itemParent.getChildAt(i).y = yPosition;

    //remove the used position from the array
    vruchtPos.splice(randomPos, 1);
    }

}
于 2012-06-27T21:48:25.457 回答