如果一个movieclip&& amp;的按钮1需要AS3帮助。单击另一个动画片段的button2然后单击gotoAndPlay

时间:2016-02-25 17:23:00

标签: actionscript-3

我制作了一张记忆卡游戏,其中有24张卡片图像,用户必须从中找到7张图片才能获胜。我是actionscript 3的初级代码,所有代码都在框架而不是文档或类文件。所有24张卡片图像都是动画片段,在7张图像的集合中,我制作了一个按钮,其中第二个状态与获胜图像位于同一层。所有卡都是从库中动态创建的,而不是在舞台上创建的。 我的问题是我曾经尝试过的,直到最后的代码,为了赢得没有成功我非常绝望,请帮助我.....我不能让两个动画片段通过变量进行交流。

enter image description here

主时间轴第6帧有此代码

*uintptr

在七个按钮中的每个按钮上,每个按钮都在一个影片剪辑中,我有这个代码:

uint32

1 个答案:

答案 0 :(得分:1)

我必须警告你,将代码放入MovieClip是一种过时的方法,应该避免。将代码放入不同的框架总会引起更多麻烦。但是,在阅读完您的代码后,我相信这应该可以回答您的问题。

请注意,大部分解释都在代码的注释中,因此请继续阅读。

import flash.text.*;
import flash.display.Stage;

/*You need a reference to each of these library instances, but you were creating
two references to them; (1) a variable, and (2) an array entry. You can create
this data once, thinking of each object as a individual "card", and add any
additional data to it together in the same place.  Most importantly, this
structure allows us to use array.sortOn().*/
var deck:Array = [
    {"img":new logo1, "valid":true},
    {"img":new pic2, "valid":false},
    {"img":new pic3, "valid":false},
    {"img":new logo2, "valid":true},
    {"img":new pic5, "valid":false},
    {"img":new pic6, "valid":false},
    {"img":new pic7, "valid":false},
    {"img":new logo3, "valid":true},
    {"img":new pic9, "valid":false},
    {"img":new pic10, "valid":false},
    {"img":new logo4, "valid":true},
    {"img":new pic12, "valid":false},
    {"img":new pic13, "valid":false},
    {"img":new pic14, "valid":false},
    {"img":new pic15, "valid":false},
    {"img":new logo5, "valid":true},
    {"img":new pic17, "valid":false},
    {"img":new logo6, "valid":true},
    {"img":new pic19, "valid":false},
    {"img":new pic20, "valid":false},
    {"img":new logo7, "valid":true},
    {"img":new pic22, "valid":false},
    {"img":new pic23, "valid":false},
    {"img":new pic24, "valid":false}
];

/*You mentioned you were having difficulty getting your MovieClips to communicate
with eachother and track the success of valid clicks to "win".  You can see we've
added a property "valid" which let's us know if it's a valid option for "winning".
We'll later add the state of it's "success".*/

// Because your locations were explicit pairs, it's also a good idea to keep these
// together, if nothing more than for maintainability & legibility.
var locs:Array = [
    {"x":121, "y":173},
    {"x":321, "y":173},
    {"x":521, "y":173},
    {"x":731, "y":173},
    {"x":937, "y":173},
    {"x":1136, "y":173},
    {"x":120, "y":346},
    {"x":320, "y":346},
    {"x":520, "y":346},
    {"x":732, "y":346},
    {"x":938, "y":346},
    {"x":1138, "y":346},
    {"x":120, "y":534},
    {"x":321, "y":534},
    {"x":520, "y":534},
    {"x":730, "y":534},
    {"x":938, "y":534},
    {"x":1135, "y":534},
    {"x":121, "y":716},
    {"x":322, "y":716},
    {"x":520, "y":716},
    {"x":732, "y":716},
    {"x":938, "y":716},
    {"x":1135, "y":716}
];

// Nulling of the timer & removal of the event listener wasn't strictly necessary.
// Simply use the second argument of Timer(millisecondDelay, repeatCount).
var timer:Timer = new Timer(5000, 1);

init();
function init():void {
    // Here, we've replaced your foo() with an initialization function where all
    // one-time stuff gets done.  
    timer.addEventListener(TimerEvent.TIMER, afterWaiting);
    timer.start();

    // Rather than checking within each card, we'll add the checks from the main stage.
    // This also means you can get rid of the code inside the clips.
    for each (var card:Object in deck) {
        card.img.butpic1.addEventListener("click", checkCard);
    }

    showShuffledCards();
}


function showShuffledCards():void {   
    removeCards();
    shuffleCards();
    placeCards();
}

function removeCards():void {
    for (var i:int = 0; i < deck.length; i++) {
        // Ensure it needs to be removed, before attempting it.
        if (deck[i].img.parent != null) {
            removeChild(deck[i].img);
        }
    }
}

function shuffleCards():void {
    /*Rather than keeping two arrays, and the shuffle only working once, we can
    use sortOn() the existing array by changing the value of the property we're
    sorting with.  This effectively shuffles the deck, and allows us to shuffle
    indefinitely.*/
    for each (var card:Object in deck) {
        card.order = Math.random();
    }

    deck.sortOn("order", Array.DESCENDING | Array.NUMERIC);
}

function placeCards():void {
    // placeShuffledCards was doing the same thing as this function,
    // so we simply use this one.
    for (var i:int = 0; i < deck.length; i++) {
        addChild(deck[i].img);
        deck[i].img.x = locs[i].x + 20;
        deck[i].img.y = locs[i].y;  
    }
}

function afterWaiting(event:TimerEvent):void {
    // Rather than explicityly name each object, because we have them in a fancy
    // array, we can write the command once, and call it on each card in the deck.
    for each (var obj:MovieClip in deck) {
        obj.gotoAndPlay(10);
    }
}

function checkCard(e:Event):void {
    // Search the deck for this card.
    var successCount = 0;
    for each (var card:Object in deck) {
        if (e.currentTarget == card.img && card.valid) {
            // If the card clicked is valid for winning, 
            // add the text and update its status.
            card.img.gotoAndPlay(1);

            var myTextBox:TextField = new TextField();    
            myTextBox.text = "Σωστό Συνέχισε!";  
            myTextBox.border = true;    
            myTextBox.borderColor = 0x000000; 
            myTextBox.width = 180;    
            myTextBox.height = 87;    
            myTextBox.x = -77;    
            myTextBox.y = -126; 
            var myFormat:TextFormat = new TextFormat(); 
            myFormat.color = 000000;    
            myFormat.size = 24;
            myFormat.align = TextFormatAlign.CENTER
            myTextBox.background = true;    
            myTextBox.backgroundColor = 0xFFF000;
            myTextBox.setTextFormat(myFormat);
            card.img.addChild(myTextBox);

            card.success = true;
        }

        // While we're looping through the deck, we'll also count up the number
        // of successful clicks.
        if (card.hasOwnProperty("success") && card.success == true) {
            successCount++;
        }
    }

    // If the count of successful clicks is 7, go to the "win" frame.
    if (successCount == 7) {
        gotoAndPlay("win")
    }
}

<强>附录

由于您尚未发布许多问题,因此您可能不熟悉StackOverflow的规则/习惯。发布代码时,请使用该内容更新您的问题。它更容易阅读,并且可以格式化(与评论不同)。

如何调试

超越勾选标记&#34; Permit Debugging&#34;在发布设置&amp;保持control-shift-enter以调试模式启动,你所要做的就是熟悉可用的帧;值得一提的是#34; Call Stack&#34;,&#34; Variables&#34;,&#34; Output&#34;和您的语法编辑器。

Code break

在这种情况下,您可以看到程序在第90行的init()功能中停止。&#34;输出&#34;窗口告诉我们某些东西不存在,而且从90上列出的内容来看,它必须是cardimglogo1,或checkCard

现在,我们知道,前两个存在,最后一个存在,但logo1(特别是在此位置)不存在。在左侧,您可以在程序的运行时中看到当前代码范围可用的变量。我们可以挖掘card内部并找到img属性,从这里我们确实可以确认logo1内没有img属性。一旦你知道窗户是如何工作的,那就非常简单吧? :)

当你分享你的文件时,(并且我会重复,因为你对SO来说相对较新),我已经冒昧地修复了你的fla文件。我还更进一步向您展示了下一步可能接下来的代码,删除框架代码,以及完全从具有零嵌入图像的代码创建功能/用户界面。你可以download it from Dropbox

memory game