PDA

View Full Version : help me convert this to AS3


malx
01-02-2008, 03:01 PM
So in As3 I wrote this function called Fade and what it did was it would fade any MC I chose when I called it. Here's the AS2 code.

In as2...

function fadeMC($type, $mc, $op1, $op2, $speed) {
if ($type == 'in') {
$mc.alpha = $op1;
$mc.onEnterFrame = function() {
$mc.alpha = $mc.alpha+$speed;
if ($mc.alpha>=$op2) {
delete $mc.onEnterFrame;

}
};
}

if ($type == 'out') {
$mc.alpha = $op1;
$mc.onEnterFrame = function() {
$mc.alpha = $mc.alpha-$speed;
if ($mc.alpha<=$op2) {
delete $mc.onEnterFrame;
}
};
}
}


This worked great when I called somthing like..

fadeMC(myMovieClip, 'in', 0, 100, 1);

so if I had my movie set at 60fps it would take a total of 1 second to fade the alpha of the movie clip.

How would I do this in as3? I've tried creating custom event listeners but I can't seem to get it right. Is it me or is this really hard? Or is it really easy and I'm just stupid.

So far I have something like this in AS3..


package com.amediacreative.amedia {

import flash.events.*;
import flash.display.*;

class CustomEvent extends MovieClip { //not sure if I should extend MC

private var $mc:Namespace;
private var $op1:Number; // Opacity
private var $op2:Number;
private var $type:Namespace;

//this.addEventListener(Event.ENTER_FRAME,EnterFrame Handler);

public function customEvent() {

// Blank Constructor

}

public function fadeMC($type, $mc, $op1, $op2, $speed):void {

// NEED to put AS3 code here.

}
}
}


Thanks for helping me in advance!

stompwampa
01-02-2008, 04:21 PM
I would create a Tween to do it, and then pass in to the tween the appropriate properties:


var fadeTween:Tween;

function fadeMC(fadeObject,startAlpha,endAlpha,fadeSpeed):v oid
{
fadeTween = new Tween(fadeObject,"alpha",None.easeNone,startAlpha,endAlpha,fadeSpeed,true) ;
//note: if your duration is in seconds, the set the last value to true.
//if the duration is in milliseconds, then set the last value to false.
}

myMovieClip.addEventListener(MouseEvent.CLICK, onClickFade);

function onClickFade(event:MouseEvent):void
{
fadeMC(event.target,0,1,1);
}

/*here, when the user clicks on the movie clip, it passes istelf in as
the object to be faded (event.target), with a startAlpha of 0,
endAplpha of 1, and a fadeSpeed of one second*/

malx
01-02-2008, 08:16 PM
I didn't know you can do this via tween. I'll look into your advice in a little bit.. thanks!

malx
01-03-2008, 03:09 AM
I F***IN LOVE YOU YOU HAVE NO IDEA! WOW THIS WORKED GREAT!

Have my babies! Thank you so much.

xwielder
01-03-2008, 11:39 AM
I F***IN LOVE YOU YOU HAVE NO IDEA! WOW THIS WORKED GREAT!

Have my babies! Thank you so much.

Wow. It's almost frightening to think if stompwampa would have replied with something that didn't work. <cowers in fear>

malx
01-03-2008, 01:07 PM
hahaha

stompwampa
01-03-2008, 03:21 PM
I F***IN LOVE YOU YOU HAVE NO IDEA! WOW THIS WORKED GREAT!

Have my babies! Thank you so much.

Haha! I don't know if my wife would appreciate me having someone else's babies.
But you're welcome anyways :-) I'm always glad to help.