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

flash actionscript : Weird AS Problem


Stressed_Simon
5/24/2005 12:00:00 AM
OK, I am building an AS driven menu system. I am using the following code:-

// stop on current frame
stop();

// set default starting places
var buttonX:Number = 30;
var clientMenuY:Number = 200;
var menuStep:Number = 23;

// create an array with the menu information
var menuArray:Array = ["architekButton",
"kinskiButton",
"creativeButton",
"nubButton",
"luellaButton",
"nylonButton",
"pachiraButton",
"edenButton",
"chainButton",
"harrowButton",
"gingerButton",
"breedButton",
"zandermanButton",
"plasticButton",
"atomicButton",
"hennesButton",
"geButton",
"sainsburyButton"];

// if logo button is pressed then load menu
logo_btn.onRelease = function() {
resetMenu();
}

/*
This function loads the main menu and adds
all the functionality for the page
*/
function loadMenu():Void {
// disable invis button
logo_btn._visible = false;
// load menu
_root.attachMovie("clientsButton", "clients_btn", 1);
_root.attachMovie("contactButton", "contact_btn", 2);
// set properties
clients_btn._x = buttonX;
clients_btn._y = 80;
contact_btn._x = buttonX;
contact_btn._y = 102;

// if they click on the contact button
contact_btn.invis_btn.onRelease = function() {
// reset menu
resetMenu();
// load email button
loadEmailButton();
}

// if they click on the clients button then run animation to output menu
clients_btn.invis_btn.onRelease = function() {
// reset menu
resetMenu();
// load client menu
clientMenu();
}
}

/*
This function loads the email button
*/
function loadEmailButton():Void {
// load email link
_root.attachMovie("emailButton", "email_btn", 3);
// set properties
email_btn._x = buttonX;
email_btn._y = 137;

// if they click on the info button then open email
email_btn.invis_btn.onRelease = function() {
sendEmail();
}
}

/*
this function sends the email
*/
function sendEmail():Void {
// send email
getURL("mailto:info@leonardcommunications.co.uk");
}

/*
This function resets the menu
*/
function resetMenu():Void {
// delete menu items
removeMovieClip(_root.clients_btn);
removeMovieClip(_root.contact_btn);
removeMovieClip(_root.email_btn);
// load menu again
loadMenu();
}

/*
This outputs the client menu to the page
*/
function clientMenu():Void {
// loop through and create menu
for (i = 0; i < menuArray.length; i++) {
// add menu items
clipName = "menuItem" + i;
zIndex = i * 10;
_root.attachMovie(menuArray, clipName, zIndex);
["menuItem" + i]_x = 30;
}
}

Now when the clientMenu() is called it moves all the elements on the screen to
the right. It seems that ["menuItem" + i]_x = 30; is the offending bit of code
but I cannot see how. To see what I am talking about you can go to
http://www.simonbaynes.com/FlashError/ then click on the word Leonard, followed
by the word clients.

Any advice would be greatly appreciated.
LuigiL
5/24/2005 12:00:00 AM
["menuItem" + i]_x = 30;
With this last line you are setting all menu items to a _x of 30. Instead you
should space out the menu items by doing something like this:

function clientMenu():Void {
var spacing:Number = 30; // if this is enough for you
// loop through and create menu
for (i = 0; i < menuArray.length; i++) {
// add menu items
clipName = "menuItem" + i;
var xPos:Number = i * spacing;
zIndex = i * 10;
_root.attachMovie(menuArray, clipName, zIndex);
["menuItem" + i]_x = xPos;
}
}
Stressed_Simon
5/24/2005 12:00:00 AM
Thanks, I am aware of that. I am just workign my way through it bit by bit. I
can see that all the movieclips are being output to the page. It is just that
the previous menu which is not mentioned in the clientMenu() function moves as
well.

I don't see how the new movieClips can effect the old movieClips unless they
have the same z-index. I am really stumped on this one.

Please help!
LuigiL
5/24/2005 12:00:00 AM
LuigiL
5/24/2005 12:00:00 AM
Stressed_Simon
5/24/2005 12:00:00 AM
Stressed_Simon
5/24/2005 12:00:00 AM
OK, you can download it here!

http://www.simonbaynes.com/index.fla

LuigiL
5/24/2005 12:00:00 AM
Well, as I stated earlier today - God is in the details.
I didn't notice it on this screen, but in Flash it was clear right away. The
code lacked a dot!
["menuItem" + i]_x = 30; should be ["menuItem" + i]._x = 30;

So, here is an updated function: (added the spacing too)

function clientMenu():Void {
var spacing:Number = 50;
// loop through and create menu
for (i = 0; i < menuArray.length; i++) {
// add menu items
clipName = "menuItem" + i;
var xPos:Number = i * spacing;
zIndex = i * 10;
_root.attachMovie(menuArray, clipName, zIndex);
this[clipName]._x = xPos;
}
}
Stressed_Simon
5/24/2005 12:00:00 AM
Cheers buddy. I was going cracked about that. I really appreciate you taking time out at home to have a look!

LuigiL
5/24/2005 12:00:00 AM
AddThis Social Bookmark Button