PDA

View Full Version : variables with array access ?



Guinness
April 1st, 2008, 07:17 PM
hello

pretty new to actionscript 3.0 and was wondering why this doesnt work



var bonkers:Array = new Array(i);
bonkers[0] = new oldie();
bonkers[1] = new chtv();
bonkers[2] = new every();
bonkers[3] = new jerkwtv();
bonkers[4] = new hawtv();
var i:int;
var myTimer:Timer = new Timer(delay, repeat);
var delay:uint = 30000;
var repeat:uint = 6;
//-- MovieClips & Sprites --\\
for (i = 0; i < 5; i++);{
addChild(bonkers).name = "bonkers" + i;
bonkers.x = 320;
bonkers.y = 214.5;
}

and i have tried this many different ways with the addChild()


for (i = 0; i < 5; i++);{
bonkers[i].addChild();
bonkers.x = 320;
bonkers.y = 214.5;
}



for (i = 0; i < 5; i++);{
addChild(bonkers[i]);
bonkers.x = 320;
bonkers.y = 214.5;
}

I get different ranging errors 2007, 1010 and others. Any help would be great.

Felixz
April 2nd, 2008, 11:04 AM
bonkers[0] = new oldie();
bonkers[1] = new chtv();
bonkers[2] = new every();
bonkers[3] = new jerkwtv();
bonkers[4] = new hawtv();
Are those classes extending Sprite class?

Guinness
April 2nd, 2008, 12:50 PM
Thanks for the help here are my thoughts
I am tring to use images to make a slideshow, images that will flash from one to the next
with a timer. I am not even to the point of using a timer tho because i am stuck tring to figure out adding and removing the movieclips. So i made each image a movieclip and their names are the following:
oldie
chtv
every
jerkwtv
hawtv
these are movieclips right now
each one of these that i say bonker[0] = new oldie();
oldie is the name of one of my movieclips i think i might have missed a step here or something it keeps saying bonkers in null or whatever i put in the addChild() makes the
2067 come up saying that the addChild cant be null



import flash.display.Loader;
import flash.display.LoaderInfo;
var imageLoader:Loader = new Loader();
var bonkers:Array = new Array();
bonkers[0] = new oldie();
bonkers[1] = new chtv();
bonkers[2] = new every();
bonkers[3] = new jerkwtv();
bonkers[4] = new hawtv();
var i:int;

//-- MovieClips & Sprites --\\
for (i = 0; i < 5; i++);{
var myRequest:URLRequest = new URLRequest(bonkers[i]);
imageLoader.load(myRequest);
addChild(imageLoader);
imageLoader.y = 320;
imageLoader.x = 214.5;
}

omine
April 2nd, 2008, 01:34 PM
What exact error message are you getting when using addChild(bonkers[i]) ?

omine
April 2nd, 2008, 01:36 PM
BTW:


var myRequest:URLRequest = new URLRequest(bonkers[i]);


Won't work because the parameter to be passed to the URLRequest constructor must be a String.

wvxvw
April 2nd, 2008, 02:39 PM
Huh... seems that these: oldie, chtv, every, jerkwtv, hawtv are clips on the main timeline... So, you don't need to use new and (), they are already instantiated, just the names (they act like variables).

You should declare variables before you use them (declare delay and repeat before you instantiate timer).

Guinness
April 2nd, 2008, 08:04 PM
Here is more of the code ok mostly i would forget about those button functions cause i will set those up to do what i want when i figure out how this works
seriously how can i access the array with that variable
what about some way to just assign the variable[i] to a new variable and loop i cant figure it out and i am sitting here eatting my cup o noodles crying because it may be my last meal... :crying: well it isnt that bad yet but it may be soon !


import flash.events.MouseEvent;
import fl.motion.easing.*;
import flash.events.Event;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.TimerEvent;
import flash.media.SoundTransform;
import flash.geom.ColorTransform;
import flash.media.SoundChannel;
import flash.utils.*;

//-- Varibles --\\

var a:Number = new Number();
var b:Number = new Number();
var c:Number = new Number();
var d:Number = new Number();
var pile:pilecomps = new pilecomps();
var china:chtv = new chtv();
var everglade:every = new every();
var jerk:jerkwtv = new jerkwtv();
var pinky:pinktv = new pinktv();
var hawt:hawtv = new hawtv();
var bonkers:Array = new Array();
bonkers[0] = pile;
bonkers[1] = china;
bonkers[2] = everglade;
bonkers[3] = jerk;
bonkers[4] = pinky;
bonkers[5] = hawt;
var bonSpot:Number = 0;
var myTimer:Timer = new Timer(delay, repeat);
var delay:uint = 30000;
var repeat:uint = 6;

//-- MovieClips & Sprites --\\

//-- Timer --\\
function Timer_constructorExample() {
myTimer.start();
for (i = 0; i < 5; i++) {
addChild(bonkers[i]);
removeChild(bonkers[i--]);
bonkers.x = 320;
bonkers.y = 214.5;
}
}
function timerHandler(e:TimerEvent):void {
repeat--;
}
//-- Listeners --\\
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE , completeHandler);
R_btn.addEventListener(MouseEvent.MOUSE_OVER, rOver);
R_btn.addEventListener(MouseEvent.CLICK, rClick);
R_btn.addEventListener(MouseEvent.MOUSE_OUT, rOut);
t_btn.addEventListener(MouseEvent.MOUSE_OVER, tOver);
t_btn.addEventListener(MouseEvent.CLICK, tClick);
t_btn.addEventListener(MouseEvent.MOUSE_OUT, tOut);
i_btn.addEventListener(MouseEvent.MOUSE_OVER, iOver);
i_btn.addEventListener(MouseEvent.CLICK, iClick);
i_btn.addEventListener(MouseEvent.MOUSE_OUT, iOut);
e_btn.addEventListener(MouseEvent.MOUSE_OVER, eOver);
e_btn.addEventListener(MouseEvent.CLICK, eClick);
e_btn.addEventListener(MouseEvent.MOUSE_OUT, eOut);
//-- functions --\\
function rOver(event:MouseEvent):void {
pilecomps.removeChild();
oldie.removeChild();
jerkwtv.removeChild();
hawtv.removeChild();
every.removeChild();
chtv.removeChild();
}
function rOut(event:MouseEvent):void {
pinktv.alpha = 1;
pilecomps.alpha = 0;
oldie.alpha = 0;
jerkwtv.alpha = 0;
hawtv.alpha = 0;
every.alpha = 0;
chtv.alpha = 0;
}
function eOver(event:MouseEvent):void {
pinktv.alpha = 1;
pilecomps.alpha = 0;
oldie.alpha = 0;
jerkwtv.alpha = 0;
hawtv.alpha = 0;
every.alpha = 0;
chtv.alpha = 0;
}
function eOut(event:MouseEvent):void {
pinktv.alpha = 1;
pilecomps.alpha = 0;
oldie.alpha = 0;
jerkwtv.alpha = 0;
hawtv.alpha = 0;
every.alpha = 0;
chtv.alpha = 0;
}
function tOver(event:MouseEvent):void {
pinktv.alpha = 1;
pilecomps.alpha = 0;
oldie.alpha = 0;
jerkwtv.alpha = 0;
hawtv.alpha = 0;
every.alpha = 0;
chtv.alpha = 0;
}
function tOut(event:MouseEvent):void {
pinktv.alpha = 1;
pilecomps.alpha = 0;
oldie.alpha = 0;
jerkwtv.alpha = 0;
hawtv.alpha = 0;
every.alpha = 0;
chtv.alpha = 0;
}
function iOut(event:MouseEvent):void {
pinktv.alpha = 1;
pilecomps.alpha = 0;
oldie.alpha = 0;
jerkwtv.alpha = 0;
hawtv.alpha = 0;
every.alpha = 0;
chtv.alpha = 0;
}
function iOver(event:MouseEvent):void {
pinktv.alpha = 1;
pilecomps.alpha = 0;
oldie.alpha = 0;
jerkwtv.alpha = 0;
hawtv.alpha = 0;
every.alpha = 0;
chtv.alpha = 0;
}

wvxvw
April 3rd, 2008, 03:40 AM
Just a few points...
removeChild needs a display object which is child of the object that called that function as an argument, not a number...
Calling constructor function of intrisic built-in classes like Number, String etc is a... strange thing to do, normaly you would just assign a value.
You for loop will never end, you decrement the iterator right after you increment...
Yet again, declare delay and repeat before you create new instance of timer. And it's a bed idea to create instances like this. More common is to declare a variable pointing to the instance and create the instance it should point to in some method of the class that declared the variable.
The same would be true for array.
Adobe recommended syntax for declaring array-type variables is this:
var aray:Array = [];
or if you want it to contain some values at this point:
var aray:Array = [someValue1, someValue2, ..., someValueN];
Following Adobe coding conventions for AS, class names should start with the capital letter, public variables with small letter, private variables - with underscore, static var - all capitals...
I don't really understand what the addEventListener part intended for, but, most probably it won't work too... Normally you would call functions inside method of the class...
You should import classes before you declare variables of their type / use their methods...

As a general advice - try to start with something less complicated and add complications after you successfully compiled what you've done before... This code should throw a bunch of errors/warnings, you'll just loose the track of what's going on there...

Guinness
April 3rd, 2008, 11:27 AM
Just a few points...
removeChild needs a display object which is child of the object that called that function as an argument, not a number...
Calling constructor function of intrisic built-in classes like Number, String etc is a... strange thing to do, normaly you would just assign a value.
You for loop will never end, you decrement the iterator right after you increment...
Yet again, declare delay and repeat before you create new instance of timer. And it's a bed idea to create instances like this. More common is to declare a variable pointing to the instance and create the instance it should point to in some method of the class that declared the variable.
The same would be true for array.
Adobe recommended syntax for declaring array-type variables is this:
var aray:Array = [];
or if you want it to contain some values at this point:
var aray:Array = [someValue1, someValue2, ..., someValueN];
Following Adobe coding conventions for AS, class names should start with the capital letter, public variables with small letter, private variables - with underscore, static var - all capitals...
I don't really understand what the addEventListener part intended for, but, most probably it won't work too... Normally you would call functions inside method of the class...
You should import classes before you declare variables of their type / use their methods...

As a general advice - try to start with something less complicated and add complications after you successfully compiled what you've done before... This code should throw a bunch of errors/warnings, you'll just loose the track of what's going on there...

Thanks for giving it so me straight. Ill work on those parts for a bit i guess.