Using input text to go to a frame (flash5)

I am working on a sort of photo gallery and I would like to know if the following is possible:

I’d like to create an option where you can fill in a name in a input text box and press a button so you automatically go to the photo of that person. The input text box is called namebox, the frames with pictures of the persons have frame names like bob, franca and patrick. The button next to the input box has this script:

[COLOR=red]on (press, keyPress “<Enter>”) {
if (namebox == “Bob”) {
gotoAndStop (“scene2”, “bob”);
if (namebox == “Franca”) {
gotoAndStop (“scene2”, “franca”);
if (namebox == “Patrick”) {
gotoAndStop (“scene2”, “patrick”);
}
}
} else {
gotoAndStop (2);
}
}
[/COLOR]

Unfortunately this does not work ( only the goto bob works )

Could someone please help?

take out those scene names. As long as the frame names are correct you shouldn’t need them, AND scene names have a tendency of screwing up goto statements. Other than that, the code looks fine. Try that first, if that doesn’t solve the problem write back.

I think the problem is because I have about 4 scenes in my movie.
I tested it with one and two scenes and it worked fine. After I added a third scene, a test movie I made started to go weird. The goto action did not work properly anymore.
I posted this question on flashkit as well and this is the code I got from catbert303. It works as long as the movie is not more then 2 scenes.

[COLOR=red]on (press) {
names = [“bob”, “franca”, “patrick”];
frameName = namebox.toLowerCase();
found = false;
for (var i = 0; i < names.length; ++i) {
if (frameName == names*) {
found = true;
break;
}
}
if (found) {
gotoAndStop(frameName);
} else {
gotoAndStop(2);
}
}
{[/COLOR]

Thanks for helping.

P

That’s really quite peculiar. I don’t know why it would work with 2 scenes but not with three. The code you got from Flashkit looks very sound though. If it’s working for you, great.

in general, I don’t use more than one scene. I don’t see a need for it, organizationaly, and it just causes tweeky problems, as you’ve seen. :slight_smile:

I made the same mistake with my 1st .fla ever. You think it makes things more clear during the creation process but actually, scenes should be only used (if at all…) when your content is completely different from one to the other and you don’t go between the 2.
In short: scenes suck. :slight_smile:
Also, in your case, instead of going to different frames for each pictures, you might want to turn them into movie clips and then use the code the guy gave you on Flash Kit to attach those picture to an empty mc on the stage. Like that, they’ll all show up exactly at the same position and it’ll be more convenient to edit later: just add the pict in your lib and change your array…

thanks for your reply. I do not have a lot of experience, especially not with array’s, but I would like to try that. Could you explain to me how this would work? with a code I could try perhaps?

thanks in advance

Let me see what I can relate about attaching movieclips dynamicaly. (with array) :slight_smile:

Attaching a movie clip is kind of like duplicating one, except that you’re grabbing the object from the library and inserting it into the flash player at run time. In order to do this (because obviously it has no instance name in the library) we use something called ‘linkage’.
Linkage is created by selecting the movie clip in the library, right clicking on it, and selecting ‘linkage’ from the drop down menu. In the dialogue box that opens, select ‘export this object’, and give it a unique identifier in the field provided. Then just hit ‘ok’. The unique identifier is equivilent to an ‘instance’ name for control of the object. Normaly the only thing that gets incorporated into the final swf is anything that is in the timeline. What this does is set the object to be exported WITH the swf.

now arrays are very cool, and easy to set up. They are objects like any other in Flash, and need to be created with a/s rather than with any of the art tools. An array can be created and populated upon creation, or you can add things to an array that has been created already. Like a variable (at least in Flash5.0) an array is accessable from the location where it is created, so if you define the array on the main timeline, then you have to use _root to access it from anywhere other than the main timeline. (more on that later) A simple array creation script is like

myCoolArray=new Array(david, lostinbeta, syntax, kirupa);

This is an array that is being populated while it’s being created. you can see that I’ve named it (like you would a variable) then I’ve told it to be equal to the new array() method. The method ‘new array()’ can take arguements, which I’ve given it. these are translated into a numbered list, the first arguement being the first item on the list, and the last being the last.
With the above array created, I can use the following code to find out things about the array.

lengthOfMyArray=myCoolArray.length();

will set the variable lengthOfMyArray to 4 which is the number of items in the array.

objectOne=myCoolArray[1];

will set the variable ‘objectOne’ to “lostinbeta”. We use square brackets to either set the items in an array, or to access them. Keep in mind that all arrays start with 0 as the first item. so really this list looks like this
item number
0 - dave
1 - lostinbeta
2 - syntax
3 - kirupa
there are four items, but the first is number 0 and the last is number 3. I believe that this is the most confusing part of arrays… and it’s pretty easy once you get used to thinking of them this way.

myCoolArray[4]=paddy;

will set a 4th item in the list and make it equal to the string “paddy”.

now if we try this again

lengthOfMyArray=myCoolArray.length();

it will set lengthOfMyArray to 5 instead of 4. The length being the actual number of entries.


now… how to do what he’s talking about. :slight_smile:

Import a pic. select it. hit F8. choose movieclip and give it a library name. Repeat for each picture. After each one becomes a movie clip in the library, you can delete it from the stage.

Use the above instructions to create ‘linkage’ for each pic from the library. In my example I’m going to say that I have three pictures set as movie clips and that I’ve named their linkage “bob”, “franca”, and you guessed it “patrick”.

On my main timeline, in the very first frame (or after the preloader if you have one) I use the following code to create my array.

names = new Array(bob,franca,patrick);

now… with a button and the same text field you used before on the main timeline you can use this code on the button to open up any of the pictures you like.


on (press) {
  picName = namebox.toLowerCase();
  for (var i = 0; i < names.length; ++i) {
  if (picName == names*) {  
    _root.attachMovie(picName,picName,300);
    _root[picName]._x=0;
    _root[picName]._y=0;
    //you can set the 0 in these statements to be equal to where-
    //ever you would like the pic to appear on the stage.
  }
}

‘300’ in the attachMovie method is the depth. It can really be anything, but I like to set this pretty high, in case I have other things being attached. Only one item can be at the same depth at the same time. If a new item is placed at the same depth as another, it will delete the previous one. So… if the text field has another name set, and the button is pressed a second time, the last pic will be removed from view by the above script.
the full syntax of attachMovie(); is as follows
movieClipName.attachMovie(linkageName,instanceName,depth);
The linkage name must be the same name that you set the linkage to in the library. The instance name can be anything, and is what you use to direct that object. In this case I used the same thing as the linkage name. And the depth, I’ve described above.
The ‘movieClipName’ can be a movie clip on the timeline, or as in this case can be _root, the main timeline.
you can see that we can dynamicaly control the object by using _root (where the item is attached) and then square brackets with the variable name inside. This is how a dot syntax string is writen when the name of the object is dynamic.

As an after thought… this might be helpful. If your pics were all of differing widths, you could use the following script to place each pic in the center of the movie, whenever it is requested with the button.


on (press) {
  xCenter=200;
  yCenter=200;
  picName = namebox.toLowerCase();
  for (var i = 0; i < names.length; ++i) {
  if (picName == names*) {  
    _root.attachMovie(picName,picName,300);
    _root[picName]._x=xCenter-_root[picName].width/2;
    _root[picName]._y=yCenter-_root[picName].height/2;
  }
}

in this case we’re assuming that the movie is 400 by 400. If the movie is a different size just change the value of the xCenter and yCenter variables.

The above represents how truely dynamic a/s can be, and how useful that is. Without knowing more than the names that we’ve give the objects, we can create and delete, move, change alpha, or colors of any object we like in the library at run time.

Here is a link that shows all the methods you can use on an array

http://www.macromedia.com/support/flash/action_scripts/objects/array_object.html

Hope it’s been helpful. If you have any questions, feel free to ask.

Thanks for the explanation. I think I learned quite a few new things. But unfortunately I did not get the array to work. ( must be something very stupid… )

What I would like:

“I want to have a button and an input text field. And if you fill in a name in the input text field and hit the button; the picture of that person pops up. Preferably in the same frame”

This is what I did:
I started a new movie. i imported 3 pictures from which I created 3 MC’s with names bobMC, francaMC and patrickMC.

I did the linkage in the library; right mouseclick on the MC, export this file and with name franca, bob and patrick for the three MC’s.

I created a button and gave it this code:
[COLOR=blue]
on (press) {
xCenter = 200;
yCenter = 200;
picName = namebox.toLowerCase();
for (var i = 0; i<names.length; ++i) {
if (picName == names*) {
_root.attachMovie(picName, picName, 300 );
_root[picName]._x = xCenter-_root[picName].width/2;
_root[picName]._y = yCenter-_root[picName].height/2;
}
}
}
[/COLOR]

Also I created an input text field (variable: namebox) and ticked border/Bg

O and the first ( and only frame ) has this action attached:

[COLOR=blue]
names = new Array(bob, franca, patrick);
[/COLOR]

Not sure what I forgot.

Could you please help?

thanks

check out your for (blabla) statment with the one from the FlashKit guy. It’s very different… Yours can’t loop 'cause there’s no i++ thingy. Plus, you shouldn’t put the if statement inside that part but inside the loop. The for statement is just to create your loop…

for(init; condition; next) {
statement(s);
}

Parameters

init An expression to evaluate before beginning the looping sequence, typically an assignment expression. A var statement is also permitted for this parameter.

condition An expression that evaluates to true or false. The condition is evaluated before each loop iteration; the loop exits when the condition evaluates to false.

next An expression to evaluate after each loop iteration; usually an assignment expression using the ++ (increment) or – (decrement) operators.

statement(s) An instruction or instructions to execute within the body of the loop.

Description

Action; a loop construct that evaluates the init (initialize) expression once, and then begins a looping sequence by which, as long as the condition evaluates to true, statement is executed and the next expression is evaluated.

Some properties cannot be enumerated by the for or for…in actions. For example, the built-in methods of the Array object (Array.sort and Array.reverse) are not included in the enumeration of an Array object, and movie clip properties, such as _x and _y, are not enumerated.

Example

The following example uses for to add the elements in an array:

for(i=0; i<10; i++) {
array * = (i + 5)10;
trace(array
);
}

The following results are displayed in the Output window:

50
60
70
80
90
100
110
120
130
140

The following is an example of using for to perform the same action repeatedly. In the code below, the for loop adds the numbers from 1 to 100:

var sum = 0;
for (var i=1; i<=100; i++) {
sum = sum + i;
}