幻灯片样式导航。 addChild&阵列

时间:2012-01-31 04:10:57

标签: flash actionscript-3

希望在这里得到一些建议。似乎是iPad演示的基本导航系统有问题。

对于这个例子,我在我的库中有5个页面,我已将其添加到数组中。 我瞄准的效果,当用户滑动到下一页时,它应该: - 使用addChild(视图外)将下一页添加到舞台 - 补间当前页面退出视图,补间下一页进入视图 - 使用removeChild删除旧页面。

这样做的原因是我会有大量的动画重页,我试图避免让它们全部出现在舞台上,以防止FPS下降。

到目前为止,这是我的代码。我尝试了一些事情,比如试图用数组重新设置currentPage但没有运气。我已经把头发拉了几个小时了!

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import flash.events.MouseEvent;
import flash.display.MovieClip;

//touch device swiping
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler_2);
function fl_SwipeHandler_2(event:TransformGestureEvent):void{
    switch(event.offsetX)
    {
        //swiped right, go back
        case 1:
        {
            prevPage();
            break;
        }
        //swiped left, go forward
        case -1:
        {
            nextPage();
            break;
        }
    }
}

//fill array with movieclip pages from library
var pageArray:Array = [page1, page2, page3, page4, page5];
var currentArray:Number = 0;

//add the first page in the array to the stage as 'currentPage'
var currentPage:MovieClip = new pageArray[currentArray];
addChild(currentPage);

function nextPage(event:TouchEvent):void{
    if (currentArray < pageArray.length - 1){

            //tween currentPage out to the left, when animation ends removeChild(currentPage);
            TweenMax.to(currentPage, 1, {x:-1024,onComplete:removeChild,onCompleteParams:[currentPage]});
            //move to the next item in the array
            currentArray++
            //add the next item in the array to the stage as 'newPage'
            var newPage:MovieClip = new pageArray[currentArray]
            addChild(newPage);
            newPage.x = 1024;
            //tween newPage into view
            TweenMax.to(newPage, 1, {x:0,onComplete:updatePage});

            /*
            PROBLEM STARTS HERE:
            - if nextPage is executed again, it will try to tween 'currentPage' which has been removed from the stage.
            - need to rename/change 'newPage' into 'currentPage' here but don't know how.
            */

    }
}

function prevClicked(event:MouseEvent):void{
    if (currentArray > 0){
        trace("same as nextPage in reverse");
    }
}

EDIT问题已解决

因此,在aesphere和一些反复试验的帮助下,它现在似乎正在起作用。其中一个主要问题是数组周围缺少 new (),例如:new page1(),new page2()。等

Multitouch.inputMode = MultitouchInputMode.GESTURE;

stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler_2);

function fl_SwipeHandler_2(event:TransformGestureEvent):void
{
    switch (event.offsetX)
    {
        case 1 :
            {
                prevPage();
                break;

            };
        case -1 :
            {
                nextPage();
                break;

        }
    }
};


import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import flash.events.MouseEvent;
import flash.display.MovieClip;


var pageArray:Array = [new page1(), new page2(), new page3(), new page4(), new page5()];
var currentPage:Number = 0;

addChild(pageArray[currentPage]);

function nextPage():void
{
    if (currentPage < pageArray.length - 1)
    {
        TweenMax.to(pageArray[currentPage], 1, {x:-1024, onComplete:removeChild, onCompleteParams:[pageArray[currentPage]]});
        currentPage++;
        pageArray[currentPage].x = 1024;
        addChild(pageArray[currentPage]);
        TweenMax.to(pageArray[currentPage], 1, {x:0});
    }

}

function prevPage():void
{
    if (currentPage > 0)
    {
        TweenMax.to(pageArray[currentPage], 1, {x:1024, onComplete:removeChild, onCompleteParams:[pageArray[currentPage]]});
        currentPage--;
        pageArray[currentPage].x = -1024;
        addChild(pageArray[currentPage]);
        TweenMax.to(pageArray[currentPage], 1, {x:0});
    }
}

1 个答案:

答案 0 :(得分:0)

页面之间的简单导航应该只使用变量来跟踪哪个页面是当前页面。你已经描述了你想要做什么,但是你做的事情比你的代码要复杂得多。

由于你有一个跟踪当前页面的计数器变量,你应该根据需要使用你已经拥有的页面数组addChild和removeChild,这样你就有一个控制点,其中currentPage总是引用当前的movieclip pageArray中的阶段。

所以从您的代码:

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import flash.events.MouseEvent;
import flash.display.MovieClip;

//I like to setup the required assets and variables first, then put everything else below:
//fill array with movieclip pages from library
var pageArray:Array = [page1, page2, page3, page4, page5];
var currentPage:Number = 0;

//Next we add the first page to the stage
addChild(pageArray[currentPage]);

//Now we add the listeners for the functionality
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler_2);
function fl_SwipeHandler_2(event:TransformGestureEvent):void{
    switch(event.offsetX)
    {
        //swiped right, go back
        case 1:
            prevPage();
            break;
        //swiped left, go forward
        case -1:
            nextPage();
            break;
    }
}

//The handlers for the swiping gestures for switch pages
function nextPage(event:TouchEvent):void
{
    //First check if we have a next page or not.
    if (currentPage < pageArray.length - 1){
        //So, we want to move to the next page, we first Tween the current page off to left

        TweenMax.to(pageArray[currentPage], 1, {x:-1024, onComplete:removeChild, onCompleteParams:[pageArray[currentPage]]});
        //Once the Tween starts,increment the page counter, set the starting xpos, addChild
        //and then tween that into place.
        currentPage++;
        pageArray[currentPage].x = 1024;
        addChild(pageArray[currentPage]);
        //Tweening into place then calls the updatePage when tween is complete.
        TweenMax.to(pageArray[currentPage], 1, {x:0, onComplete:updatePage});
    }

}

function prevPage(event:TouchEvent):void
{
    trace('pretty much nextPage but decrementing currentPage plus a check against 0');
}

希望评论有意义。试试这个。