【问题标题】:Am I developing a game in as3 memory?我是在 as3 内存中开发游戏吗?
【发布时间】:2017-04-05 13:41:14
【问题描述】:

想知道如何在你第一次拥有的 as3 中制作记忆游戏:

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

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

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

4-all 只是一个包含卡片的动画片段

感谢您的理解

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

导入 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 个对象中

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    第 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2018-03-27
      • 2017-09-07
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多