Groups | Blog | Home
all groups > flash actionscript > january 2005 >

flash actionscript : Help with Array


David Stiller
1/20/2005 4:12:29 PM
TinMonkey,

All you need, as far as I can tell, is your array. I don't see any
value in creating additional variables that point to array indexes, since
you can always simply reference those indexes directly.

[quoted text, click to view]

Let's step through what you have so far.

// Change image function
function nextImage1(number) {
if(number >= 0 && number < images.length) {
currentImage1 = number;
_root.viewer.contentHolder.pic1LoadBar._visible=true;
pic1Content = images[number][0];
pic1.load();
}
}

You'd call this function with a parameter of some kind. Let's say ...

myButton.onRelease = function() {
nextImage(5);
}

So nextImage() will be called. First thing it will do is is check if
its parameter, number (in this case, 5), is greater than or equal to zero
(yes) and less than the length of the images array (yes; images holds ten
items). Because this condition evaluates to true, it proceeds to set a
variable currentImage1 to number; that is, to set currentImage1 to 5. But
why? Where is this variable used?

Next, a nested movie clip is set to be visible (pict1LoadBar, which
resides within contentHolder, which resides within viewier, which resides in
the _root). Is this your loader component? If so, is this the Loader UI
component that ships with Flash? If so, you can look up the Loader class in
the ActionScript dictionary to see all the methods and properties available
to you for this class.

I see Loader.contentPath as a property, which specifies the path to the
external JPG or SWF. That means you can reference an instance of this
component and address its contentPath property. So assuming the instance
name of your Loader componet is pic2 (as you've said), you would reference
it like this:

pic2.contentPath = something ...

-- of course, the path to this component is up to you. If it sits in the
_root and your code is in the root, you don't need a path. Simply pic2 will
work. If your code is somewhere else but pic2 is in the _root, you might
write ...

_root.pic2contentPath = something ...

Okay, and you know what the image is, because it's based on the number
that was passed to this function (5). So your final function might look
something like this:

function nextImage1(number) {
if (number >= 0 && number < images.length) {
_root.pic2.contentPath = images[number - 1];
}
}

.... which essentially looks to the images array at the index provided.
Arrays start at zero, not one, so if you want to call the fifth image, you
need to specify index 4. (That's the number - 1 part.) And that's it.
pic2's contentPath property will be set to images[4], which is image5, as
specified earlier.

The only problem with your initial array is that you haven't supplied
strings as your array entries.

//create image array
var images:Array = new Array();
images[0] = image1;
images[1] = image2;
// etc.

Since image1 isn't in quotes, ActionScript is looking for a variable
named image1 that contains ... who knows what? No. What you want is for
images[0] to literally equal image1, so ...

var images:Array = new Array();
images[0] = "image1";
images[1] = "image2";


Make sense?


David
stiller (at) quip (dot) net
"Luck is the residue of good design."



David Stiller
1/20/2005 5:13:08 PM
TinMonkey,

I'm on my way home from the office, so this is quick. I'll take a look
at your file later.

[quoted text, click to view]

You can handle this with a variable. Just have your button, onRelease,
advance the variable in question, then call the function.

// keyframe script ...
var currentImage = 0;
myButton.onRelease = function() {
nextImage(currentImage);
currentImage++;
}

Does that make sense? You're simply creating a variable called
currentImage (or whatever you want) and setting it to zero. When the button
is released, it calls nextImage() as before, then increments currentImage by
1 (currentImage++ is the same as saying currentImage = currentImage + 1).

And there you have it. The first time you click, it will tell
nextImage() to open image 0. After that, image 1. After that, image 2, and
so on. Your nextImage() function works exactly as before.


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

TinMonkey
1/20/2005 8:15:05 PM
I have an array that dynamically holds the address of 10 images. the array is
named images. I need to set up a script so that when i press a button it
changes the path of a loader component i have on stage called pic2 to the next
element in the array and loads it. here is what I am trying so far...I would
appreciate any help I can get.

//create image array
var images:Array = new Array();
images[0] = image1;
images[1] = image2;
images[2] = image3;
images[3] = image4;
images[4] = image5;
images[5] = image6;
images[6] = image7;
images[7] = image8;
images[8] = image9;
images[9] = image10;

//variables to hold loader content paths
pic1Content = images[0];
pic2Content = images[1];
pic3Content = images[2];
pic4Content = images[3];
pic5Content = images[4];

//change image function
function nextImage1(number){
if(number >= 0 && number < images.length){
currentImage1 = number;
_root.viewer.contentHolder.pic1LoadBar._visible=true;
pic1Content = images[number][0];
pic1.load()
}
}
TinMonkey
1/20/2005 10:07:31 PM
Thank you for the reply...I did not explain my project very well at all..let me
start over...first of all here is the published file so you can see how it
works... http://www.marketease.com/brochure/


now a layout of the project. I have one main file that loads other design
template swfs dynamically. the templates contain instances of the loader
component. all the images to be used are uploaded to my server with a php
script and then the addresses are passed to the main swf file with a loadvars
function. after the addresses are loaded i want to enter them into an array, so
that the images can be browsed through in each instance of the loader
component. I would like the loader components to intitially load the first 3
elements of the array ( i.e. instance 1 will load first element, instance 2
will load second and so on) ok, that should explain the project better. now to
your response..

I am fairly new to scripting, so I did not realize that a I could load my
image path varaibles right into the array, so I will get rid of the image1-10
varables and reference them directly.
pic1LoadBars is an instance of the progress bar component.

so far I understand your response and i will give it all a try... the one
thing I am still not sure of is where you said this "... which essentially
looks to the images array at the index provided.
Arrays start at zero, not one, so if you want to call the fifth image, you
need to specify index 4. (That's the number - 1 part.) And that's it.
pic2's contentPath property will be set to images[4], which is image5, as
specified earlier."
The problem is I wont have a set number I want...just the next in line..if
image[3] is showing I want it to go to image 4 on(release) then if it is
pressed again it will go to image 5 etc. that is the part I am most confused
about, I want it to somehow take the value of the image that is being displayed
then show the next...does this make sense?

TinMonkey
1/20/2005 11:34:36 PM
AddThis Social Bookmark Button