View Full Version : How to load first item in Array
vxd
August 24th, 2008, 11:22 PM
Hi,
I'm trying to load the first swf in my Array automatically when the flash movie starts.
Here's my code, does anyone know how to?
var aImages:Array = new Array("01.swf", "02.swf", "03.swf", "04.swf");
var aButtons:Array = new Array(bHome, bProfile, bFolio, bContact);
var myLoader:Loader = new Loader();
function btnClick(evt:Event):void {
trace(evt.target.name);
var countButtons:Number = aButtons.length;
var index:Number;
for (var i:Number = 0; i<countButtons; i++) {
if (evt.target.name == aButtons[i].name) {
index = i;
}
}
trace( aImages[index] + " Loaded");
myLoader.load(new URLRequest(aImages[index]));
mContainer.addChild(myLoader);
}
var countButtons:Number = aButtons.length;
for (var i:Number = 0; i<countButtons; i++) {
aButtons[i].addEventListener(MouseEvent.CLICK, disableButtons);
aButtons[i].addEventListener(MouseEvent.CLICK, btnClick);
aButtons[i].mouseChildren = false;
}
function disableButtons(evt:Event):void {
for (var i:Number = 0; i<aButtons.length; i++) {
if (evt.target.name == aButtons[i].name) {
trace(evt.target.name + " has been clicked");
aButtons[i].mouseEnabled= false;
aButtons[i].alpha = .5;
} else {
aButtons[i].mouseEnabled= true;
aButtons[i].alpha = 1;
}
}
}
thanks
vxd
REEFˇ
August 24th, 2008, 11:26 PM
Add this after your code:
myLoader.load(new URLRequest(aImages[0]));
mContainer.addChild(myLoader);
Probably want a event listener to made sure the movie is only added once the preloading has completed.
*Why do you have 2 countButtons variables if they both store the same info? You dont need the one in the btnClick function if you just move the one outside with the rest of your var definitions.
vxd
August 25th, 2008, 12:26 AM
Thanks REEF, It works. I'm still trying to figure out how to add a preloader.
REEFˇ
August 25th, 2008, 12:55 AM
Google it, it's so easy and understandable with all the tutorials out there. There's even a preloading in AS3 on kirupa.com if I'm not wrong.
vxd
August 25th, 2008, 01:42 AM
I've already made the preloader, I just need to add it in.
thanks Reef
vxd
August 25th, 2008, 04:56 AM
I've tried adding the preloader but it's not working.
What am I doing wrong?
here's my code
var aImages:Array = new Array("pic01.jpg", "pic02.jpg", "pic03.jpg", "pic04.jpg");
var aButtons:Array = new Array(bHome, bProfile, bFolio, bContact);
var myLoader:Loader = new Loader();
var movieLoader:Loader;
function setProgress(value:Number) {
mPreloader.mProgress.width = value*mPreloader.mBase.width;
}
function movieLoading(evt:ProgressEvent):void {
var loaded:Number = evt.bytesLoaded / evt.bytesTotal;
var percent:int = loaded * 100;
mPreloader.tLoaderInfo.text = percent;
setProgress(loaded);
}
myLoader.contentLoaderInfo.addEventListener(Progre ssEvent.PROGRESS, movieLoading);
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, movieLoaded);
function movieLoaded(evt:Event):void {
myLoader.load(new URLRequest(aImages[0]));
mContainer.addChild(myLoader);
}
function btnClick(evt:Event):void {
trace(evt.target.name);
var index:Number;
for (var i:Number = 0; i<countButtons; i++) {
if (evt.target.name == aButtons[i].name) {
index = i;
}
}
trace( aImages[index] + " Loaded");
myLoader.load(new URLRequest(aImages[index]));
mContainer.addChild(myLoader);
}
var countButtons:Number = aButtons.length;
for (var i:Number = 0; i<countButtons; i++) {
aButtons[i].addEventListener(MouseEvent.CLICK, disableButtons);
aButtons[i].addEventListener(MouseEvent.CLICK, btnClick);
aButtons[i].mouseChildren = false;
}
function disableButtons(evt:Event):void {
for (var i:Number = 0; i<aButtons.length; i++) {
if (evt.target.name == aButtons[i].name) {
trace(evt.target.name + " has been clicked");
aButtons[i].mouseEnabled= false;
aButtons[i].alpha = .5;
} else {
aButtons[i].mouseEnabled= true;
aButtons[i].alpha = 1;
}
}
}
REEFˇ
August 25th, 2008, 06:46 PM
var aImages:Array = new Array("pic01.jpg", "pic02.jpg", "pic03.jpg", "pic04.jpg");
var aButtons:Array = new Array(bHome, bProfile, bFolio, bContact);
var myLoader:Loader = new Loader();
var movieLoader:Loader = new Loader();
var countButtons:int = aButtons.length;
for (var i:int = 0; i<countButtons; i++) {
aButtons[i].addEventListener(MouseEvent.CLICK, disableButtons);
aButtons[i].addEventListener(MouseEvent.CLICK, btnClick);
aButtons[i].mouseChildren = false;
}
myLoader.contentLoaderInfo.addEventListener(Progre ssEvent.PROGRESS, movieLoading);
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, movieLoaded);
myLoader.load(new URLRequest(aImages[0]));
// ######### FUNCTIONS -------------------------------------------------
function setProg(val:Number) {
mPreloader.mProgress.width = val*mPreloader.mBase.width;
}
function movieLoading(evt:ProgressEvent):void {
var loaded:Number = evt.bytesLoaded / evt.bytesTotal;
var percent:int = loaded * 100;
mPreloader.tLoaderInfo.text = percent;
setProg(loaded);
}
function movieLoaded(evt:Event):void {
mContainer.addChild(myLoader);
}
function btnClick(evt:Event):void {
trace(evt.target.name);
var index:int;
for (var i:int = 0; i<countButtons; i++) {
if (evt.target.name == aButtons[i].name) {
index = i;
}
}
trace( aImages[index] + " Loaded");
myLoader.load(new URLRequest(aImages[index]));
mContainer.addChild(myLoader);
}
function disableButtons(evt:Event):void {
for (var i:int = 0; i<countButtons; i++) {
if (evt.target.name == aButtons[i].name) {
trace(evt.target.name + " has been clicked");
aButtons[i].mouseEnabled= false;
aButtons[i].alpha = .5;
} else {
aButtons[i].mouseEnabled= true;
aButtons[i].alpha = 1;
}
}
}By the way, if you see a word turning blue when you make a var name or function name, don't use it for your own tricks. You can't access them later if you take Flash's built in keywords :sure:.
vxd
August 26th, 2008, 01:00 AM
You are the greatest REEF.
REEFˇ
August 26th, 2008, 01:15 AM
I don't know about greatest but thank you! I rule in solving noob problems (problem is, I cant solve my own) :P
vxd
August 26th, 2008, 04:06 AM
Hey REEF,
I've got another problem I need solved.
Once the first swf is loaded how do I get the first link to be disabled and alpha? It gets disabled and alpha when I click on it but not when it is loaded from the start.
I'd tried adding
myLoader.addEventListener(Event.ENTER_FRAME, disableButtons);
but that didn't work.
Thanks again
vxd
hasch2o
August 26th, 2008, 04:15 AM
Hey vxd
try putting it in this function
function movieLoaded(evt:Event):void {
mContainer.addChild(myLoader);
aButtons[0].mouseEnabled= false;
aButtons[0].alpha = .5;
}
this function will fire when the movie is loaded
(i hope i understood you correctly)
cheers
vxd
August 26th, 2008, 04:37 AM
Yeah I understand it, It works but aButton[0] will stay disabled and have a alpha of 0.5 even when I click on another link.
Thanks
vxd
vxd
August 26th, 2008, 04:43 AM
Thanks, hasch2o. I've figured it out, I added
aButtons[0].mouseEnabled= false;
aButtons[0].alpha = .5;
outside the function
hasch2o
August 26th, 2008, 05:00 AM
Ah, now i see it.
sorry, it was my fault, of course it stays disabled because it will be called everytime.
you did the right thing, not sure if it's clean programming though.
and where did you put it? just so you don't make your code unreadable, i would put those 2 lines after:
myLoader.load(new URLRequest(aImages[0]));
on the other hand you could also use a dispatch event (didn't work that much with those yet though)
aButtons[0].dispatchEvent(MouseEvent.CLICK);
//hope that works don't have flash to test it!
cheers
vxd
August 26th, 2008, 05:14 AM
Yeah I placed it under
myLoader.load(new URLRequest(aImages[0]));
I know it's not clean coding but I haven't got a clue of any other way to do it.
I'll try the dispatchEvent now and let you know how I go.
vxd
hasch2o
August 26th, 2008, 05:24 AM
I know it's not clean coding but I haven't got a clue of any other way to do it.
^ neither do I :)
dispatchEvent just seems to be cleaner to me because if you need to change something in the function "disableButtons" you don't need to check if you need to add it at the other part as well.
looking forward to see if it works.
(I know it's possible, just don't know if I am the right guy to be asked :) )
vxd
August 26th, 2008, 06:15 AM
1120: Access of undefined property aButton.
Well I've been reading up on dispatchEvent() I haven't got a clue when or where to use it.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.