AS3单击按钮/ mc时显示和隐藏动画片段/图像

时间:2014-12-18 10:13:52

标签: actionscript-3 flash movieclip

我想制作类似http://www.bakedbymelissa.com/checkout/CustomizerCreator.aspx

的内容

现在我将正方形(square1square2square3square4)作为纸杯蛋糕和圆圈(circle1circle2circle3circle4)作为按钮。如果我点击circle1,则应显示square1,依此类推。 AS有一个单独的图层,形状都在一个图层中。我把正方形放在彼此的顶部,但问题是,当我点击按钮时,正方形显示出来但是它真的不能被看见,因为它位于它顶部的正方形下方。你是如何做到这一点,以便出现相应的方块,出现的前一个方块消失了?你知道的任何替代方法都可以解决问题。

my stage looks like this

AS3:

import flash.display.MovieClip; 
import flash.events.Event;  
import flash.events.MouseEvent;  

import fl.transitions.Tween;  
import fl.transitions.TweenEvent;  
import fl.transitions.easing.*;  

var fadeIn:Tween; 

var thisCircle:MovieClip; 
var thisSquare:MovieClip; 

var circles:Array = new Array(circle1,circle2,circle3,circle4); 
var squares:Array = new Array(square1,square2,square3,square4); 

for(var i:Number = 0; i < circles.length; i++) 
{ 
 thisCircle = circles[i]; 
 thisCircle.buttonMode = true; 
 thisCircle.id = i; 
 thisCircle.addEventListener(MouseEvent.CLICK, doFadeIn); 

 thisSquare = squares[i]; 
 thisSquare.alpha = 0; 
} 

function doFadeIn(e:MouseEvent):void  
{ 
 e.currentTarget.mouseEnabled = false; 
 trace(e.currentTarget.name + " is disabled while " + squares[e.currentTarget.id].name + " tweens in."); 
 fadeIn = new Tween(squares[e.currentTarget.id],"alpha",None.easeNone,0,1,2.5,true);  
 fadeIn.addEventListener(TweenEvent.MOTION_FINISH,enableButton(e.currentTarget));  
}  

function enableButton(thisButton:Object):Function 
{ 
 return function(e:TweenEvent):void 
 { 
    thisButton.mouseEnabled = true; 
    trace(e.target.obj.name + " has finished fading in, "+ thisButton.name + " is now enabled again."); 
 }; 
}  

//为fruitbeard提供代码。

1 个答案:

答案 0 :(得分:1)

我认为在您的代码中,您忘记淡出当前的广场然后淡入新广场。

看看这段代码:

// to indicate the index of current active circle/square
var current:int = 0;
var fadeIn:Tween, fadeOut:Tween; 

var thisCircle:MovieClip; 
var thisSquare:MovieClip; 

var circles:Array = new Array(circle1, circle2, circle3, circle4); 
var squares:Array = new Array(square1, square2, square3, square4); 

for(var i:Number = 0; i < circles.length; i++) 
{ 
    thisCircle = circles[i]; 
    thisCircle.buttonMode = true; 
    thisCircle.id = i; 
    thisCircle.addEventListener(MouseEvent.CLICK, doFadeIn); 

    // keep the first square as visible
    if(i != current){
        thisSquare = squares[i]; 
        thisSquare.alpha = 0; 
    }
} 

function doFadeIn(e:MouseEvent):void  
{ 
    // if our button is the active one, exit
    if(current == e.currentTarget.id) return;

    // fade out current square
    fadeOut = new Tween(squares[current], "alpha", None.easeNone, 1, 0, 2.5, true);     

    // fade in the new active square
    fadeIn = new Tween(squares[e.currentTarget.id], "alpha", None.easeNone, 0, 1, 2.5, true);  

    current = e.currentTarget.id;
}  

您可以看到此代码正常工作here

我希望这可以帮到你。