将同一对象的多个实例添加到舞台

时间:2015-06-21 06:32:39

标签: actionscript-3 object

我正在做一个"吐痰"纸牌游戏,我试图能够在舞台上同时拥有3张牌,以便能够拖到一堆。所以我的问题是我如何在舞台上拥有同一个对象的多个实例,但能够为每张卡分配不同的帧等等。以下是了解其背景的代码。

" cardArray"有53个框架,其中52个有卡面,53个是" deck"图片。

//variables, constants
var cardDeck:Array = new Array();
var timer:Timer = new Timer(2000);
var j:int = 0;
var card:MovieClip = new CardArray();
var cardInDeck:Boolean;
const deckSize:int = 52;

//events:
card.addEventListener(MouseEvent.MOUSE_DOWN,fDown);
card.addEventListener(MouseEvent.MOUSE_UP,fUp);
startRandomise();

//This is where the unshuffled "deck" is created - values loaded into the array.
function createDeck():void
{
    var i:int;
    for(i =0; i<deckSize; i++)
    {
        cardDeck[i] = i+1;
    }
}

function randomizeDeck( a : *, b : * ):int 
{
    return (Math.random()>.5) ? 1 : -1;

}

//Grab the value from the (presumably) current frame of the card
function convertCard(cardNo:int):int
{
    var deckPos:int;
    if (cardNo <= deckSize/4)
    {
        deckPos = cardNo;
    }
    else if (cardNo > deckSize/4 && cardNo <= deckSize/2)
    {
        deckPos = cardNo -13;
    }
    else if (cardNo > deckSize/2 && cardNo <=deckSize*3/4)
    {
        deckPos = cardNo -deckSize/2;
    }
    else if (cardNo >39)
    {
        deckPos = cardNo -39;
    }
    return deckPos;
}

btn.addEventListener(MouseEvent.MOUSE_UP,showArray);
function showArray(event:MouseEvent):void
{
    if(j<deckSize /2)
    {
        addChild(card);

        card.gotoAndStop(cardDeck[j]);
        trace(cardDeck[j]+","+deckCompare[j]);
        j++;        
    }
    if(cardInDeck)
    {
        card.x = 200;
        card.y = 200;
        cardInDeck+false;
    }
}



function fDown(evt:MouseEvent)
{
    card.startDrag();
}

function fUp(evt:MouseEvent)
{
    stopDrag();
    if(card.hitTestObject(deck))
    {
        trace("deck: "+convertCard(deck.currentFrame));
        trace("card: "+convertCard(card.currentFrame));
        if(convertCard(card.currentFrame) == convertCard(deck.currentFrame)+1 || convertCard(card.currentFrame) == convertCard(deck.currentFrame)-1 || convertCard(card.currentFrame) == 13 && convertCard(deck.currentFrame) == 1 || convertCard(card.currentFrame) == 1 && convertCard(deck.currentFrame) == 13 || convertCard(deck.currentFrame) == 14)
        {
            deck.gotoAndStop(card.currentFrame);
            removeChild(card);
            cardInDeck=true;
        }
        else
        {
            card.x = 200;
            card.y = 200;
        }
    }
    else
    {
        card.x = 200;
        card.y = 200;
    }
}


function startRandomise():void
{
    createDeck();
    cardDeck.sort(randomizeDeck);
    cardDeck.sort(randomizeDeck);
    trace("random: "+cardDeck); 
    trace("deckCompare: "+deckCompare);
    card.x = 200;
    card.y = 200;
    deck.gotoAndStop(53);
}

1 个答案:

答案 0 :(得分:0)

要拥有多张卡,您需要实例化多张卡。我对卡牌游戏吐痰一无所知,也不了解你的许多功能的目的,但你会想要做这些事情:

创建一个生成(实例化)新卡的函数:

function createCard(faceFrame):MovieClip {
    var tmpCard:MovieClip = new CardArray(); //this creates a new instance of a card 
    tmpCard.addEventListener(MouseEvent.MOUSE_DOWN, cardMouseDown); //make the card call the mouse down function when that event is triggered
    tmpCard.gotoAndStop(faceFrame); //assign the card it's face (frame)
    addChild(tmpCard); //add the card to the screen

    return tmpCard;
}  

然后,在你的鼠标按下处理程序(我将其重命名为更清楚的是什么),你可以执行以下操作来删除鼠标卡:

function cardMouseDown(evt:MouseEvent) {
    //the events currentTarget property is reference to the item that you attached the listener to, so in this case the card who triggered the mouse down
    card = evt.currentTarget as MovieClip; //assign the current clicked card to the global card variable (so we can access it in the mouse up function)
    card.startDrag();

    //add the mouse up listener on the stage, since there are cases where you can drag so fast that the mouse isn't over the item it's dragging (and then if you were to release the mouse at that moment it wouldn't dispatch the event)
    stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUp);
}

然后你的鼠标功能看起来像这样:

function stageMouseUp(evt:MouseEvent) {
    //remove the mouse up listener from the stage
    stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUp);

    card.stopDrag();
    if(card.hitTestObject(deck))
    {
    ......//rest if the code

好像你想在showArray函数中生成一张新卡?所以像这样:

function showArray(event:MouseEvent):void
{
    var newCard:MovieClip = createCard(cardDeck[j]);
    j++

    newCard.y = newCard.x = 200;

    trace(cardDeck[j]+","+deckCompare[j]);            
}

顺便说一句,您应该考虑将您的Card对象称为更合理的内容,例如Card而不是CardArray,因为它不是数组......