As3检查计时器用完时是否可以看到影片剪辑

时间:2015-04-22 02:53:57

标签: actionscript-3 flash

几年前发布了一个非常类似的问题,这个有用的答案让我了解了解的一部分,但作为AS3的新手,我需要更多帮助才能达到终点。

这是我尝试做的事情:

  1. 我有9个影片剪辑供我排序。我需要能够检测何时不再需要对其进行分类。
  2. 我想创建一个倒数计时器,从30秒开始倒计时,并显示在屏幕上的动态文本框中(我总是新手;做下面的代码对我来说是一个巨大的延伸,甚至在多次教程之后定时器,我无法让它工作。代码在我的文件中被注释掉;我从下面删除了它。
  3. 我想我需要创建一个项目数组,但不知道如何做到这一点并将其合并到计时器代码和条件语句中。
  4. 认为它可能会是这样的:

    if (time==0 && itemsToSort >0) {
          goToAndPlay (2, "Try Again!");
    }
    if (time==0 && itemsToSort <1){
          goToAndPlay (5, "Great job!")
          cheer.play();
    }
    

    我还没有设置一个数组我还没定义什么时间和项目代表的意思,我不确定上述是否有效,或者在下面的代码中我应该在哪里如果能起作用的话就说出来。

    感谢!!!

    stop();
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import flash.media.Sound;
    
    var objectoriginalX:Number;
    var objectoriginalY:Number;
    var correct:Sound = new Toss();
    var incorrect:Sound = new Swoosh();
    var background:Sound = new Background();
    
    cstrawberry.buttonMode = true;
    cstrawberry.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    cstrawberry.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    csandwich.buttonMode = true;
    csandwich.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    csandwich.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    cnapkin.buttonMode = true;
    cnapkin.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    cnapkin.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    rbottle.buttonMode = true;
    rbottle.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    rbottle.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    rcup.buttonMode = true;
    rcup.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    rcup.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    rfoil.buttonMode = true;
    rfoil.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    rfoil.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    lspork.buttonMode = true;
    lspork.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    lspork.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    ljuice.buttonMode = true;
    ljuice.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    ljuice.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    lyogurt.buttonMode = true;
    lyogurt.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    lyogurt.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    background.play();
    
    function hover(event:MouseEvent):void {
        this.filters = [ new DropShadowFilter() ];
    }
    
    function pickupObject(event:MouseEvent):void {
       event.target.startDrag(true);
       event.target.parent.addChild(event.target);
       objectoriginalX = event.target.x;
       objectoriginalY = event.target.y;
    }
    function dropObject(event:MouseEvent):void {   
       event.target.stopDrag();   
       //check if the leading Letter for both Sprites is the same   
       if (testIfMatch(event.target.name, event.target.dropTarget.parent.name)){
            event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
            event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
            event.target.buttonMode = false;
            event.target.visible = false;
            correct.play();
       }else{      
            event.target.x = objectoriginalX;       
            event.target.y = objectoriginalY;
            incorrect.play();       
       }
    }
    
    function testIfMatch(_drop:String, _target:String):Boolean  {   
    if (_drop.substr(0, 1) == _target.substr(0, 1)) {       
        return true;        
    } else{      
        return false       
    }
    

    }

1 个答案:

答案 0 :(得分:0)

如果您希望有一个剩余时间的可视指示器,使用Timer可能无法为您提供所需的灵活性。此外,您需要注册mouseOvermouseOut事件的按钮才能应用DropShadow

stop();
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.media.Sound;

var objectoriginalX:Number;
var objectoriginalY:Number;
var correct:Sound = new Toss();
var incorrect:Sound = new Swoosh();
var background:Sound = new Background();

// For convenience (and lookup later), we add the buttons to an array.
var btnArray = [cstrawberry, csandwich, cnapkin, rbottle, rcup, rfoil, lspork, ljuice, lyogurt];

// Loop through the array, and register each to the events we need.
for each (var button in btnArray) {
    button.buttonMode = true;
    button.addEventListener("mouseOver", buttonEvents);
    button.addEventListener("mouseOut", buttonEvents);
    button.addEventListener("mouseDown", buttonEvents);
    button.addEventListener("mouseUp", buttonEvents);
}

background.play();

function buttonEvents(e:MouseEvent):void {
    // Here we handle all button states
    // Simplify references to the current button by creating a local var.
    var btn:MovieClip = e.currentTarget as MovieClip;

    switch (e.type) {
        case "mouseOver":
            // Show the dropdown.
            btn.filters = new Array(new DropShadowFilter(10, 90, 0, 0.25, 10, 10));
            break;
        case "mouseOut":
            // Hide the dropdown
            btn.filters = new Array(new DropShadowFilter(0, 90, 0, 0, 10, 10));
            break;
        case "mouseDown":
            // Drag the button and store the original locs
            btn.startDrag(true);
            btn.parent.addChild(btn);
            objectoriginalX = btn.x;
            objectoriginalY = btn.y;
            break;
        case "mouseUp":
            // Drop the drag, and evaluate if it was correct
            btn.stopDrag();   

            if (testIfMatch(btn.name, btn.dropTarget.parent.name)){
                btn.removeEventListener("mouseDown", pickupObject);
                btn.removeEventListener("mouseUp", dropObject);
                btn.buttonMode = false;
                btn.visible = false;
                correct.play();
            } else {
                btn.x = objectoriginalX; 
                btn.y = objectoriginalY;
                incorrect.play(); 
            }
            break;
    }
}

function testIfMatch(_drop:String, _target:String):Boolean {
    //check if the leading Letter for both Sprites is the same    
    if (_drop.substr(0, 1) == _target.substr(0, 1)) { 
        return true;  
    } else {
        return false ;
    }
}

// Let's create something to look at the countdown.
var countdownTime:int = 30;
var countdown:TextField = new TextField();
countdown.text = "Time: " + countdownTime;
addChild(countdown);


stage.addEventListener(Event.ENTER_FRAME, updateCounter);
var lastSecond:Number = flash.utils.getTimer();
function updateCounter(e:Event):void {
    var now:Number = flash.utils.getTimer();

    // If more than one second has past, reset the lastSecond to now, and update the counter.
    if (now - lastSecond > 1000) {
        lastSecond += 1000;
        countdownTime--;
        countdown.text = "Time: " + countdownTime;
        if (countdownTime < 1) {
            stage.removeEventListener(Event.ENTER_FRAME, updateCounter);

            // Count how many are still visible
            var visibleButtons:int = 0;
            for each (btn in btnArray) {
                if (btn.visible) {
                    visibleButtons++;
                }
            }

            if (visibleButtons > 0) {
                goToAndPlay (2, "Try Again!");
            } else {
                goToAndPlay (5, "Great job!")
                cheer.play();
            }
        }
    }
}