PDA

View Full Version : Actionscript 3.0 Question



amilazzo
November 2nd, 2009, 06:38 PM
Hello,
Look over my script, it displays no error but it also displays no image. What am I doing wrong?


var req:URLRequest = new URLRequest("http://www.treehugger.com/barack-obama-for-president.jpg");
var loader:Loader = new Loader();
loader.load(req);
loader.alpha = 0;
loader.addEventListener(Event.ENTER_FRAME, fade);
addChild(loader);
function fade(event:Event):void
{
var frames:Number;
if(loader.alpha < 1)
{
if(frames <= 20)
{
loader.alpha += 0.05;
}
}
if(loader.alpha >= 1)
{
if(frames <= 20)
{
frames = 1;
}
if(frames > 20)
{
if(loader.alpha > 0)
{
loader.alpha -= 0.05
}
if(loader.alpha <= 0)
{
var req:URLRequest = new URLRequest("http://students.georgiasouthern.edu/greeklife/George-W-Bush.jpg");
var loader:Loader = new Loader();
loader.load(req);
loader.alpha = 0;
frames = 0;
}
}
}
}

cbeech
November 2nd, 2009, 08:28 PM
think this might be more what you're looking for - the main problems above are based in the multiple condition system that isn't really functioning. additionally, it seems the 'second' image url is incorrect.



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

var imageURL:Array = ["http://www.treehugger.com/barack-obama-for-president.jpg", "http://students.georgiasouthern.edu/greeklife/George-W-Bush.jpg"];
var index:int = 0;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, fadeIN);
addChild(loader);

var tween:Tween;

function loadImage():void {
loader.load( new URLRequest(imageURL[index]) );
(index < imageURL.length-1) ? index ++ : index = 0;
}

function fadeIN(e:Event):void {
tween = new Tween(loader, 'alpha', Strong.easeIn, 0, 1, 30, false);
tween.addEventListener(TweenEvent.MOTION_FINISH, fadeOUT);
}

function fadeOUT(e:TweenEvent):void {
tween = new Tween(loader, 'alpha', Strong.easeIn, 1, 0, 30, false);
tween.addEventListener(TweenEvent.MOTION_FINISH, function(){ loadImage(); });
}

loadImage();

amilazzo
November 2nd, 2009, 09:38 PM
@cbleech: yu simplified it so much but what i want it to do is fade in for 20 frames then hold the image for 20 frames then fade out for 20 frames then fade in... i just learned as3 today so i don't get your method or how to edit it

cbeech
November 3rd, 2009, 11:44 AM
cool - well good job, keep it up - the code is a bit more advanced, but you can get it, look up the Tween Class in Flash help (F1) for more information on how to play with tweens - the second to last parameter indicates the number of frames that the tween will take place, the last param indicates if that value is in 'frames' (false) or 'milliseconds' (true) - one can use either, in this case frames.

- ok so here's a easy way to get the hold between fades, use an 'anonymous' function in the TweenEvent listener, and using setTimeout (you can also find that in the help docs), the second parameter indicates the amount of time in milliseconds until the method indicated in the first param is fired - (at 30fps frame rate, 20 frames is approximately 660)



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

var imageURL:Array = ["http://www.treehugger.com/barack-obama-for-president.jpg", "http://students.georgiasouthern.edu/greeklife/George-W-Bush.jpg"];
var index:int = 0;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, fadeIN);
addChild(loader);

var tween:Tween;

function loadImage():void {
loader.load( new URLRequest(imageURL[index]) );
(index < imageURL.length-1) ? index ++ : index = 0;
}

function fadeIN(e:Event):void {
tween = new Tween(loader, 'alpha', Strong.easeIn, 0, 1, 20, false);
tween.addEventListener(TweenEvent.MOTION_FINISH, function(){ setTimeout(fadeOUT, 660); });
}

function fadeOUT():void {
tween = new Tween(loader, 'alpha', Strong.easeIn, 1, 0, 20, false);
tween.addEventListener(TweenEvent.MOTION_FINISH, function(){ loadImage(); });
}

loadImage();