[F8] Fun With Assigning Functions In Loops
Hi everyone,
let's see if I can explain this properly.... I'm building a timeline in Flash 8 using Actionscript 2.0. The timeline is split into 28 years, each about 1000 pixles wide (I know, I know... but the client insisted). I have a scrollbar at the bottom of the screen which can be dragged to scroll the timeline left and right. The timeline works on an xml file - one node for the timeline itself, which then contains 28 nodes (one for each year), which in turn can contain as many images (along with their captions) as the client needs. The Flash file parses the XML, and duplicates 'container' movieclips, placing them in each year's clip. It then loads in the images from the relevant file path, depending what's written in the xml.
This all works fine.
What I need each individual image, when clicked, to display its caption (also pulled in from the same xml node) in a container that's in the image's _parent movieclip (I'd be happy for it to just display the correct thing in a trace() action tbh - I can work the rest of the stuff out).
The way I'm trying to do it is to add an onMouseUp() action to the container after the image has been loaded (well, it's the container's _parent actually, but the same applies). The problem is that because the xml is pulled into Flash as a multidimensional array (Year > Image > Properties) I need to use nested loops when I'm retrieving the values. The incremental values are i and j (for example, if i is 0 and j is 0 then myArray[i][j][0] would return the filename of the first image in the first year).
But whenever I try and pass i and j to the function for that particular image container, I always end up with the last value from the loop (i.e. 28 and 2) rather than what should be displayed (i.e. 0,0).
This has happened before, and I can't remember how I got around it - I've tried assigning i and j to variables held by the movieclip instance, but I must not be doing it right as it's just. Not. Working.
Anyone got any ideas how I can give each instance its own variables with the current versions of i and j in the loop?
Thanks!
FlashKit > Flash Help > Flash ActionScript
Posted on: 04-26-2007, 09:59 AM
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Assigning Functions On The Fly
I'm creating a menu from mcs loaded from the library, I want to assign an onRelease function to each of the menu items which i'm storing in a temporary array when created... basically this: -
Code:
//loop and attach menu items for the number of items in layLength
for(var i=0, j=0; i < layLength; i++){
var temp = container.attachMovie("menuItemMC", "menuItemMCxxx" + i, i);
temp.img = as2obj.boat.img[i].attributes.file; //some xml data
tempArray.push(temp);
y++;
}
//Assign onRelease events to each menu item loaded
for(g in tempArray){
tempArray[g].onRelease = function() {
trace("I am associated with " + tempArray[g].img);
}
}
the problem is the functions assigned to the created movieclips will just default to the current value of 'g' I.e. when trace is triggered it will trace the tempArray value of whatever g is currently, I want it to remember the value at the time the function is assigned to the movie.
Sorry for the poor explanation, my english is bad... I'm british :S
Thanks
Dan
[F8] Assigning Two Functions To A Button
Hi there,
I would like a single button press two run two functions together. I have a movieclip on the stage, instance name ny that has two frames, the scond frame being a different colour to the first frame.
When I press the button I want it to goto frame 2 and perform a trace statement...
var score:Number = 0;
function addScore(new_score:Number) {
score += new_score;
}
var ny:MovieClip;
ny.onPress =addny+democrat;
function democrat():Void {
this.gotoAndStop(2);
}
function addny():Void {
addScore(40);
trace("Total votes are "+score);
ny.stop();
}
What I'm hoping for is the mc to change colour and output the statement "Total votes are 40". The first function is so I can add further movie clips and assign them scores to add as well so I can have a running total.
Also does anyone know how I can disable the movieclip after it's been pressed to avoid the user from adding the total repeatedly for the mc?
Any help is greatly appreciated
Best
Steve
Assigning Functions Via A Loop
I am working on a page where three differnt buttons effect one movie clips properties, the one movie clip will go to its rollover state when any of the buttons are on rollover, hmm, that sounds about right]
i have assigned functions within a loop and the rollovers, rollouts and instance in the loop have been defined for all but the movieClips dont go to the right place....
the code i have used is this....
Code:
for (var i:Number = 1; i < 4; i++) {
// attaching ALL rollovers to effect the interactive button
this["invis"+i+"_btn"].onRollOver = this["chart"+i+"_btn"].onRollOver = this["inter"+i+"_mc"].onRollOver = function() {
buttonName = this._name;
buttonNumber = Number(buttonName.substr(5, 1));
trace ("rollover");
trace (buttonNumber);
this["inter"+buttonNumber+"_mc"].gotoAndStop("on");
}
this["invis"+i+"_btn"].onRollOut = this["chart"+i+"_btn"].onRollOut = this["inter"+i+"_mc"].onRollOut = function() {
trace ("rollout");
buttonName = this._name;
buttonNumber = Number(buttonName.substr(5, 1));
this["inter"+buttonNumber+"_mc"].gotoAndStop("normal");
}
}
as i have said...all the traces show up correct but not the gotoAndStop on the movieClip (inter+i+_mc_)'s
any ideas??
Nested MC's And Assigning Functions.
Right... I have a few rows of movie clips making a menu. Because I will be adding to the number of MC's I use a variable called "menuitems" and run a FOR loops to assign all of these MC's onrollOver, onrollOut and onRelease functions. I'm doing this to save having to assign these functions to them individualy (as they are the same functions). Its works fine, but when the MC is released it is told to jump to frame 2, where there will be nested mc's that need to be able to clicked and rolled over. Because the parent movie has event functions assigned to it they seem to overide them. Is there a way around this? I tried placing a transparent MC on the 1st frame of the movie and trying to assign the onRelease function to the "hotspot" but it doesnt work. Here is my code:
Code:
function buildthumbs () {
for (i=1; i<= menuitems; i++) {
this["a"+i].onRollOver = function () {
x+=2;
this.swapDepths (x);
new Tween (this, "_xscale", Back.easeOut, this._xscale, 120, 12, false);
new Tween (this, "_yscale", Back.easeOut, this._yscale, 120, 12, false);
new Tween (this, "_alpha", None.easeNone, this._alpha, 100, 8, false);
}
this["a"+i].onRollOut = function () {
this.gotoAndStop (1);
new Tween (this, "_xscale", Back.easeOut, this._xscale, 100, 12, false);
new Tween (this, "_yscale", Back.easeOut, this._yscale, 100, 12, false);
new Tween (this, "_alpha", None.easeNone, this._alpha, 70, 12, false);
}
this["a"+i].hotspot_mc.onRelease = function () {
new Tween (this, "_xscale", Regular.easeOut, this._xscale, 330, 12, false);
new Tween (this, "_yscale", Regular.easeOut, this._yscale, 330, 12, false);
a = new Tween (nothing, "_x", None.easeNone, 0, 100, 12, false);
a.onMotionFinished = function () {
b = new Tween (this.logo, "_alpha", Regular.easeOut, 100, 0, 12, false);
b.onMotionFinished = function () {
_parent.gotoAndPlay (2);
}
}
}
}
}
I'm not sure if the problem lies in trying to call
Code:
this["a"+i].hotspot_mc.onRelease = function()
because when I drop the "hotspot_mc" bit it works fine, but the nested mc's dont work.
Please help!!!
Assigning Functions Via A Loop
I am working on a page where three differnt buttons effect one movie clips properties, the one movie clip will go to its rollover state when any of the buttons are on rollover, hmm, that sounds about right]
i have assigned functions within a loop and the rollovers, rollouts and instance in the loop have been defined for all but the movieClips dont go to the right place....
the code i have used is this....
Code:
for (var i:Number = 1; i < 4; i++) {
// attaching ALL rollovers to effect the interactive button
this["invis"+i+"_btn"].onRollOver = this["chart"+i+"_btn"].onRollOver = this["inter"+i+"_mc"].onRollOver = function() {
buttonName = this._name;
buttonNumber = Number(buttonName.substr(5, 1));
trace ("rollover");
trace (buttonNumber);
this["inter"+buttonNumber+"_mc"].gotoAndStop("on");
}
this["invis"+i+"_btn"].onRollOut = this["chart"+i+"_btn"].onRollOut = this["inter"+i+"_mc"].onRollOut = function() {
trace ("rollout");
buttonName = this._name;
buttonNumber = Number(buttonName.substr(5, 1));
this["inter"+buttonNumber+"_mc"].gotoAndStop("normal");
}
}
as i have said...all the traces show up correct but not the gotoAndStop on the movieClip (inter+i+_mc_)'s
any ideas??
Assigning Button Functions
having a little trouble with this one. i'm creating a series of buttons that all need to run the same function of click but need to pass it different paramaters.
i've been tryingg:
myClip.myButton.onRelease = myFunction;
this works, but as soon as i try myFunction(param); it gets all messed up. if anyone knows how to do this so that i don't have to hard code all the buttons i'd love to hear it.
cheers
Assigning Functions To Movieclips In An Array
I'm creating a menu from mcs loaded from the library, I want to assign an onRelease function to each of the menu items which i'm storing in a temporary array when created... basically this: -
Code:
//loop and attach menu items for the number of items in layLength
for(var i=0, j=0; i < layLength; i++){
var temp = container.attachMovie("menuItemMC", "menuItemMCxxx" + i, i);
temp.img = as2obj.boat.img[i].attributes.file; //some xml data
tempArray.push(temp);
y++;
}
//Assign onRelease events to each menu item loaded
for(g in tempArray){
tempArray[g].onRelease = function() {
trace("I am associated with " + tempArray[g].img);
}
}
the problem is the functions assigned to the created movieclips will just default to the current value of 'g' I.e. when trace is triggered it will trace the tempArray value of whatever g is currently, I want it to remember the value at the time the function is assigned to the movie.
Sorry for the poor explanation, my english is bad... I'm british :S
Thanks
Dan
Assigning Multiple Functions To A Mousebutton
Basicly, I have a button which forms part of a mc called window1. When the button is released, it must move the "window1" mc (and itself, as it is attached to window1) from _x=0 to _x=300. When pressed again, it must return window1 from _x=300 to _x=0, thereby emulating a expand/contract effect.
Any takers?
Thanks,
-fabman
Automatically Assigning Functions To Buttons
Hi -
I've written a script which reads an xml file, automatically prints the correct question, and creates a button for each possible answer. I can vary #answers (i.e. #buttons) just by editing xml.
The only problem is, I can't work out how to automatically generate the right function for each button:
_root["button"+j].onPress = function(button) {
trace("button = " + j);
};
Doesn't work - as it always produces button = 2.
So I've tried setting up an array, writing the possible answers there as I generate the buttons and then reading it on button press:
_root["button"+j].onPress = function(button) {
trace(buttonValues[j]);
};
But same problem. Basically, I need some way of working out when I press the button which button has been pressed, so that I can look up the correct value. Is there a neat way of doing this?
Thanks!
Assigning New Instance Names AND Using Them In Functions
Hey, I want every new instance created to have a unique name, then I want to be able to interact with that unique instance to drag it, color it, size it, etc. Everything I've tried has failed--do you guys have ideas? In other words, how can I change "newCircle" below to, say, "newCircle1" and then use "newCircle1" to call the function?
Code:
circle_btn.onRelease = function():Void{
attachMovie("mcCircle","newCircle",getNextHighestDepth());
trace( newCircle);
newCircle.onPress = myOnPress;
newCircle.onRelease = myOnRelease;
};
//====================== DRAG AND DROP FUNCTIONALITY =========================
myOnPress = function():Void {
//trace( this );
this.startDrag(false);
};
myOnRelease = function():Void {
this.stopDrag();
};
Assigning Functions To Buttons In An Array...
Hi people, this is my first post here, so bare with me....
I haven't coded anything of significance the last few years, but now i'm trying to get into ActionScript 3, and... well, I'm having some problems with a slideshow I'm trying to make.
It loads the slide-data (image, heading, text and a URL for a Read More button) from a xml-file. The problem is that when I'm trying to create the eventlistener for the Read More button, all buttons get the value of the last slide.
Here's the code
Code:
for (i=0; i<max; i++) {
slide = new slideMc();
slide.x = -3000;
slide.y = 0;
slide.alpha = 0;
slide.ready = false;
imgLoader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
imgLoader.load(new URLRequest(slideXML.article[i].bigArticleImg));
slide.slideImg.addChild(imgLoader);
slide.heading.text = slideXML.article[i].heading;
slide.ingress.text = slideXML.article[i].ingress;
addChildAt(slide, i);
slideList.push(slide);
slideTable[imgLoader.contentLoaderInfo] = slide;
moreURL = slideXML.article[i].readMoreLink;
slideList[i].moreBtn.buttonMode = true;
slideList[i].moreBtn.addEventListener(MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
var request:URLRequest = new URLRequest(moreURL);
try {
navigateToURL(request, '_self');
} catch (err:Error) {
trace("Error occurred!");
}
}
);
If anyone's interrested, the files for the project can be downloaded here
If i get the help to finish this, i'll make the finished slideshow available as a free download if there's interrest, so others can learn from it.
Thanks!
Assigning Functions To Movieclips In Array
I'm creating a menu from mcs loaded from the library, I want to assign an onRelease function to each of the menu items which i'm storing in a temporary array when created... basically this: -
Code:
//loop and attach menu items for the number of items in layLength
for(var i=0, j=0; i < layLength; i++){
var temp = container.attachMovie("menuItemMC", "menuItemMCxxx" + i, i);
temp.img = as2obj.boat.img[i].attributes.file; //some xml data
tempArray.push(temp);
y++;
}
//Assign onRelease events to each menu item loaded
for(g in tempArray){
tempArray[g].onRelease = function() {
trace("I am associated with " + tempArray[g].img);
}
}
the problem is the functions assigned to the created movieclips will just default to the current value of 'g' I.e. when trace is triggered it will trace the tempArray value of whatever g is currently, I want it to remember the value at the time the function is assigned to the movie.
Sorry for the poor explanation, my english is bad... I'm british :S
Thanks
Dan
Assigning OnPress Functions In A Loop...
is that possible? i'm a complete beginner when it comes to flash and actionscript... so if it sounds like i dont know what i'm talking about... it's because i don't!
let me describe my situation...
I have about 20 boxes... and on each box i want to assign the onPress to trace the value corresponding to an Array at element "i" in the console...
instead of doing:
Code:
box1.onPress = function() {
trace("the value is: "+hypotheticalArray[1]);
}
from 1 to 20... i'd like to loop these assignments.
like this:
Code:
function myTrace(i) {
trace("the value is: "+hypotheticalArray[i]);
}
//assume that there are as many textboxes as there are elements in the array.
for (var i=0; i<hypotheticalArray.length; i++) {
set("box"+i+".onPress",myTrace(i));
}
ANY suggestions would be greatly appreciated!
Assigning Multiple Functions Through Loop
Hello one and all.
I have many buttons on my stage that all need to have a different function assigned to them. Each of these buttons lives within a movieClip. Here is the code i am using:
Code:
for (i = 1; i<88; i++){
myClip["btn"+i].onRelease = function (){
_root.callConst([i]);
}
}
The idea is that there is 88 buttons all called btn1-btn88. And i want to assign code to each of them that results in an onRelease function that calls an external function with a variable that corresponds to its button number.
What is happening now is that code is being corresponed to each button (yay!), but the variable that is being passed by all of them is the final value of "i" (or "88" in this particular case).
What am i missing?
Thanks in advance,
newbie
Dynamically Assigning Functions To Mcs OnPress
Hi all,
I am trying to work out the logistics/structure of writing general 'action' functions (ie, move, scale, etc) and then assigning those functions to different mcs depending on what is currently 'in focus'.
Here is the code that illustrates my current thinking. It is general, and not specific! In kirupa style, I will highlight each line to express my thinking, so that you can better provide me with feedback.
Code:
function fadeOut(mc) {
if ((mc).width >= 50) {
--(mc)._width;
--(mc)._height;
} else {
//DO NOTHING
}
}
function fadeIn() {
}
function zoomIn() {
}
function zoomOut() {
}
onEnterFrame = function() {
if (homeScreen == true) {
btnDown.onPress = fadeOut(mc_home) = fadeIn(mc_contacts);
} else if (contactsScreen == true) {
btnDown.onPress = zoomIn(mc_people) = zoomOut(mc_numbers);
}
}
This is an example of an action function that I want to work with mcs.
Code:
function fadeOut(mc) {
if ((mc).width >= 50) {
--(mc)._width;
--(mc)._height;
} else {
//DO NOTHING
}
}
Is this how I get it to receive an mc?
Code:
function fadeOut(mc) {
And how do I reference the mc afterwards, like this?
Code:
function fadeOut(mc) {
if ((mc).width >= 50) {
More action functions, to be filled in later.
Code:
function fadeIn() {
}
function zoomIn() {
}
function zoomOut() {
}
Here is how I am checking for button presses. Seems pretty straight forward.
Code:
onEnterFrame = function() {
Here is how I am checking to see what is 'in focus'.
Code:
if (homeScreen == true) {
Here is where I am also confused, how do I get an mc to work with the action functions? Can I have two functions working on one mc at the same time? Is this even the right 'high level' structure that I should be using for this task, let alone correct syntax?
Code:
btnDown.onPress = fadeOut(mc_home) = fadeIn(mc_contacts);
This is the same as the above code, just working with different functions and mcs.
Code:
} else if (contactsScreen == true) {
btnDown.onPress = zoomIn(mc_people) = zoomOut(mc_numbers);
}
}
I will potentially have close to 50 mcs that I will have to work with, and maybe 20 'action' functions. So I wanted to make sure that I have the basics down, then I can just rinse and repeat. Anything else I am not considering?
Thanks to anyone who can lend some clarity,
Nick
[AS 2.0] Assigning Functions To Buttons = Not Working
hey crew:
here's the essence of what i'm doing:
Code:
var red:Number = 0xff1100;
var blue:Number = 0x0011FF;
function buttonRollOver(clip)
{
old_button = red; // just a pseudo-code for space reasons
clip = blue;
old_button = clip;
do some neato stuff;
};
function buttonSelect
{
old_button = red; // just a pseudo-code for space reasons
clip = blue;
old_button = clip;
do some special stuff;
}
for (i=0; i<totalButtons; i++)
{
_level0.attachMovie("libClip", "button_" + i + "_mc", buttonDepth);
button = _level0[button_[i]_mc;
button.onRollOver = buttonRollover(button);
button.onRelease = buttonSelected(button);
};
two wildly crazy things happen:
1) button clips don't work
2) ALL button functions are activated at once
...WTF??! what am i doing wrong here?
Assigning Functions To Buttons In An Array...
Hi people, this is my first post here, so bare with me....
I haven't coded anything of significance the last few years, but now i'm trying to get into ActionScript 3, and... well, I'm having some problems with a slideshow I'm trying to make.
It loads the slide-data (image, heading, text and a URL for a Read More button) from a xml-file. The problem is that when I'm trying to create the eventlistener for the Read More button, all buttons get the value of the last slide.
Here's the code
Code:
for (i=0; i<max; i++) {
slide = new slideMc();
slide.x = -3000;
slide.y = 0;
slide.alpha = 0;
slide.ready = false;
imgLoader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
imgLoader.load(new URLRequest(slideXML.article[i].bigArticleImg));
slide.slideImg.addChild(imgLoader);
slide.heading.text = slideXML.article[i].heading;
slide.ingress.text = slideXML.article[i].ingress;
addChildAt(slide, i);
slideList.push(slide);
slideTable[imgLoader.contentLoaderInfo] = slide;
moreURL = slideXML.article[i].readMoreLink;
slideList[i].moreBtn.buttonMode = true;
slideList[i].moreBtn.addEventListener(MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
var request:URLRequest = new URLRequest(moreURL);
try {
navigateToURL(request, '_self');
} catch (err:Error) {
trace("Error occurred!");
}
}
);
If anyone's interrested, the files for the project can be downloaded here
If i get the help to finish this, i'll make the finished slideshow available as a free download if there's interrest, so others can learn from it.
Thanks!
Assigning Functions To Movieclips In Array
I'm creating a menu from mcs loaded from the library, I want to assign an onRelease function to each of the menu items which i'm storing in a temporary array when created... basically this: -
Code:
//loop and attach menu items for the number of items in layLength
for(var i=0, j=0; i < layLength; i++){
var temp = container.attachMovie("menuItemMC", "menuItemMCxxx" + i, i);
temp.img = as2obj.boat.img[i].attributes.file; //some xml data
tempArray.push(temp);
y++;
}
//Assign onRelease events to each menu item loaded
for(g in tempArray){
tempArray[g].onRelease = function() {
trace("I am associated with " + tempArray[g].img);
}
}
the problem is the functions assigned to the created movieclips will just default to the current value of 'g' I.e. when trace is triggered it will trace the tempArray value of whatever g is currently, I want it to remember the value at the time the function is assigned to the movie.
Sorry for the poor explanation, my english is bad... I'm british :S
Thanks
Dan
Question: Assigning Functions To Emptymovieclips?
I'm pretty new to ultrashock, but it rocks.
I'm kind of a noob in flash as well, perhaps someone will be able to help me out with this one...
So I'm working on this interface(in FlashMX not FlashMX2004)and i'm loading actionscript with the
//---
#include "superScriptFileOfFunk.as"
//---
command. I'm working toward having all of my code load through the use of .as files. Ok. *shrug* no biggie.
So far I createEmptyMovieClips to loadMovie a bunch of my graphics in. Again *shrug* no big thing.
The problem comes in with a specific case: when I try to load functions onto an empty mc that I've created on the fly; the function I assign doesn't work. I've looked at the code function
Specifically I'm looking to assign an onData function to a emptyMovieClip. I have tested the onData function that I'm trying to assign and when
This is what i'm trying: loaded from an .as file into the _root timeline
//---
createEmptyMovieClip('dholder',20); //where 20 is some unique level number
dholder.onData = function()
{
_parent.fillContent();
}
//---
when i use
//---
onData = function()
{
_parent.fillContent();
}
//---
directly on a real empty clip i stored in my library it works just perfectly.
The biggest thing they tell newbies is to RTFM. So I took a look to see what I could find and I found something that looks like this:
//---
sliderstrip.onPress = function ()
{
trace ("onPress called");
} //sliderstrip is an empty movie clip that i've created on the fly that loads in a strip graphic.
//---
I modified the reference code so that it should work on the sliderstrip clip i've created, but again it doesn't work; when you click there is no trace that occurs.
I'm also working on assigning buttonlike states to mc's and I can't seem to get it working. It seems to be the same question as above: assigning functions from an .as to mcs that have been loaded into script created mcs.
Right now 3/4 of my files are pulling from .as and i'd like to make them all pull.
This is my first work using loading script from .as files.
I'm sure there's something little(or major) I'm overlooking, perhaps someone more experienced would have an idea?
Thanks,
n8tive
If i've left something out let me know and i'll hook it up.
Assigning Custom Functions To Dynamic Objects
Hey All,
I have a problem here, i have came up with a simpler example of what im trying to achive.
Ok building a navigation from a XML file. The XML file is loaded no bother, and all stored in arrays, thats all fine. However, when i create the parts (boxes) of the navigation, i would like to, say, move them 10 pixels right every enterframe.
so to build the menu again is fine:
function (random){
for (i=0; i<installmentsMax; i++){
_root.attachmovie .... blah
}
}
however after they are created, i would like to apply the movement by way of a function, normally:
mc.onEnterFrame = function (move){
_x += 10;
}
however how would one apply that to each box thats created? When they attached the names will be "holder0" "holder1" .... so how to i cycle through them all and attach the move function, a more easier version of the question i guess is, would you assign all functions as the menu is getting built? If this is the case, would the "holder"+i.onEnterFrame if placed in the "random" function be cancelled if you delete the "random" function above?
I just read this and it confused me so i dont think anyone else will have a chance to understand it but if you do please help :P
Ok i found a better way to explain, if you have
function (test) {
mc.onEnterFrame = function (){
trace("hi");
}
}
if i delete the function called test using "delete" will that cancel the trace output?
Thanks
Assigning Functions To Buttons That Are Not Apparent In First Frame
Hi there,
I have an annoying problem, I have a separate movieclip in a movie that contains three frames, on each frame is a different button, and different content, so not all my buttons are on the first frame of the movieclip. I want to keep all my actionscript on the main timeline, but I cannot assign functions to the buttons in the later frames because they are not all there on the first frame of the movieclip.
Does anyone know of a way to assign a function to a button that will appear later in the movieclips timeline?
Thanks,
Bob
info@bobcooper.org.uk
www.bobcooper.org.uk
Problems Assigning Individual Functions To Objects
Hi,
I have the following code, which is part of a larger script used to place a copy of a clip on stage & move it using set speed parameters. It also creates an object to store information used by the clip & other functions
In my mind every time I press the fire button (which activates the code) a new clip will be generated with a unique name using the variable “z” as in _root["shell"+z] the same will go for the object created “_root["fireShell"+z] = {};”
The code works fine until I press to fire another shell – then the old one stops & the new one moves at twice the speed of the previous shell.
I can't understand why this is happening. In the code each clip is designated it's own onEnterFrame event so should be happily doing it's own thing when the button is pressed to fire another shell?
As a side issue a previous reply to a related post recommended using the this._parent naming convention when referring to the object (parent) in which a clip was contained. Is it possible to refer to a clips parent as an object as the code “this._parent” just gave me the level that the clip was contained within & not the parent object?
code: _root.fire_mc.onPress = function() {
z++;
var speed = 2;
x1 = speed*Math.cos(ang);
y1 = speed*Math.sin(ang);
grav = .01;
_root.attachMovie("shell", "shell"+z, z);
_root["shell"+z]._x = Math.floor(_root.gun1_mc._x+_root.gun1_mc.barrel_m c._width*Math.cos(ang));
_root["shell"+z]._y = Math.floor(_root.gun1_mc._y+_root.gun1_mc.barrel_m c._width*Math.sin(ang));
_root["fireShell"+z] = {};
_root["fireShell"+z].x = _root["shell"+z]._x;
_root["fireShell"+z].y = _root["shell"+z]._y;
_root["fireShell"+z].clip = _root["shell"+z];
_root["fireShell"+z].xmov = speed*x1;
_root["fireShell"+z].ymov = speed*y1;
_root.shell_array.push(_root["fireShell"+z]);
_root["fireShell"+z].clip.onEnterFrame = function() {
yAmount = _root["fireShell"+z].ymov += grav;
_root["fireShell"+z].x += _root["fireShell"+z].xmov;
_root["fireShell"+z].y += yAmount;
_root["fireShell"+z].clip._x = _root["fireShell"+z].x;
_root["fireShell"+z].clip._y = _root["fireShell"+z].y;
};
};
Thanks for any help.
Assigning Same Functions To Multiple Movie Clips.
I have multiple movie clips on my timeline(not dynamically added.)
I would like to assign a same Mouse Over function to all of them.
something like below.
Code:
package{
import flash.display.MovieClip;
import flash.events.Event;
public class My_MC extends MovieClip {
public function My_MC() {
this.addEventListener(MouseEvent.MOUSE_OVER, mouseOn);
}
private function mouseOn(e:MouseEvent):void{
trace("OVER");
}
}
}
There are too many movie clips to give each an instance name and use for loop.
Also, their positions are visually too specific to dynamically add them using addChild.
How can I do this?
Code: Assigning Functions To Multiple Links
Hello,
For the past half-year I've been coding like this:
ActionScript Code:
_root.menu_mc.link1.onRollOver = function () {
_root.menu_mc.link1.text.htmlText = "<FONT COLOR='#000000'>" + "Cool" + "</FONT>";
_root.menu_mc.link1.text.thickness = 100;
_root.menu_mc.link1.text.sharpness = 30;
}
_root.menu_mc.link1.onRollOut = function () {
_root.menu_mc.link1.text.htmlText = "<FONT COLOR='#FFFFFF'>" + "Cool" + "</FONT>";
_root.menu_mc.link1.text.thickness = -180;
_root.menu_mc.link1.text.sharpness = 181;
}
_root.menu_mc.link2.onRollOver = function () {
_root.menu_mc.link2.text.htmlText = "<FONT COLOR='#000000'>" + "Awesome" + "</FONT>";
_root.menu_mc.link2.text.thickness = 100;
_root.menu_mc.link2.text.sharpness = 30;
}
_root.menu_mc.link2.onRollOut = function () {
_root.menu_mc.link2.text.htmlText = "<FONT COLOR='#FFFFFF'>" + "Awesome" + "</FONT>";
_root.menu_mc.link2.text.thickness = -180;
_root.menu_mc.link2.text.sharpness = 181;
}
_root.menu_mc.link3.onRollOver = function () {
_root.menu_mc.link3.text.htmlText = "<FONT COLOR='#000000'>" + "Sick" + "</FONT>";
_root.menu_mc.link3.text.thickness = 100;
_root.menu_mc.link3.text.sharpness = 30;
}
_root.menu_mc.link3.onRollOut = function () {
_root.menu_mc.link3.text.htmlText = "<FONT COLOR='#FFFFFF'>" + "Sick" + "</FONT>";
_root.menu_mc.link3.text.thickness = -180;
_root.menu_mc.link3.text.sharpness = 181;
}
I know and I've seen some more efficent code such as, making loops and assigning variables.
Can someone help me get started by making the above code more efficient? I've attached the source file.
Many thanks,
irok
Assigning Same Functions To Multiple Movie Clips.
I have multiple movie clips on my timeline(not dynamically added.)
I would like to assign a same Mouse Over function to all of them.
something like below.
Code:
package{
import flash.display.MovieClip;
import flash.events.Event;
public class My_MC extends MovieClip {
public function My_MC() {
this.addEventListener(MouseEvent.MOUSE_OVER, mouseOn);
}
private function mouseOn(e:MouseEvent):void{
trace("OVER");
}
}
}
There are too many movie clips to give each an instance name and use for loop.
Also, their positions are visually too specific to dynamically add them using addChild.
How can I do this?
Assigning Same Functions To Multiple Movie Clips.
I have multiple movie clips on my timeline(not dynamically added.)
I would like to assign a same Mouse Over function to all of them.
something like below.
Code:
package{
import flash.display.MovieClip;
import flash.events.Event;
public class My_MC extends MovieClip {
public function My_MC() {
this.addEventListener(MouseEvent.MOUSE_OVER, mouseOn);
}
private function mouseOn(e:MouseEvent):void{
trace("OVER");
}
}
}
There are too many movie clips to give each an instance name and use for loop.
Also, their positions are visually too specific to dynamically add them using addChild.
How can I do this?
Assigning Functions/methods To Dynamically Generated MovieClips
I tried to assign functions to a MC either using _root.movieClip.functionname = function name(){};
or
function name(){};
movieClip.myfunction = function();
problem is : none works.
Question is:
1. Do the duplicated MC's inherit the methods of their parent?
2. anyone know a solution ?
Thank you very much. God bless you and me.
Assigning Multiple Conditions On Movieclip Event Functions
i want to have two calls on a single movieclip event function. like you can on button funtions (like this)
Code:
on(release,releaseOutside)// note multiple calls (release and release outside)
{
// my actions
}
how would i get two calls on a function constructed like the below
Code:
myMovieClip.onRelease = function()
{
// my actions
}
do i seperate them by commas or "[" or ....
thanks!
Assigning Button Functions To Dynamically Created Buttons
Hi all,
Heres ther scenario - im building an xml navigation movie for our site. The xml is read in, and loops through xml creating the top level buttons (there's 7 in total). Within that loop is another loop which creates the subnavigation options.
Because the navigation is hoziontal and the button lengths vary greatly, the brief is to do everything on the fly - there's nothing other than the backgrounds and the fonts in the library.
So for example, the first xml node is 'About', the script makes the 'About' button, positions it on screen, then loops through & creates the sub nav buttons. All good so far. Once the sub nav is complete, I want to assign the top level button an action to show/hide the subnav (it calls a prototype):
//newClip is the top level button
newClip.onRollOver = function ()
{
subClip.moveThisY (22, 3);
};
newClip.onRollOut = function ()
{
subClip.moveThisY (-19,15);
};
This works to a point, but all the buttons show/hide the same subnavigation (the last one created) not their own. i.e. if the first button is 'About' and the last is 'Products', the 'About' button shows & hides 'Products' sub nav. any ideas where im going wrong? i'm guessing i need someway of evaluatig/assigning a value to the Rollover function.
Cheers for ya help peeps.
James
It Creates The Dynamic Movieclips, Problems Assigning Functions To Them
Hello, I have some script that creates movieclips with text based upon an array of data.
I am having problems trying to get these movieclips to have functions, I am, for now, just trying to get them to perform a simple trace.
any ideas?
ActionScript Code:
function onPhotosSearch(rs:ResultsSet)
{
_numResults = rs.photos.length;
var depth = 0;
var nextY = 10;
for (var i:Number=0; i<_numResults; i++) {
var thisPhoto:Photo = rs.photos[i];
var taggy:Array = thisPhoto.getTagsAsStrings();
_flashr.photosGetInfo(thisPhoto.id, thisPhoto.secret);
trace('Photo: ' + thisPhoto + ', tags:' + taggy);
_root.wow.text = taggy;
for (var nother = 0; nother < taggy.length; nother++) {
trace (taggy[nother]);
var tempMC:MovieClip = attachMovie ("box", "box" + count, depth ++, {_y : (nextY) , _x : (5)} );
nextY += tempMC._height + 2;
tempMC.name_txt.text = taggy[nother];
count ++;
tempMC.onRelease = function(){
doSearch2;
}
}}
}
function doSearch2():Void
{
trace("single");
}
Functions & Loops...
Hi,
Have been trying to write a global function to move a movie clip up or down and smoothly decelerating the _y value to reach a new position.
Have this code I have used before but not as a function (running as a 2 frame loop in another clip), and using a button to sends the new value of "newPos" to this code as the new end _y position for the menu clip.:
currPos = (_root.menu._y);
changePos = (newPos - currPos)/10;
_root.menu._y = (Number(currPos)+Number(changePos));
Next Frame:
prevFrame ();
play();
This works fine, but when I place it inside a function and send the value "newPos" it doesn't work as I don't know where to place the loop, inside the function or the timeline calling it?:
function moveClip (clipName, newPos){
currPos = (_clipName._y);
changePos = (newPos - currPos)/10;
_clipName._y = (Number(currPos)+Number(changePos));
}
....or maybe this code won't work as a function at all?
Any help gladly appreciated!
Thanks
Functions Loops
hi,
i will try to explain my problem as well as possible.
i have made my own class, for a project i am working on for work, in it have the following functions:
i have a function called addTile()
this attaches mc, or it loads external files, then pushes them into an array(myTiles)
which goes alittle something like this:
Code:
public function addTile(tilePath) {
tileDepth++;
if (tilePath.indexOf(".swf") != -1 || tilePath.indexOf(".jpg") != -1) {
var tempTile = holderMC.createEmptyMovieClip("tile"+tileDepth, tileDepth);
tempTile.loadMovie(tilePath);
} else {
var tempTile = holderMC.attachMovie(tilePath, "tile"+tileDepth, tileDepth);
}
tempTile._x = getTilesWidth();
myTiles.push(tempTile);
}
now the getTilesWidth() is a function that returnes all the widths of the "tiles" in one variable,
now the problem is, that when i load an external file, flash cant access the dimensions of the file before it has fully loaded,
so i am trying to make a loop to make sure that the file has loaded before i push it its width into the getTilesWidth() function.
i have tried the following but i get a infinate loop error
Code:
function getDimensions() {
if (tempTile._width == 0) {
getDimensions1();
} else if (tempTile._width != 0) {
var width=tempTile._width
}
return width
}
function getDimensions1() {
if (tempTile._width == 0) {
getDimensions();
} else if (tempTile._width != 0) {
var width=tempTile._width
}
return width
}
how can i do this without getting an infanite loop, or how can i do this another way?
i thought about using try..catch..finally, but not sure if you can use it for this example.
thanks,
zlatan
For Loops And Functions In As3
I'm trying to use a couple for loops to create buttons in AS3, but it's not right and I'm not sure how to fix it.
PHP Code:
var i:Number;
for (i=1; i<=11; i++){
this["arrow"+i].alpha = 0;
this["clientsBtn"+i].addEventListener(MouseEvent.ROLL_OVER, "client"+i+"_OVER");
this["clientsBtn"+i].addEventListener(MouseEvent.ROLL_OUT, "client"+i+"_OUT");
this["clientsBtn"+i].addEventListener(MouseEvent.CLICK, "client"+i+"_CLICK");
this["clientsBtn"+i].gotoAndStop(1);
this["clientsBtn"+i].alpha = 1;
}
var j:Number;
for (j=1; j<=11; j++){
function "client"+i+"_OVER"(evt:MouseEvent):void {
trace("over");
}
function "client"+i+"_OUT"(evt:MouseEvent):void {
trace("out");
}
function "client"+i+"_CLICK"(evt:MouseEvent):void {
trace("click");
}
}
For Loops And Functions In As3
I have a "for loop" that is placing a bunch of fish as well as instances of an mc that contains a button_mc. I made a "fishClick" function for the button, but I don't know how to get the button to know which fish it belongs to. I want to be able to have different things happen depending on which button you click. If I could just get it to spit out a corresponding number - even that would be great. I tried doing trace(this.name), but I just get root1.
PHP Code:
for (var i:int = 1; i<=fishArray.length; i++) {
//
var aFish:DisplayObject = new fishArray[i-1].creature();
fishScroller.addChild(aFish);
//
var fishName = new fishName_mc();
fishScroller.addChild(fishName);
//
fishName.name = "fishName"+i;
//
aFish.name = "fish"+i;
//
fishName.infoBtn.addEventListener(MouseEvent.CLICK, fishClick);
}
Loops And Functions
I've attempted to add functions to some movieclips created with a loop (and sadly failed). Here's how I did it, and i'm just wondering if someone can point out where i went wrong.
Here is the basic structure of the code:
Code:
function onEnterFrame() {
for (i = 0; i < count; i++) {
myMovieClips = this["myMovieClip" + i];
if (condition = true) {
//i'm pretty sure that the problem is right here:
myMovieClips.doSomething();
myMovieClips.doSomethingElse();
}
}
}
//then i have my functions
function doSomething() {
}
function doSomethingElse() {
}
The functions that i created aren't getting triggered for some reason.
Any help with this would be appreciated.
Functions And Loops - Basic
Hello,
Is it possible to loop within a function, and then wait upon the result of that loop before continuing with the fuction?
for instance:
Code:
function one(){
varName = false;
varName = function two();
trace(varName);
}
function two(){
//loop until a variable's value is changed to 'true' by another movie (this happends after about 10 seconds)
varChanged = true;
return varChanged;
}
I need to say "Loop until the variable's value has been changed to 'true' and then continue with the rest of the function. Is this possible?
Thank you.
Laef
[F8] For Loops Making Functions
I'm making a game where balls are made dynamically
with a for loop and the functions that let you drag
them are also made in the for loop.
Code:
//this is the number of balls
blu = 6;
ball = {};
game = {};
for (var i = 0; i<=blu; i++) {
this.attachMovie("ball", "ball"+i+"_mc", i+1);
game["ball"+i] = {};
//this sets the intial dragging to false
game["ball"+i].dragging = false;
game["ball"+i].x = _root["ball"+i+"_mc"]._x;
game["ball"+i].y = _root["ball"+i+"_mc"]._y;
//this is where the trouble is. in this function "game["ball"+i].dragging" isn't
//recognized as anything.
//If you trace it, it will say undefined, even though it is
//set as false in the lines above. What do I do?
_root["ball"+i+"_mc"].onPress = function() {
startDrag(this);
game["ball"+i].dragging = true;
};
_root["ball"+i+"_mc"].onRelease = function() {
stopDrag();
game["ball"+i].dragging = false;
};
}
This is really perplexing. Any help?
BTW, the code has my comments in it to help.
Setting Functions In Loops
This problem has come up about once every 6 months for me, usually I change my thinking and work around it but today I've decided to try and learn a little something...
I have a bunch of movieclips on stage with similar ascending Instance Names such as "clip_1" "clip_2" etc. I'll loop through them adding an onRelease function that, for example starts a different clip..So Clicking on 'clip_1' will play 'movie_1' etc.
But the variable that I use in the onRelease function always equals the last value that it was set at. So if I had 30 Clips, the function attached to them all plays movie_30.
I understand why this is happening.... I just don't know how to go about fixing it.
I use this method allot for things like calendars. Load each day of the month as a movieclip, and add a onRelease function that brings up detailed info for that day.
Code:
clip_text = new Array("foo", "bar");
for (i=0; i<=1; i++) {
cnt_but = "clip_"+i;
_root[cnt_but].onRelease = function() {
trace(clip_text[i]);
};
}
In this example 2 movieclips 'clip_1' and 'clip_2' Clicking on them both results in "bar" and I want clip_1 to trace "foo".
Is there an easy syntax way to fix this? Or am I really just going about this the wrong way?
Thanks!
Loops And Button Functions
I have 8 buttons in a image gallery. Each named btn01 - btn08.
Once the user clicks on a button it should update a few variables, run two functions and assign different visible and enabled states. I had written this like:
ActionScript Code:
_root.thumb.btn01.onRelease = function() {
startLoading(imagePath + "_1_large.jpg");
url = imagePath + "_1_large.jpg";
downloadName = fileName + "_1_large.jpg";
centerimage();
centerzoom();
_root.thumb.thumb_active.gotoAndStop(1);
_root.thumb.btn01.enabled = 0;
_root.thumb.btn01.visible = false;
_root.thumb.btn02.enabled = 1;
_root.thumb.btn02.visible = true;
_root.thumb.btn03.enabled = 1;
_root.thumb.btn03.visible = true;
_root.thumb.btn04.enabled = 1;
_root.thumb.btn04.visible = true;
_root.thumb.btn05.enabled = 1;
_root.thumb.btn05.visible = true;
_root.thumb.btn06.enabled = 1;
_root.thumb.btn06.visible = true;
_root.thumb.btn07.enabled = 1;
_root.thumb.btn07.visible = true;
_root.thumb.btn08.enabled = 1;
_root.thumb.btn08.visible = true;
};
but in the quest to improve my code I thought... yeah I can do that with a loop. Shockingly, the following code doesn't work. Could somebody take a look and tell me where I'm going wrong please?
ActionScript Code:
for (var i = 1; i <= 8; i++) {
this.thumb.btn0(+i).onRelease = function() {
anotherFunction(i)
for (var j = 1; j <= 8; j++) {
if (i = j) {
this.thumb.btn0(+j).enabled = 0;
this.thumb.btn0(+j).visible = false;
}
else {
this.thumb.btn0(+j).enabled = 1;
this.thumb.btn0(+j).visible = true;
}
}
}
}
function anotherFunction(value){
startLoading(imagePath + "_" + value + "_large.jpg");
url = imagePath + "_" + value + "_large.jpg";
downloadName = fileName + "_" + value + "_large.jpg";
centerimage();
centerzoom();
this.thumb.thumb_active.gotoAndStop(value);
}
ta,
jbw
Api Drawing (loops And Functions?)
Quite possibly the newbiest question known to man so I apologise but I'm new to flash and actionscript.
I'm creating a picture using the api drawing
I have the following code:
var my_new_mc_1=new MovieClip()
my_new_mc.graphics.lineStyle(3,0x3399CC,1)
my_new_mc.graphics.beginFill(0x3399CC,1)
my_new_mc.graphics.drawRect(300,150,50,50)
addChild(my_new_mc)
I want to create the above shape four times in different places on the stage.
Now I could just use the exact same code above and define a new variable but to make the code efficient I'm guessing I'll have to use a loop or function?
How would one do that? I've been trying for ages and getting no where
Recursive Functions And Loops
Hi guys, I'm having a problem here.
I'm trying to create a minesweeper game as exercise, but I stumbled upon a problem.
I've cleaned the code for you so only the relevant code is here:
ActionScript Code:
function expand(cell) { getbounds(bounds,cell.row,cell.col); var sum:Number = 0; for (i = bounds[0]; i <= bounds[1]; i++) { for (j = bounds[2]; j <= bounds[3]; j++) { var cellName:String = "cell"+i+"x"+j; if (_root[cellName].bomb) { sum++; } } } cell.txt = sum; if (sum == 0) { for (i = bounds[0]; i <= bounds[1]; i++) { for (j = bounds[2]; j <= bounds[3]; j++) { cellName = "cell"+i+"x"+j; trace (cellName); if (_root[cellName].txt == " ") { trace ("1"); expand(_root[cellName]); trace ("2"); } } } } }var bounds = new Array(4);function getbounds(bounds, row, col) { bounds[0] = Math.max(1, row-1); // Minimale hoogte dat hij moet checken bounds[1] = Math.min(rows, row+1); // Maximale hoogte dat hij moet checken bounds[2] = Math.max(1, col-1); // Minimale breedte dat hij moet checken bounds[3] = Math.min(columns, col+1); // Maximale breedte dat hij moet checken}
the getbounds function delivers me the direct neighbors. 8 of them to be exactly. Or, to say it better, it gaves me the maximum values of each.
The 2 loops go trough these values, and check each of the value of 1 neighbor. It works great. But if I hit a square that isn't connected with a bomb, I want to do this function on each neighbor. So I use the recursive function method. I just simply recall it. So the first neighbor that's in line for the expand() function is topleft. And it works also great on that. But instead of cleaning the loop it will take the topleft neighbor of the next one as well.
Since flash works per line based on his code, I understand why he is doing that. But I do not understand is why flash breaks the loop.
Here is a picture for a better view:
Does anyone know how to solve this?
Help - Multiple Functions From For... Loops
hey guys...
i was hoping you could help me with this next one, iv'e been dancing around it for a while and i have a feeling that the answer is pretty simple.
let's just say that I have 10 buttons which i want to create eventhandlers to
and that i want to loop through a number to create these functions for these buttons and to make stuff easier on myself.
ex.
var i:int = new int();
for (i=1;i<=10;i++)
{
this["button"+i].buttonMode=true;
}
like that... (that's turning out ok)
but my question is, how do i do the same with functions and eventlisteners.
ex.
var i:int = new int();
for (i=1;i<=10;i++)
{
this["button"+i].buttonMode=true;
this["button"+i].addEventListener(MouseEvent.CLICK,this["button"+i+"handler"]);
function this["button"+i](Evt:Event):void
{
this["button"+i].gotoAndPlay("a");
}
}
because that doesn't work
is there a way to create functions and eventlisteners like that? whats that even called? - i don't even know how to name my problem... i know it might sound noobish to some of you, but i could really use some help.
Eldad
Problem With Functions And Arrays In Loops
I have an array of movie clips and an array of links. I want to assign the links to the movie clips. It works fine without a loop. The working code follows. But it says the file is undefined when I attempt to replace the last 9 lines of code with:
for (m=0;m<3;m++) {
movieArray[m].onRelease = function (m) {
getURL(linksArray[m]);
}
}
Here is the working code. How do I use a loop for the last block? -- the real project has at least 24 movies and links.
***
//Make a batch of empty movie clips
_root.holder1_mc.createEmptyMovieClip("imageHolder 1_mc",1);
_root.holder1_mc.imageHolder1_mc.createEmptyMovieC lip("image",51)
_root.holder1_mc.createEmptyMovieClip("imageHolder 2_mc",2);
_root.holder1_mc.imageHolder2_mc.createEmptyMovieC lip("image",52)
_root.holder1_mc.createEmptyMovieClip("imageHolder 3_mc",3);
_root.holder1_mc.imageHolder3_mc.createEmptyMovieC lip("image",53)
//put images in the movie clips
_root.holder1_mc.imageHolder1_mc.image.loadMovie(" hmr/thumbs/abc001.jpg")
_root.holder1_mc.imageHolder2_mc.image.loadMovie(" hmr/thumbs/abc002.jpg")
_root.holder1_mc.imageHolder3_mc.image.loadMovie(" hmr/thumbs/abc003.jpg")
//position the movie clips
_root.holder1_mc.imageHolder1_mc._x=7;
_root.holder1_mc.imageHolder2_mc._x=64;
_root.holder1_mc.imageHolder3_mc._x=121;
//Array of links (in the real project these are read from an external file)
_global.linksArray = new Array();
linksArray = ["http://www.tonybarre.com/matt","http://www.tonybarre.com/travis","http://www.tonybarre.com/melissa"];
//Array of movie clips (in the real project I create these with a loop)
_global.movieArray = new Array();
movieArray = [_root.holder1_mc.imageHolder1_mc, _root.holder1_mc.imageHolder2_mc, _root.holder1_mc.imageHolder3_mc]
//Assign the links to the corresponding movie clips (I want to put the following code into a loop
movieArray[0].onRelease = function () {
getURL(linksArray[0]);
}
movieArray[1].onRelease = function () {
getURL(linksArray[1]);
}
movieArray[2].onRelease = function () {
getURL(linksArray[2]);
}
Problem With Functions And Arrays In Loops MX2004
I have an array of movie clips and an array of links. I want to assign the links to the movie clips. It works fine without a loop. The working code follows. But it says the file is undefined when I attempt to replace the last 9 lines of code with:
for (m=0;m<3;m++) {
movieArray[m].onRelease = function (m) {
getURL(linksArray[m]);
}
}
Here is the working code. How do I use a loop for the last block? -- the real project has at least 24 movies and links.
***
//Make a batch of empty movie clips
_root.holder1_mc.createEmptyMovieClip("imageHolder 1_mc",1);
_root.holder1_mc.imageHolder1_mc.createEmptyMovieC lip("image",51)
_root.holder1_mc.createEmptyMovieClip("imageHolder 2_mc",2);
_root.holder1_mc.imageHolder2_mc.createEmptyMovieC lip("image",52)
_root.holder1_mc.createEmptyMovieClip("imageHolder 3_mc",3);
_root.holder1_mc.imageHolder3_mc.createEmptyMovieC lip("image",53)
//put images in the movie clips
_root.holder1_mc.imageHolder1_mc.image.loadMovie(" hmr/thumbs/abc001.jpg")
_root.holder1_mc.imageHolder2_mc.image.loadMovie(" hmr/thumbs/abc002.jpg")
_root.holder1_mc.imageHolder3_mc.image.loadMovie(" hmr/thumbs/abc003.jpg")
//position the movie clips
_root.holder1_mc.imageHolder1_mc._x=7;
_root.holder1_mc.imageHolder2_mc._x=64;
_root.holder1_mc.imageHolder3_mc._x=121;
//Array of links (in the real project these are read from an external file)
_global.linksArray = new Array();
linksArray = ["http://www.tonybarre.com/matt","http://www.tonybarre.com/travis","http://www.tonybarre.com/melissa"];
//Array of movie clips (in the real project I create these with a loop)
_global.movieArray = new Array();
movieArray = [_root.holder1_mc.imageHolder1_mc, _root.holder1_mc.imageHolder2_mc, _root.holder1_mc.imageHolder3_mc]
//Assign the links to the corresponding movie clips (I want to put the following code into a loop
movieArray[0].onRelease = function () {
getURL(linksArray[0]);
}
movieArray[1].onRelease = function () {
getURL(linksArray[1]);
}
movieArray[2].onRelease = function () {
getURL(linksArray[2]);
}
Using For Loops To Assign Listeners And Create Functions, Possible?
i have 20 text fields that i want to have mouse event listeners and their functions, can i use for loops to take care of them all? my code works for one of them, but then i tried using a for loop to do all of them and couldn't. also, can buttonMode be applied to text fields in a simple way?
this code doesn't work:
Code:
for(var i:int = 1; i <= 20; i++)
{
part[i].addEventListener(MouseEvent.CLICK, goToFrame[i]);
part[i].addEventListener(MouseEvent.ROLL_OVER, onOver[i]);
part[i].addEventListener(MouseEvent.ROLL_OUT, onOut[i]);
}
for(var j:int = 1; i <= 20; j++)
{
function goToFrame[j](e:MouseEvent):void
{
part[j].removeEventListener(MouseEvent.ROLL_OVER, onOver[j]);
part[j].removeEventListener(MouseEvent.ROLL_OUT, onOut[j]);
this.gotoAndPlay("S" + [j]);
}
}
for(var k:int = 1; i <= 20; k++)
{
function onOver[k](e:MouseEvent):void
{
part[k].textColor = 0x0000FF;
}
}
for(var l:int = 1; i <= 20; l++)
{
function onOut[l](e:MouseEvent):void
{
part[l].textColor = 0x000000;
}
}
this code works:
Code:
part1.addEventListener(MouseEvent.CLICK, goToFrame1);
part1.addEventListener(MouseEvent.ROLL_OVER, onOver1);
part1.addEventListener(MouseEvent.ROLL_OUT, onOut1);
function goToFrame1(e:MouseEvent):void
{
part1.removeEventListener(MouseEvent.ROLL_OVER, onOver1);
part1.removeEventListener(MouseEvent.ROLL_OUT, onOut1);
gotoAndPlay("S1");
}
function onOver1(e:MouseEvent):void
{
part1.textColor = 0x0000FF;
}
function onOut1(e:MouseEvent):void
{
part1.textColor = 0x000000;
}
Using For Loops To Assign Listeners And Create Functions, Possible?
i have 20 text fields that i want to have mouse event listeners and their functions, can i use for loops to take care of them all? my code works for one of them, but then i tried using a for loop to do all of them and couldn't. also, can buttonMode be applied to text fields in a simple way?
this code doesn't work:
Code:
for(var i:int = 1; i <= 20; i++)
{
part[i].addEventListener(MouseEvent.CLICK, goToFrame[i]);
part[i].addEventListener(MouseEvent.ROLL_OVER, onOver[i]);
part[i].addEventListener(MouseEvent.ROLL_OUT, onOut[i]);
}
for(var j:int = 1; i <= 20; j++)
{
function goToFrame[j](e:MouseEvent):void
{
part[j].removeEventListener(MouseEvent.ROLL_OVER, onOver[j]);
part[j].removeEventListener(MouseEvent.ROLL_OUT, onOut[j]);
this.gotoAndPlay("S" + [j]);
}
}
for(var k:int = 1; i <= 20; k++)
{
function onOver[k](e:MouseEvent):void
{
part[k].textColor = 0x0000FF;
}
}
for(var l:int = 1; i <= 20; l++)
{
function onOut[l](e:MouseEvent):void
{
part[l].textColor = 0x000000;
}
}
this code works:
Code:
part1.addEventListener(MouseEvent.CLICK, goToFrame1);
part1.addEventListener(MouseEvent.ROLL_OVER, onOver1);
part1.addEventListener(MouseEvent.ROLL_OUT, onOut1);
function goToFrame1(e:MouseEvent):void
{
part1.removeEventListener(MouseEvent.ROLL_OVER, onOver1);
part1.removeEventListener(MouseEvent.ROLL_OUT, onOut1);
gotoAndPlay("S1");
}
function onOver1(e:MouseEvent):void
{
part1.textColor = 0x0000FF;
}
function onOut1(e:MouseEvent):void
{
part1.textColor = 0x000000;
}
Creating Conditional Loops Inside Functions
I want to create the appearance of reverse motion by moving the timeline backwards one frame at a time with a gotoAndStop(x) command.
On the button I have the following code:
ActionScript Code:
on(release) {
//25 is the target frame that is passed to the function
reverse_frame(25);
}
In frame one of the actions layer in my movie I have the following code:
ActionScript Code:
function reverse_slide(tgtframe) {
gotoAndStop(tgtframe);
}
This moves the movie back to frame 25 and works just fine. The problem comes when I introduce the conditional loop like this:
ActionScript Code:
function reverse_slide(tgtframe) {
framecount = tgtframe;
If (framecount <> 0);
gotoAndStop(framecount);
framecount = framecount - 1;
}
I'm not sure if it's a syntax problem or a restriction in AS. Advice or help would be greatly appreciated as related to this problem.
Thank you,
Keith
Desperate Help Needed Dynamic Functions And Nested Loops
Hey - Before you read any of this, i desperately need some help with this, and i appreciate any help whatsoever!
I have this motion function with easing. It has five parameters:
The 'target' to apply the function to, the desired X and Y values ('newX'/'newY')of the targer, the desired X and Y scales of the target ('newXscale'/'newYscale') and the desired alpha of the target ('newAlpha')
Code:
function motionTween(target, newX, newY, newXscale, newYscale, newAlpha) {
currentX = target._x;
currentY = target._y;
currentXscale = target._xscale;
currentXscale = target._yscale;
currentAlpha = target._alpha;
speed = .1;
if (newX=undefined) {
newX = currentX;
}
if (newY=undefined) {
newY = currentY;
}
if (newXscale=undefined) {
newXscale = currentXscale;
}
if (newYscale=undefined) {
newYscale = currentYscale;
}
if (newAlpha=undefined) {
newAlpha = currentAlpha;
}
this.onEnterFrame = function() {
var dx = newX-currentX;
var dy = newY-currentY;
var dxs = newXscale-currentXscale;
var dys = newYscale-currentYscale;
var da = newAlpha-currentAlpha;
if ((dx<1) && (dy<1) && (dxs<1) && (dys<1) && (da<1)) {
currentX = newX;
currentY = newY;
currentXscale = newXscale;
currentYscale = newXscale;
currentAlpha = newAlpha;
delete this.onEnterFrame;
} else {
currentX += dx*speed;
currentY += dy*speed;
currentXscale += dxs*speed;
currentYscale += dys*speed;
currentAlpha += da*speed;
}
};
}
Firstly, in the coding, i have been guided to include "this.onEnterFrame = function() {" ... what (in this instance) does 'this' refer to?
Here are three loops nested within each other. This is what i am using to apply the function and other things.
The code refers to instances of movie clips, that together make up the map of a college.
I have two floors: _root.floor_1 and _root.floor_2
within each floor are a number of departments: eg. _root.floor_1.dep_1 or _root.floor_2.dep_5 etc.
within each department is a number of rooms: eg.
_root.floor_1.dep_7.room_3 or _root.floor_2.dep_2.room_12 etc.
I have stored these in arrays:
'home_ar' is both floors (_root.floor_1, _root.floor_2)
'floor_1_ar' is the number of departments in floor 1 (_root.floor_1.dep_1, _root.floor_1.dep_2 --> etc.)
'floor_2_ar' is the number of departments in floor 2
'floor_1_dep_1_ar' is the number of rooms in each department
(_root.floor_1.dep_1.room_1, _root.floor_1.dep_1.room_2 --> etc.)
I started the entries to each array at 1, and not 0 (eg home_ar[1] = _root.floor_1, home_ar[2] = _root.floor_2). For this reason, in the loop sequences below, i have begun each at 1, and ended each at 'the-length-of-the-corresponding-array minus 1'
Code:
for (a=1; a<=(home_ar.length -1); a++) {
var iFloor = _root["floor_"+a];
trace("a = " + a) // returns 1 and 2
for (b=1; b<=["floor_"+a+"_ar"].length; b++) {
var iDep = iFloor["dep_"+b];
trace("b = " + b) // returns only 1
iDep.onRollOver = function() {
motionTween.call(iDep, undefined, undefined, (iDep._xscale*1.2), (iDep._yscale*1.2), undefined);
createTextField("focus_txt", 6, 20, 10, 0, 0);
createTextField("focusvalue_txt", 7, 60, 10, 0, 0);
focus_txt.text = "Focus:";
focus_txt.autoSize = "left"
focus_txt.setTextFormat(fmt_3);
focusvalue_txt.text = this.depname;
focusvalue_txt.autoSize = "left"
focusvalue_txt.setTextFormat(fmt_4);
};
iDep.onRollOut = function() {
motionTween.call(iDep, undefined, undefined, ((iDep._xscale)/1.2), ((iDep._yscale)/1.2), undefined);
};
iDep.onRelease = function() {
motionTween.call(iFloor, iDep.newX, iDep.newY, iDep.newXscale, iDep.newYscale, undefined);
};
for (c=1; c<=(["floor_"+a+"_dep_"+b+"_ar"].length -1); c++) {
var iRoom = iDep["room_"+c];
trace("c = " + c) // returns only 1
iRoom.onRollOver = function() { //*I am yet to write this*
};
}
}
}
My first problem, it seems, is that the 'for (a=1; a<=(home_ar.length -1); a++)' is incrementing, but the 'for (b=1; b<=["floor_"+a+"_ar"].length; b++)' & 'for (c=1; c<=(["floor_"+a+"_dep_"+b+"_ar"].length -1); c++) {' loops are not. When i trace a, it returns the desired values of 1 and 2 (_root.floor_1 and _root.floor_2). However, both tracing b and tracing c return always only 1. This suggests to me that "floor_"+a+"_ar" and "floor_"+a+"_dep_"+"_ar" are not being recognised as arrays? I can assure you that the arrays are set up correctly, and return the desired components when i perform a trace.
My second problem, is that i cannot call the function. As b returns 1, i am able to atleast see the affects of '.onRollOver', '.onRollOut' and '.onRelease' of '_root.floor_1.dep_1' and '_root.floor_2.dep_1'. The text box i have asked to create when rolling on to each department displays the correct variable i stated elsewhere '.depName', which tells me the function is being recognised. However, the motion function is not being performed.
I am a little rusty in the area of dynamic functions and calling methods/functions, so i need help here!
Elsewhere (and before this loop is performed) i have stated the desired value of corresponding to each department for "newX, newY, newXscale, newYscale and newAlpha" (with the target being directly put into the functions parameters) though as no motion is performed, i do not know if they are being called.
Originaly, i set them up using 'with' statements. However, when i performed a trace, none of the values were returned - is this because i did not define 'newX' etc as variables? Now i have them set up manually, and upon tracing, they are returned.
Is there a way of condensing this into a single line instruction for each department and then calling this?
Code:
//old
with (_root.floor_1.dep_1) {
newX = 16;
newY = 690;
newXscale = 350;
newYscale = 350;
depname = "Department 1";
}
trace ("dep_1.newX = " + _root.floor_1.dep_1.newX) //returns nothing
//current
_root.floor_1.dep_1.newX = 16;
_root.floor_1.dep_1.newY = 690;
_root.floor_1.dep_1.newXscale = 350;
_root.floor_1.dep_1.newYscale = 350;
_root.floor_1.dep_1.depname = "Department 1";
trace ("dep_1.newX = " + _root.floor_1.dep_1.newX) //returns 16
//is there anyway of doing something along the lines of this?
parameters(newX, newY, newXscale, newYscale, newAlpha, depname) = new parameters
_root.floor_1.dep_1.parameters (16, 690, 350, 350, undefined, "Department_1")
Finally, i've read about 'calling a method of an object'. What is this? and could i use it to call the methods (or the parameters) of my departments?
I know this is a long list of problems, but i desperately need some help, and i cannot explain one problem without listing how my code is working, and then highlighting my other problems within it. I REALLY appreciate any help! Thank You!
Complex Drag & Drop Problem - Functions, Arrays, For Loops
I have a drag and drop exercise I am trying to make. 20 objects are draggable, with 10 hit areas or places these things can be dropped into.
The thing I am struggling with is that once each of the 10 hit areas have been filled by 10 of the draggable objects, a popup window should appear saying 'are you sure you have finished?'.
I am trying to achieve this by using:
A function (containing a for loop) called 'totalfilled' at lines 8-29, and an array called 'allfilled'. Totalfilled is called when an object is dropped onto each of the drop targets.
At the moment the script seems to be working if you drop the objects onto boxes 2-10, but not for the first box (nearest the top of the screen.)
The flash file (mx2004) can be accessed at http://www.munzamedia.co.uk/flash/AF...01-06_temp.fla
Any suggestions would be appreciated.
Thanks,
Mark
|