Flash加载图像不使用XML

时间:2012-02-18 16:14:14

标签: xml flash actionscript-3 actionscript

好的,所以我得到了这个演示Flash文件,它通过XML加载图像并添加不同的效果。目前它被用作页面上的横幅。现在我被要求保留效果,但要嵌入Flash中的图片(以便摆脱XML)。关于如何将其更改为不从XML读取,我一无所知。任何帮助都将非常感激,因为我在MX时代停止使用Flash ...:S

import caurina.transitions.Tweener;
import caurina.transitions.properties.ColorShortcuts;
import flash.events.MouseEvent;

ColorShortcuts.init();

var hover_effect:Boolean;
var auto_play:Boolean;
var auto_play_duration:Number = 3000;
var no_of_rows:uint = 4;
var no_of_columns:uint = 8;

var tween_duration:Number = 0.7;
var tween_delay:Number = 0.03;
var block_scale:Number = 0.1;
var flashmo_margin:Number = 10;
var offstage_position:Number = 300;

var i:Number;
var j:Number;
var new_x:Number;
var new_y:Number;
var pic:Number = 0;
var previous_no:Number = 0;
var current_no:Number = 0;
var total:Number;

var timer:Timer;
var flashmo_xml:XML;
var flashmo_photo_list = new Array();
var mc:MovieClip;
var photo_group:MovieClip = new MovieClip();
var circle_group:MovieClip = new MovieClip();
var photo_array:Array;
var photo_group_array:Array;
var size_width:uint;
var size_height:uint;
var photo_width:uint;
var photo_height:uint;

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

photo_group.mask = flashmo_photo_area;
photo_group_array = [];
photo_button.alpha = 0;
flashmo_cover.visible = false;
flashmo_bar.flashmo_next.visible = false;
flashmo_bar.flashmo_previous.visible = false;
flashmo_bar.flashmo_play.visible = false; 
flashmo_bar.flashmo_pause.visible = false;

flashmo_bar.addChild(circle_group);
this.addChildAt(photo_group, 1);
this.addChild(photo_button);
this.addChild(flashmo_bar);

function load_gallery(xml_file:String):void
{

create_photo_rotator();
}

//remove argument since it is no longer being passed the Event
function create_photo_rotator():void
{   
    //manually set a bunch of properties that the XML normally would have
    hover_effect=true;
    auto_play=true; 
    auto_play_duration=2.4;
    //grid_row=3;
    //grid_column=9; 
    tween_duration=0.7; 
    tween_delay=0.03;

    //manually build array of photo objects
    //notice that "Photo1" is the filename, same as the photo Class name we set above
    //add as many of this code block as there are embedded photos
    flashmo_photo_list.push({
        filename: "slide1",
        flow: "in",
        direction: "right",
        rotation: ""
    });

    flashmo_photo_list.push({
        filename: "slide2",
        flow: "in",
        direction: "left",
        rotation: ""
    });

    flashmo_photo_list.push({
        filename: "slide3",
        flow: "in",
        direction: "down",
        rotation: "-180"
    });

    flashmo_photo_list.push({
        filename: "slide4",
        flow: "in",
        direction: "up",
        rotation: ""
    });

    total = flashmo_photo_list.length;

    load_photo();

}

function load_photo():void
{
    //manually call on_photo_loaded and pass it current photo index
    on_photo_loaded(pic);
    pic++;

    load_circle();

}

function load_circle():void
{
    var fm_circle:MovieClip = new flashmo_circle();
    fm_circle.x = 5 + ( fm_circle.width + 4 ) * ( pic - 1 ) + fm_circle.width * 0.5;
    fm_circle.y = ( flashmo_bar.bar.height - fm_circle.height ) * 0.5;  
    fm_circle.name = "flashmo_circle_" + circle_group.numChildren;
    fm_circle.visible = false;
    circle_group.addChild( fm_circle );

    if( circle_group.numChildren == 1 )
    {       
        Tweener.addTween( fm_circle , { _color_redOffset: 255, _color_greenOffset: 255, 
                     _color_blueOffset: 255, time: tween_duration, transition: "easeIn" } );
    }
}

function on_photo_progress(e:ProgressEvent):void
{
    var percent:Number = Math.round(e.bytesLoaded / e.bytesTotal * 100);

}

//change argument to expect photo index instead of Event
function on_photo_loaded(photoIndex:int):void
{   
    mc = MovieClip( circle_group.getChildAt( pic - 1 ) );
    mc.visible = true;

    //create Bitmap from embedded image's BitmapData
    var photoBitmap:Bitmap = new Bitmap(flashmo_photo_list[photoIndex].filename);

    if( pic == 1 )
    {
        //adjust values on newly created Bitmap
        photo_width = photoBitmap.width;
        photo_height = photoBitmap.height;

        adjust_size();
    }
    else if( pic > 1 )
    {
        enable_circle( mc );

        if( pic == 2 )
        {
            flashmo_bar.flashmo_previous.addEventListener( MouseEvent.CLICK, pic_previous );
            flashmo_bar.flashmo_next.addEventListener( MouseEvent.CLICK, pic_next );            
            flashmo_bar.flashmo_play.addEventListener( MouseEvent.CLICK, pic_play );
            flashmo_bar.flashmo_pause.addEventListener( MouseEvent.CLICK, pic_pause );
            flashmo_bar.flashmo_previous.visible = flashmo_bar.flashmo_next.visible = true;

            timer = new Timer(auto_play_duration);
            timer.addEventListener(TimerEvent.TIMER, auto_play_timer);

            if( auto_play )
            {
                flashmo_bar.flashmo_play.visible = false;
                flashmo_bar.flashmo_pause.visible = true;

                timer.start();
            }
            else
            {
                flashmo_bar.flashmo_play.visible = true;
                flashmo_bar.flashmo_pause.visible = false;
            }           
        }
    }

    if( pic < total )
    {
        load_photo();
    }

    //pass the Bitmap to create_effect (which is already expecting a Bitmap)
    create_effect( photoBitmap );

}

function adjust_size():void
{
    flashmo_photo_area.width = photo_button.width = flashmo_bar.bar.width = photo_width;
    flashmo_photo_area.height = photo_button.height = photo_height;

    flashmo_bar.flashmo_next.x = 
            flashmo_bar.bar.width - flashmo_bar.flashmo_next.width - flashmo_margin * 0.5;

    flashmo_bar.flashmo_previous.x = flashmo_bar.flashmo_next.x - flashmo_bar.flashmo_next.width;

    flashmo_bar.flashmo_play.x = flashmo_bar.flashmo_pause.x = 
            flashmo_bar.flashmo_previous.x - flashmo_bar.flashmo_previous.width;

    flashmo_bar.y = photo_height;
    photo_group.x = flashmo_photo_area.x;
    photo_group.y = flashmo_photo_area.y;
    photo_group.mask = flashmo_photo_area;
}

function create_effect( bitmap:Bitmap ):void
{   
    var flashmo_pic_mc:MovieClip = new MovieClip();

    size_width  = Math.ceil( photo_width / no_of_columns );
    size_height = Math.ceil( photo_height / no_of_rows );
    photo_array = [];

    if( photo_group.x == flashmo_photo_area.x )
    {
        photo_group.x += size_width * 0.5;
        photo_group.y += size_height * 0.5;
    }

    for( j = 0; j < no_of_rows; j++ )
    {
        for( i = 0; i < no_of_columns; i++ )
        {                                                       
            var temp_bitmap_data:BitmapData = new BitmapData ( size_width, size_height, true, 0xFFFFFF );
            var source_rect:Rectangle = new Rectangle ( i * size_width, j * size_height, 
                                                       size_width, size_height );

            temp_bitmap_data.copyPixels ( bitmap.bitmapData, source_rect, new Point );

            var temp_bitmap:Bitmap = new Bitmap ( temp_bitmap_data );
            var temp_sprite:Sprite = new Sprite;

            temp_bitmap.smoothing = true;
            temp_bitmap.x = - temp_bitmap.width * 0.5;
            temp_bitmap.y = - temp_bitmap.height * 0.5;

            temp_sprite.addChild ( temp_bitmap );
            temp_sprite.x = i * size_width;

            if( photo_group.numChildren > 0 )
                temp_sprite.y = photo_height;
            else
                temp_sprite.y = j * size_height;

            photo_array.push ( temp_sprite );
            flashmo_pic_mc.addChild( temp_sprite );
        }
    }

    flashmo_pic_mc.name = "flashmo_pic_" + photo_group.numChildren;
    photo_group_array.push( photo_array );
    photo_group.addChild ( flashmo_pic_mc );
}

function auto_play_timer(te:TimerEvent):void
{
    current_no++;
    change_photo();
}

function change_photo():void
{
    if( current_no >= photo_group.numChildren ) 
        current_no = 0;
    if( current_no < 0 )
        current_no = photo_group.numChildren - 1;

    var flashmo_direction:String = flashmo_photo_list[current_no].direction;
    var flashmo_rotation_str:String = flashmo_photo_list[current_no].rotation;
    var flashmo_rotation:Number = 0;

    if( flashmo_direction == "" )
        flashmo_direction = "center";

    if( flashmo_rotation_str != "" )
        flashmo_rotation = parseInt( flashmo_rotation_str );

    if( flashmo_photo_list[current_no].flow == "in" )
        transition_in( current_no, flashmo_direction, flashmo_rotation );
    else
        transition_out( previous_no, flashmo_direction, flashmo_rotation );

    disable_buttons();
}

function transition_in( pic_index:uint, from_direction:String, flashmo_rotation:Number ):void
{
    photo_group.addChild( photo_group.getChildByName( "flashmo_pic_" + pic_index ) );
    photo_array = photo_group_array[ pic_index ];

    var row_no:uint;
    var column_no:uint;

    for (i = 0; i < photo_array.length ; i++)
    {       
        if( flashmo_rotation != 0 )
            photo_array[i].rotation = flashmo_rotation;

        photo_array[i].scaleX = photo_array[i].scaleY = block_scale;
        photo_array[i].alpha = 0;

        if( from_direction == "left" )
        {
            photo_array[i].x = - size_width;            
            photo_array[i].y = Math.floor( i / no_of_columns ) * size_height;
        }
        else if( from_direction == "right" )
        {
            photo_array[i].x = photo_width;
            photo_array[i].y = Math.floor( i / no_of_columns ) * size_height;
        }
        else if( from_direction == "up" )
        {
            photo_array[i].x =  ( i % no_of_columns ) * size_width ;
            photo_array[i].y =  - size_height;
        }
        else if( from_direction == "down" )
        {
            photo_array[i].x = ( i % no_of_columns ) * size_width;
            photo_array[i].y = photo_height;
            photo_array[i].scaleX = photo_array[i].scaleY = 0;
        }
        else
        {
            photo_array[i].x = ( i % no_of_columns ) * size_width;
            photo_array[i].y = Math.floor( i / no_of_columns ) * size_height;           
        }
    }

    for ( i = 0; i < photo_array.length ; i++ )
    {       
        if( from_direction == "left" )
        {
            j = photo_array.length - i - 1;
            j = ( j % no_of_rows ) * no_of_columns + Math.floor( j / no_of_rows );

            new_x = ( j % no_of_columns ) * size_width;
            new_y = photo_array[j].y;
        }
        else if( from_direction == "right" )
        {
            j = ( i % no_of_rows ) * no_of_columns + Math.floor( i / no_of_rows );
            new_x = ( j % no_of_columns ) * size_width;
            new_y = photo_array[j].y;
        }
        else if( from_direction == "up" )
        {
            j = photo_array.length - i - 1;
            new_x = photo_array[j].x;
            new_y = Math.floor( j / no_of_columns ) * size_height;
        }
        else if( from_direction == "down" )
        {
            j = i;
            new_x = photo_array[j].x;
            new_y = Math.floor( j / no_of_columns ) * size_height;
        }
        else
        {
            j = i;
            new_x = photo_array[j].x;
            new_y = photo_array[j].y;
        }

        Tweener.addTween ( photo_array[j], { x: new_x, y: new_y,
            alpha: 1, scaleX: 1, scaleY: 1, rotation: 0,
            delay: tween_delay * i, time: tween_duration, transition: "easeOutQuart",
            onComplete: transition_complete, onCompleteParams:[i, true] }   );
    }
}

function transition_complete( no:Number, transition_in:Boolean ):void
{
    if( no == photo_array.length - 1 )
    {       
        if( transition_in )
        {
            photo_array = photo_group_array[ previous_no ];

            var sprite_index:Number = 0;

            for ( j = 0; j < no_of_rows; j++ )
            {
                for ( i = 0; i < no_of_columns; i++ )
                {   
                    photo_array[ sprite_index ].x = -1000;
                    photo_array[ sprite_index ].y = -1000;          
                    sprite_index++;         
                }
            }
        }

        enable_buttons();
    }
}

function transition_out( pic_index:uint, to_direction:String, flashmo_rotation:Number ):void
{
    photo_group.addChild( photo_group.getChildByName( "flashmo_pic_" + current_no ) );
    photo_group.addChild( photo_group.getChildByName( "flashmo_pic_" + pic_index ) );
    photo_array = photo_group_array[ pic_index ];

    var flashmo_effect:String = "easeInQuart";

    for ( i = 0; i < photo_array.length ; i++)
    {       
        if( to_direction == "left" )
        {
            j = ( i % no_of_rows ) * no_of_columns + Math.floor( i / no_of_rows );
            new_x = - offstage_position;
            new_y = photo_array[j].y;
        }
        else if( to_direction == "right" )
        {
            j = photo_array.length - i - 1;
            j = ( j % no_of_rows ) * no_of_columns + Math.floor( j / no_of_rows );
            new_x = photo_width + offstage_position;
            new_y = photo_array[j].y;
        }       
        else if( to_direction == "up" )
        {
            j = i;
            new_x = photo_array[j].x;
            new_y = - offstage_position;
        }
        else if( to_direction == "down" )
        {
            j = photo_array.length - i - 1;
            new_x = photo_array[j].x;
            new_y = photo_height + offstage_position;           
        }
        else
        {
            j = i;
            new_x = photo_array[j].x;
            new_y = photo_array[j].y;
            flashmo_effect = "easeInOutQuart";
        }

        Tweener.addTween ( photo_array[j], { x: new_x, y: new_y,
            scaleX: block_scale, scaleY: block_scale, alpha: 0, rotation: flashmo_rotation, 
            delay: tween_delay * i, time: tween_duration, transition: flashmo_effect,
            onComplete: transition_complete, onCompleteParams:[i, false] } );
    }

    to_normal_position( current_no );
}

function to_normal_position( pic_index:uint ):void
{
    photo_array = photo_group_array[ pic_index ];

    var sprite_index:Number = 0;

    for ( j = 0; j < no_of_rows; j++ )
    {
        for ( i = 0; i < no_of_columns; i++ )
        {   
            photo_array[ sprite_index ].rotation = 0;
            photo_array[ sprite_index ].alpha = 
            photo_array[ sprite_index ].scaleX = 
            photo_array[ sprite_index ].scaleY = 1;         
            photo_array[ sprite_index ].x = i * size_width;
            photo_array[ sprite_index ].y = j * size_height;            
            sprite_index++;         
        }
    }
}


function pic_over(e:MouseEvent):void
{
    Tweener.addTween( photo_button, { alpha: 0.15, time: tween_duration, transition: "easeIn" } );
}

function pic_out(e:MouseEvent):void
{
    Tweener.addTween( photo_button, { alpha: 0, time: tween_duration, transition: "easeOut" } );
}

function pic_previous(e:MouseEvent):void
{
    current_no--;
    change_photo();
}

function pic_next( e:MouseEvent ):void
{
    current_no++;
    change_photo();
}

function pic_play(e:MouseEvent):void
{
    flashmo_bar.flashmo_pause.visible = true;
    flashmo_bar.flashmo_play.visible = false;
    auto_play = true;

    if( circle_group.alpha == 1 )
        timer.start();
}

function pic_pause(e:MouseEvent):void
{
    flashmo_bar.flashmo_pause.visible = false;
    flashmo_bar.flashmo_play.visible = true;
    auto_play = false;
    timer.reset();
}

function circle_out( me:MouseEvent ):void
{
    mc = MovieClip( me.target );
    Tweener.addTween( mc, { _color_redOffset: 0, _color_greenOffset: 0, 
                     _color_blueOffset: 0, time: tween_duration, transition: "easeIn" } );
}

function circle_over( me:MouseEvent ):void
{
    mc = MovieClip( me.target );
    Tweener.addTween( mc, { _color_redOffset: 255, _color_greenOffset: 255, 
                     _color_blueOffset: 255, time: tween_duration, transition: "easeIn" } );
}

function circle_click( me:MouseEvent ):void
{
    mc = MovieClip( me.target );
    current_no = parseInt( mc.name.slice( 15,17 ) );
    change_photo();
}

function enable_buttons():void
{
    if( auto_play )
        timer.start();
    else
        timer.reset();

    photo_button.visible = true;
    flashmo_bar.flashmo_previous.mouseEnabled = true;
    flashmo_bar.flashmo_next.mouseEnabled = true;

    flashmo_bar.flashmo_previous.addEventListener( MouseEvent.CLICK, pic_previous );
    flashmo_bar.flashmo_next.addEventListener( MouseEvent.CLICK, pic_next );
    circle_group.alpha = 1;

    for( i = 0; i < circle_group.numChildren; i++ )
    {
        if( i == current_no )
            continue;

        mc = MovieClip( circle_group.getChildAt( i ) );
        enable_circle( mc );
    }

    previous_no = current_no;
}

function disable_buttons():void
{
    if( auto_play )
        timer.reset();

    photo_button.visible = false;
    flashmo_bar.flashmo_previous.mouseEnabled = false;
    flashmo_bar.flashmo_next.mouseEnabled = false;

    flashmo_bar.flashmo_previous.removeEventListener( MouseEvent.CLICK, pic_previous );
    flashmo_bar.flashmo_next.removeEventListener( MouseEvent.CLICK, pic_next );
    circle_group.alpha = 0.5;

    for( i = 0; i < circle_group.numChildren; i++ )
    {
        mc = MovieClip( circle_group.getChildAt( i ) );
        disable_circle( mc );
        Tweener.addTween( mc, { _color_redOffset: 0, _color_greenOffset: 0, 
                     _color_blueOffset: 0, time: tween_duration, transition: "easeIn" } );
    }

    mc = MovieClip( circle_group.getChildAt( current_no ) );
    Tweener.addTween( mc , { _color_greenOffset: 255, _color_redOffset: 255,
                     _color_blueOffset: 255, time: tween_duration, transition: "easeIn" } );
}

function enable_circle( mc:MovieClip ):void
{
    mc.addEventListener( MouseEvent.CLICK, circle_click );
    mc.addEventListener( MouseEvent.MOUSE_OUT, circle_out );
    mc.addEventListener( MouseEvent.MOUSE_OVER, circle_over );
    mc.buttonMode = true;
}

function disable_circle( mc:MovieClip ):void
{
    mc.removeEventListener( MouseEvent.CLICK, circle_click );   
    mc.removeEventListener( MouseEvent.MOUSE_OUT, circle_out );
    mc.removeEventListener( MouseEvent.MOUSE_OVER, circle_over );
    mc.buttonMode = false;
}


Object(root).grid_slider.logo.cotton.addEventListener(MouseEvent.CLICK,fl_ClickToGoToWebPage);
function fl_ClickToGoToWebPage(event:MouseEvent):void
{
    navigateToURL(new URLRequest("http://www.google.com"), "_self");
}

使用新错误更新了代码:

TypeError: Error #1010: A term is undefined and has no properties.
at original_fla::GridSliderClick_1/create_photo_rotator()
at original_fla::GridSliderClick_1/load_gallery()
at original_fla::MainTimeline/frame1()

1 个答案:

答案 0 :(得分:2)

要将此转换为不使用XML和外部图像,您需要做几件事。我将尽我所能引导您完成它,但我没有基于您提供的代码的大量信息(例如,我没有看到此代码实际初始化的方式,因为它看起来像{{1正在被未张贴的东西调用。

您要做的第一件事就是嵌入所有外部图像。为此,您需要使用File&gt;将图像导入Flash库。导入&gt;导入图书馆......

将所有图像导入库后,您的库现在应该为每个图像都有一个位图和一个符号。忽略或删除符号 - 您不需要它们。右键单击每个位图,然后打开属性。在这里,在顶部对话框中命名位图(例如loadGallery()),然后在“链接”区域中单击“导出for ActionScript”按钮。 “类”对话框应自动填充位图名称(例如Photo1),“基类”对话框应为Photo1

图像现在将嵌入到swf中 - 您所做的就是将每个图像转换为ActionScript可访问的flash.display.BitmapData类。现在您需要更改代码以使用这些嵌入的图像而不是外部图像。

转换此代码的一个挑战是代码严重依赖于XML。

在我看来,代码通过调用方法BitmapData来初始化。但是,我不知道那叫什么。所以保持这种方法,但只是将其剥离:

load_gallery()

这将绕过加载XML。接下来,您需要大量修改function load_gallery(xml_file:String):void { create_photo_rotator(); } 方法:

create_photo_rotator()

<强>更新:
基本上,您必须手动构建照片数据数组,因为您不再拥有XML。只需复制将对象数据推送到//remove argument since it is no longer being passed the Event function create_photo_rotator():void { //manually set a bunch of properties that the XML normally would have hover_effect = <set true or false>; auto_play = <set true or false>; auto_play_duration = <set number>; no_of_rows = <set number>; no_of_columns = <set number>; tween_duration = <set number>; tween_delay = <set number>; //manually build array of photo objects //notice that "Photo1" is the filename, same as the photo Class name we set above //add as many of this code block as there are embedded photos //**UPDATE**:store an instance of the BitmapData class instead of the name of the class as a string - notice the key name has changed to "bitmapData" instead of "filename" flashmo_photo_list.push({ bitmapData: new Photo1(), flow: "<flow string>", direction: "<direction string>", rotation: "<rotation string>" }); total = flashmo_photo_list.length; load_photo(); } 的代码块,即可获得尽可能多的照片。确保将每个flashmo_photo_list设置为先前嵌入的关联位图的Class的新实例,并手动设置其他属性(只需浏览XML并将其设置的任何内容复制到那里)。因此,如果在库中有一个名为“Photo1”的位图,并且在Photo1的属性中单击了“为ActionScript导出”并将“类”设置为“Photo1”,则代码将为:bitmapData。< / p>

现在,更改bitmapData: new Photo1()方法以删除实际加载照片:

load_photo()

最后,更改function load_photo():void { //manually call on_photo_loaded and pass it current photo index on_photo_loaded(pic); pic++; load_circle(); } 以使用嵌入式位图:

on_photo_loaded()

您制作//change argument to expect photo index instead of Event function on_photo_loaded(photoIndex:int):void { mc = MovieClip( circle_group.getChildAt( pic - 1 ) ); mc.visible = true; //create Bitmap from embedded image's BitmapData //**UPDATE**: reference the stored bitmapData instead of filename string var photoBitmap:Bitmap = new Bitmap(flashmo_photo_list[photoIndex].bitmapData); if( pic == 1 ) { //adjust values on newly created Bitmap photo_width = photoBitmap.width; photo_height = photoBitmap.height; adjust_size(); } else if( pic > 1 ) { enable_circle( mc ); if( pic == 2 ) { flashmo_bar.flashmo_previous.addEventListener( MouseEvent.CLICK, pic_previous ); flashmo_bar.flashmo_next.addEventListener( MouseEvent.CLICK, pic_next ); flashmo_bar.flashmo_play.addEventListener( MouseEvent.CLICK, pic_play ); flashmo_bar.flashmo_pause.addEventListener( MouseEvent.CLICK, pic_pause ); flashmo_bar.flashmo_previous.visible = flashmo_bar.flashmo_next.visible = true; timer = new Timer(auto_play_duration); timer.addEventListener(TimerEvent.TIMER, auto_play_timer); if( auto_play ) { flashmo_bar.flashmo_play.visible = false; flashmo_bar.flashmo_pause.visible = true; timer.start(); } else { flashmo_bar.flashmo_play.visible = true; flashmo_bar.flashmo_pause.visible = false; } } } if( pic < total ) { load_photo(); } //pass the Bitmap to create_effect (which is already expecting a Bitmap) create_effect( photoBitmap ); } 的原因是因为之前嵌入的照片会导出为new Bitmap个类,因此您使用BitmapData创建照片{{1 }}。 更新:确保您引用对象上的“bitmapData”值,而不是之前使用的“filename”值。

根据您提供的代码,这些更改应该可以满足您的要求。但是,我不能自己测试,所以你可能仍然需要调整一些东西。