在As3.0库中单击鼠标处理

时间:2011-09-26 13:23:26

标签: flash actionscript-3 actionscript

我正在玩一个画廊。我想添加更多功能。

这是最初的代码:

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

function doSMTH(luka) {
var catNum:Number = luka;

stop();
goBack.addEventListener(MouseEvent.CLICK, goBacke);
    function goBacke(e:MouseEvent):void{
        gotoAndStop(1);
        stage.removeChild(thumbnail_group);
}


// Tweener
// http://code.google.com/p/tweener/
import caurina.transitions.*;

var filename_list = new Array();
var url_list = new Array();
var url_target_list:Array = new Array();
var title_list = new Array();
var description_list = new Array();

var i:Number;
var tn:Number = 0;
var tween_duration:Number = 0.4;
var move_x:Number = 200;
var move_y:Number = -300;

//var fm_tween:Tween;
var total:Number;
var flashmo_xml:XML = new XML();
var folder:String = "photos/";
var xml_loader:URLLoader = new URLLoader();
xml_loader.load(new URLRequest("gallery.xml"));
xml_loader.addEventListener(Event.COMPLETE, create_thumbnail);

var thumbnail_group:MovieClip = new MovieClip();
stage.addChild(thumbnail_group);

thumbnail_group.x = tn_group.x;
var default_y:Number = thumbnail_group.y = tn_group.y;

tn_group.visible = false;
url_button.visible = false;
tn_title.text = "";
tn_desc.text = "";
tn_url.text = "";
tn_url_target.text = "";

function create_thumbnail(e:Event):void 
{
    flashmo_xml = XML(e.target.data);
    total = flashmo_xml.category[catNum].thumbnail.length();

    for( i = 0; i < total; i++ )
    {
        filename_list.push( flashmo_xml.category[catNum].thumbnail[i].@filename.toString() );
        url_list.push( flashmo_xml.category[catNum].thumbnail[i].@url.toString() );
        url_target_list.push( flashmo_xml.category[catNum].thumbnail[i].@target.toString() );
        title_list.push( flashmo_xml.category[catNum].thumbnail[i].@title.toString() );
        description_list.push( flashmo_xml.category[catNum].thumbnail[i].@description.toString() );
    }
    load_tn();
}

function load_tn():void
{
    var pic_request:URLRequest = new URLRequest( folder + filename_list[tn] );
    var pic_loader:Loader = new Loader();

    pic_loader.load(pic_request);
    pic_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loaded);
    tn++;
}

function on_loaded(e:Event):void
{
    if( tn < total )
    {
        load_tn();
        tn_desc.text = "Loading " + tn + " of " + total + " photos";
    }
    else
    {
        tn_title.text = title_list[0];
        tn_desc.text = description_list[0];
        tn_url.text = url_list[0];
        tn_url_target.text = url_target_list[0];

        var mc:MovieClip = MovieClip( thumbnail_group.getChildAt(total-2) );
        mc.addEventListener( MouseEvent.CLICK, go_out );

        Tweener.addTween( mc, { rotation: 0, time: tween_duration, transition: "easeOut" } );
        url_button.visible = true;
        url_button.addEventListener(MouseEvent.CLICK, goto_URL);
    }

    var flashmo_bm:Bitmap = new Bitmap();
    var flashmo_mc:MovieClip = new MovieClip();

    flashmo_bm = Bitmap(e.target.content);
    flashmo_bm.x = - flashmo_bm.width * 0.5;
    flashmo_bm.y = - flashmo_bm.height * 0.5;
    flashmo_bm.smoothing = true;

    var bg_width = flashmo_bm.width + 16;
    var bg_height = flashmo_bm.height + 16;

    flashmo_mc.addChild(flashmo_bm);
    flashmo_mc.graphics.lineStyle(1, 0x999999);
    flashmo_mc.graphics.beginFill(0xFFFFFF);
    flashmo_mc.graphics.drawRect( - bg_width * 0.5, - bg_height * 0.5, bg_width, bg_height );
    flashmo_mc.graphics.endFill();
    flashmo_mc.x = move_x;
    flashmo_mc.y = move_y;
    flashmo_mc.rotation = 45;
    flashmo_mc.name = "flashmo_" + thumbnail_group.numChildren;

    Tweener.addTween( flashmo_mc, { x: Math.random() * 20 - 10, y: Math.random() * 20 - 10, 
                      rotation: Math.random() * 20 - 10, 
                      time: tween_duration * 2, transition: "easeOut" } );

    thumbnail_group.addChildAt(flashmo_mc, 0);
}


function go_out(e:MouseEvent):void
{

    var mc:MovieClip = MovieClip(e.target);
    //var imgFwd:MovieClip = MovieClip(e.target);
    mc.removeEventListener( MouseEvent.CLICK, go_out );
    //imgFwd.removeEventListener(MouseEvent.CLICK, go_out);
    Tweener.addTween( mc, { x: 220, y: -180, rotation: 45, time: tween_duration,
                      transition: "easeInQuart", onComplete: go_in } );
}

function go_in():void
{
    var mc:MovieClip = MovieClip( thumbnail_group.getChildAt(total-1) );
    var mc2:MovieClip = MovieClip( thumbnail_group.getChildAt(total-2) );

    var s_no:Number = parseInt( mc.name.slice(8,10) ) + 1;
    if(s_no == total) s_no = 0;

    Tweener.addTween( mc, { x: Math.random() * 20 - 10, y: Math.random() * 20 - 10, 
                            rotation: Math.random() * 20 - 10, time: tween_duration, 
                            transition: "easeOut", onComplete: add_click } );
    Tweener.addTween( mc2, { rotation: 0, time: tween_duration, transition: "easeOut" } );

    thumbnail_group.addChildAt( mc, 0 );
    tn_title.text = title_list[s_no];
    tn_desc.text = description_list[s_no];
    tn_url.text = url_list[s_no];
    tn_url_target.text = url_target_list[s_no];
}

function add_click():void
{
    var mc:MovieClip = MovieClip(thumbnail_group.getChildAt(total-1) );
    mc.addEventListener( MouseEvent.CLICK, go_out );
}

function goto_URL(me:MouseEvent)
{
    navigateToURL(new URLRequest(tn_url.text), tn_url_target.text);
}

link_to_205.addEventListener( MouseEvent.CLICK, goto_205 );

function goto_205(me:MouseEvent)
{
    navigateToURL(new URLRequest(""), "_parent");
}


tn_url_target.visible = false;
tn_title.visible = false;
tn_desc.visible = false;
url_button.visible = false;
tn_url.visible = false;
link_to_205.visible = false;

}

我添加了两个导航按钮。

  1. goFWD,其代码为imgFwd.addEventListener(MouseEvent.CLICK, go_out);
  2. 但是通过将此事件添加到按钮,按钮本身会执行补间,现在是mc:MovieClip。

    我不知道为什么会这样?也许有什么想法?

    我想,如果在AS3.0中有这样的东西 - 点击这个按钮会让AS认为mc:MovieClip被点击?有什么相似的吗?

    任何想法如何使用按钮让它前进...

    提前致谢!!!

1 个答案:

答案 0 :(得分:1)

单击goFWD按钮时应该补间什么?从快速看,我猜你想要补间thumbnail_group。是这样的吗?

MouseEvent.CLICK处理程序go_out中,您使用e.target投射当前目标(已点击的内容)。如果您想定位thumbnail_group mc,那么只需更改代码:

function go_out(e:MouseEvent):void
{

    var mc:MovieClip = MovieClip(e.target);
    mc.removeEventListener( MouseEvent.CLICK, go_out );
    Tweener.addTween( thumbnail_group.getChildAt(total-1), { x: 220, y: -180, rotation: 45, time: tween_duration,
                      transition: "easeInQuart", onComplete: go_in } );
}

如果这不是你想要的,那么请澄清,我会尽力帮助。