我在as3内存中开发游戏吗?

时间:2017-04-05 13:41:15

标签: actionscript-3 flash

想知道如何在as3中首先制作记忆游戏:

1 - 每次游戏运行时卡片都是随机的。

2 - 至少有一个条件来验证这些卡是否相同以及它们是否会消失。

3-如果卡不相等则恢复正常

4 - 所有只是一个动画片段,其中包含卡片

感谢您的理解

到目前为止我有这个代码:

import flash.events.MouseEvent;

//variáveis relativo ao score, pattern and so on
var pattern = new Array();
var buttons = new Array();
buttons.push(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p);
var position = 0;

//Dizer aos botões que esperam ser clicados pelo rato
a.addEventListener(MouseEvent.CLICK, Clicked);
b.addEventListener(MouseEvent.CLICK, Clicked);
c.addEventListener(MouseEvent.CLICK, Clicked);
d.addEventListener(MouseEvent.CLICK, Clicked);
e.addEventListener(MouseEvent.CLICK, Clicked);
f.addEventListener(MouseEvent.CLICK, Clicked);
g.addEventListener(MouseEvent.CLICK, Clicked);
h.addEventListener(MouseEvent.CLICK, Clicked);
i.addEventListener(MouseEvent.CLICK, Clicked);
j.addEventListener(MouseEvent.CLICK, Clicked);
k.addEventListener(MouseEvent.CLICK, Clicked);
l.addEventListener(MouseEvent.CLICK, Clicked);
m.addEventListener(MouseEvent.CLICK, Clicked);
n.addEventListener(MouseEvent.CLICK, Clicked);
o.addEventListener(MouseEvent.CLICK, Clicked);
p.addEventListener(MouseEvent.CLICK, Clicked);

function Clicked(clickInfo:MouseEvent){
trace("Clique");
switch(clickInfo.target){
case a:
clickInfo.target.gotoAndStop(2);
break;
case b:
clickInfo.target.gotoAndStop(3);
}
}

我希望你点击你做的任何一个字母出现我决定的4个对象中的任何一个

1 个答案:

答案 0 :(得分:2)

步骤1:您需要创建一个处理翻转的Card类,并显示适合其指定值的图像。基本上,方法如下:

Card.assign(type:int); // To assign a value and tell the card which face to show.
Card.unflip(); // Show back and enable mouse.
Card.flip(); // Show face and disable mouse.

第2步:为随机的卡片组分配相同的值。

// Must contain 16 cards.
var Cards:Vector.<Card> = new Vector.<Card>;
Cards.push(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p);

// Lets make a copy.
var aCopy:Vector.<Cards> = Cards.slice();

// Lets take 8 random pairs and assign them values.
for (var i:int = 1, i <= 8; i++)
{
    var aCard:Card = extractRandom(aCopy);
    var bCard:Card = extractRandom(aCopy);

    aCard.assign(i);
    bCard.assign(i);
}

function extractRandom(list:Vector.<Cards>):Card
{
    // Obtain random card.
    var anIndex:int = list.length * Math.random();
    var result:Card = list[anIndex];

    // Remove it from list.
    // That is why we are working with the copy of the original array.
    list.splice(anIndex, 1);

    return result;
}

第3步:核心循环。

for each (var aCard:Card in Cards)
{
    aCard.addEventListener(MouseEvent.CLICK, onClick);
}

// To store selected cards.
var firstCard:Card;
var secondCard:Card;

// To keep track of user's progress.
var totalPairs:int = 8;
var matchedPairs:int = 0;

function onClick(e:Event):void
{
    // If secondCard is set then user is watching 2
    // wrong cards at he moment. Must ignore clicks.
    if (secondCard) return;

    // Get reference to the clicked card.
    var aCard:Cards = e.currentTarget as Card;

    if (firstCard)
    {
        // Save the second selected card reference.
        secondCard = aCard;
        secondCard.flip();

        if (firstCard.type == secondCard.type)
        {
            // If cards are matched then just leave them open immediately.
            firstCard = null;
            secondCard = null;

            matchedPairs++;

            if (matchedPairs == totalPairs)
            {
                // Win.
            }
        }
        else
        {
            // Otherwise let user watch the for a while and then close.
            setTimeout(unMatch, 1000);
        }
    }
    else
    {
        // Save the first selected card reference.
        firstCard = aCard;
        firstCard.flip();
    }
}

function unMatch():void
{
    firstCard.unflip();
    secondCard.unflip();

    firstCard = null;
    secondCard = null;
}