Actionscript 3.0:控制添加了数组的子动画片段

时间:2013-04-21 23:42:58

标签: arrays actionscript-3 slideshow

我正在尝试构建一个通过按键控制的通用幻灯片闪存文件...左右箭头转到下一个/上一个幻灯片,向下和向上调用/取消单个动画/项目符号/无论如何一张幻灯片。 到目前为止,我设法得到一个文件,可以加载动画片段中的所有页面,并使用左右箭头轻拂它们,并且即使在非常快速地轻弹时也会有很好的淡出和淡入效果。 经过几个小时和大量谷歌搜索之后我根本没有得到的是如何控制单页动画片段。我到目前为止上传了该文件:

http://www.broesel-brzelius.de/zeug/slide.zip

你可以看到我(尚未粗略)尝试控制pageHolder.Pages.page1 movieclip到gotoAndStop(2)只会导致加载到数组中位置0的pageHolder.Pages movieclip进入框架2,从而显示第二页。

到目前为止

代码:

import flash.display.Sprite; 
import flash.events.MouseEvent; 
import com.greensock.*; 
import flash.display.MovieClip; 

// instantiate a variable to find number of pages 
var numberOfPages:Pages = new Pages(); 
// instantiate an array to hold the page movieclips 
var pageArray:Array = new Array(); 
// instantiate a container to hold the pages 
var pageHolder:Sprite = new Sprite(); 
// declare variables that will hold reference to the current page IDs
var targetIDold:int = 0;
var targetIDnew:int = 0;
// declare a variable that will hold the current direction of slide movement
var movement:int = -1;

// call a function that builds the application 
// pass in the number of pages in fl_prevSlide 
buildApp(numberOfPages.totalFrames);

// this function builds the application
function buildApp(n:int):void
{
    // declare variables for the pages
    var p:Pages;
    // instantiate a new Page, send its playhead to the current value of i+1
    // it is necessary to add 1 to the value of i, since i starts at 0, while the timeline starts at 1
    for (var i:int = 0; i < n; i++) 
    { 
        p = new Pages(); 
        p.gotoAndStop(i + 1); 
        pageArray.push(p);
    } 
    // set the position of the pageHolder relative to the buttonHolder 
    pageHolder.x = pageHolder.y = 0; 
    // add the first page (at index 0) from pageArray to pageHolder 
    pageHolder.addChild(pageArray[targetIDnew]); 
    // add pageHolder and buttonHolder to the stage 
    addChild(pageHolder); 
} 

//Adding Listener and corresponding function to change between slides
stage.addEventListener(KeyboardEvent.KEY_DOWN, f_changeSlide);
function f_changeSlide(evt:KeyboardEvent):void
{
    if(evt.keyCode == 37) // left arrow
    {
        f_prevSlide();
    }
    else if (evt.keyCode == 39 || evt.keyCode == 32) // right arrow or space
    {
        f_nextSlide();
    }
}
function f_prevSlide():void
{
    if(targetIDnew > 0)
    {
        movement = -1;
        targetIDnew -= 1;
        f_addPage();
    }
}
function f_nextSlide():void
{
    if(targetIDnew < (numberOfPages.totalFrames - 1))
    {
        movement = 1;
        targetIDnew += 1;
        f_addPage();
    }
}

function f_addPage():void 
{ 
    // use the targetID variable to access the corresponding index in the pageArray and assign it to a temporary variable
    targetIDold = targetIDnew - movement;
    var _mcOld:MovieClip = MovieClip(pageArray[targetIDold]);
    var _mcNew:MovieClip = MovieClip(pageArray[targetIDnew]);
    //avoid flickering
    if (_mcNew.alpha == 1) {
        _mcNew.alpha = 0;
    }
    // add the temp variable to pageHolder
    pageHolder.addChild(_mcNew);
    // tween the temp variable to the specified properties, then call a function to remove the previous page
    // new page gets faded in, old page gets faded out
    TweenMax.to(_mcNew, 1.5, {alpha:1}); 
    TweenMax.to(_mcOld, 1.5, {alpha:0, onComplete:f_removePage}); 
} 

function f_removePage():void 
{ 
    // previous page will always be at index 0; remove it 
    // the new page just added will drop down to index 0 when the previous page is removed 
    pageHolder.removeChildAt(0); 
}

//Trying to advance / decrease the timeline in the movieclips of the single pages by pressing down / up
stage.addEventListener(KeyboardEvent.KEY_DOWN, f_changeFrame);
function f_changeFrame(evt:KeyboardEvent):void
{
    if(evt.keyCode == 40) // down arrow
    {
        f_nextFrame();
    }
    else if (evt.keyCode == 38) // up arrow
    {
        f_prevFrame();
    }
}
function f_nextFrame():void
{
        var w:uint = pageHolder.numChildren - 1 ;
        (pageHolder.getChildAt(w) as MovieClip).gotoAndStop(currentFrame + 1);
//      for (var i:uint = 0; i < pageHolder.numChildren; i++)
//      {
//       trace (+i+'.\t name:' + pageHolder.getChildAt(i).name + '\t type:' + typeof (pageHolder.getChildAt(i)));
//      }
        trace ("down!");
}

function f_prevFrame():void
{
        var w:uint = pageHolder.numChildren - 1 ;
        (pageHolder.getChildAt(w) as MovieClip).gotoAndStop(currentFrame - 1);
//      for (var i:uint = 0; i < pageHolder.numChildren; i++)
//      {
//       trace (+i+'.\t name:' + pageHolder.getChildAt(i).name + '\t type:' + typeof (pageHolder.getChildAt(i)));
//      }
        trace ("up!");
}

编辑:澄清代码

Edit2:TL; DR:问题是三个函数的最后一位代码,这三个函数无法控制mc page1的时间轴,这是mc页面的子节点,它被逐帧推入一个数组并加载到运行时进入sprite pageHolder。如何控制单页动画片段的时间轴?

1 个答案:

答案 0 :(得分:0)

终于开始工作了。这将作为一个完美的powerpoint替代品:D

您需要将单张幻灯片(mc pages的每一帧上的一个动画片段)命名为page0page1等,然后可以使用此代码控制其时间轴(只需替换上面问题中列出的函数:

function f_nextFrame():void
{
    var temp:uint = pageHolder.numChildren - 1;
    var frame:uint = ((pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].currentFrame);
    (pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].gotoAndStop(frame + 1);
}

function f_prevFrame():void
{
    var temp:uint = pageHolder.numChildren - 1;
    var frame:uint = ((pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].currentFrame);
    (pageHolder.getChildAt(temp) as MovieClip)["page" + targetIDnew].gotoAndStop(frame - 1);
}