PDA

View Full Version : Sliding a mc addChild on MouseClick Event



aoh
December 5th, 2009, 10:33 PM
Hi,

I'm having trouble to slide a mc that loads external image through a addChild function.

How do I change the code in order to slide the mc that contains the new mc addChild that the loader has created?

Heres my code, I have been trying to crack this for days and I couldn't seem to solve it. Apologies if I don't make sense, trying to convert myself from AS2 to AS3 :(



var speed:Number=4;
var imageLoader:Loader;


//loading image via addChild function into mc

A_mc.addEventListener(Event.ENTER_FRAME, function(E:Event) {loadImage("images/img_gd_global_00.jpg");});
B_mc.addEventListener(Event.ENTER_FRAME, function(E:Event) {loadImage("images/img_gd_global_01.jpg");});


function loadImage(url:String):void {
imageLoader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(Pro gressEvent.PROGRESS, imageLoading);
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, imageLoaded);

}

function imageLoaded(E:Event):void {
addChild(imageLoader);

}
function imageLoading(E:ProgressEvent):void {
}


// sliding the MC name
A_mc.dx=new Number();
B_mc.dx=new Number();

A_mc.addEventListener(Event.ENTER_FRAME,display_my _name_F);
B_mc.addEventListener(Event.ENTER_FRAME,display_my _name_F);

function display_my_name_F(E:Event):void {
E.target.text=E.target.name;
E.target.x+=(E.target.dx-E.target.x)/speed;
}

//click function on MC name
A_mc.addEventListener(MouseEvent.CLICK,hit_me_F);
B_mc.addEventListener(MouseEvent.CLICK,hit_me_F);


function hit_me_F(ME:MouseEvent):void {
trace("total number of children in its parent:"+ME.target.parent.numChildren);
ME.target.parent.setChildIndex(ME.target, ME.target.parent.numChildren-1);// place it at the top
//ME.target.parent.setChildIndex(ME.target, 0);// place it at the bottom
if (ME.target.dx==0) {
ME.target.dx=-512;
} else {
ME.target.dx=0;
}
}


Thanks in advance!

cbeech
December 6th, 2009, 10:30 AM
well i'm sorry, but there are a lot of things wrong with your code here :(

the major problem is the use of the ENTER_FRAME event here - first it's not really meant for what your trying to use it for and there are other event handlers for these purposes, second you should never use more than 'one' ENTER_FRAME per scope instance, third you should always 'remove' the event once an action is completed (in this context), fourth because the event is not being removed it will be continuously trying to load new images - this is much of the issue, although there are better methods to achieve what you after in all other areas.

one thing i'm going to point out is the use of the Tween class native to Flash for simple transitions - this 'replaces' the concept of using an ENTER_FRAME event to monitor and reposition the instances (because it *can* be done that way - but is also inefficient)

so here is a basic example - this 'should' work in your environment replacing all code posted above, but it's difficult to tell not knowing the full structure of you file:



import fl.transitions.Tween;
import fl.transitions.easing.*;

var index:int = 0;
var containers:Array = [ A_mc, B_mc ];
var images:Array = [ "images/img_gd_global_00.jpg", "images/img_gd_global_01.jpg" ];

function loadImage():void {
var loader:Loader = new Loader();
loader.load( new URLRequest(images[index]) );

loader.contentLoaderInfo.addEventListener(Progress Event.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, loadComplete);

containers[index].addChild(loader);
containers[index].addEventListener(MouseEvent.CLICK, slideImage);
}

function loadProgress(e:Event):void { }

function loadComplete(e:Event):void {
e.target.parent.text = e.target.parent.name;
e.target.parent.tween = new Tween(e.target.parent, "x", Strong.easeOut, 0, e.target.parent.dx, 30, false);

if(index<images.length-1) {index++; loadImage();}
}

function slideImage(e:MouseEvent):void {
trace("total number of children in its parent:"+e.target.numChildren);

addChild(e.target); //top of display list
//addChildAt(e.target, 0) //bottom of display list

var position:int = (e.target.x != e.target.dx) ? e.target.dx : 0;
e.target.tween = new Tween(e.target, "x", Strong.easeOut, e.target.x, position, 30, false);
}


//start the process
loadImages();