开发者

Mouse click handling in As3.0 gallery

开发者 https://www.devze.com 2023-04-08 08:34 出处:网络
I\'m playing around with a gallery. I want to add some more functionality. HERE\'s the initial code: import flash.events.Event;

I'm playing around with a gallery. I want to add some more functionality.

HERE's the initial code:

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 fl开发者_Python百科ashmo_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;

}

I added two buttons for navigation.

  1. goFWD that would have a code imgFwd.addEventListener(MouseEvent.CLICK, go_out);

but by adding this event to the button, button itself does the tween, and now the mc:MovieClip.

I don't know why it does that? maybe any ideas?

I thought, if there's such a thing in AS3.0 - a click on this button makes AS think that mc:MovieClip is clicked?? is there anything like that?

Any ideas how to make it go forward with a button...

Thanks in advance!!!


What should be tweening when you click the goFWD button? From a quick look, I would guess you want to tween thumbnail_group. Is that the case?

In your MouseEvent.CLICK handler, go_out, you are casting the current target (the thing that was clicked) by using e.target. If you want to target the thumbnail_group mc, then just change the code like so:

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 } );
}

If this is not what you are looking for, then please clarify and I will try to help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号