Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / Flash Actionscript / July 2008



Tip: Looking for answers? Try searching our database.

Array of movieclip doesn't working right

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rockylhc - 25 Jul 2008 04:27 GMT
Hi, I created an array, and inside the array, there's some name that same with
the movieclip on stage. So now I try to make it like, everytime I click on the
movieclip, the array will delete it name in the array. Now still trying it out
and seems like need some help, anyone?

And also include the file where I stucked.
http://www.omorimori.com/rockylhc/splice.fla
kglad - 25 Jul 2008 06:28 GMT
use the splice() method of arrays to remove your movieclip reference (or instance name) from your array.
Noelbaland - 25 Jul 2008 06:28 GMT
Hello,

You weren't too far off.  I just made a few changes to your script. View the
comments for more info.

Hope that helps

var fruit:Array = ["btn0", "btn1", "btn2", "btn3", "btn4"];

function removeFromArray(target:String)
{
    for (i=0; i<5; i++) {
                // Changed this around.  Query the array first.
        if (fruit[i] == target) {
             fruit.splice(i, 1);
            trace("Fruit array: " + fruit + "\n");
        }
    }
}

for(var i=0; i<5; i++) {
        // Put the name in a string variable
    var btnName:String = fruit[i];

        // Declare a movieclip variable type and assign it
        // the current movieclip on stage
    var btn:MovieClip = _root[btnName];

    btn.onRelease = function() {
        trace(this._name + " was removed from array");
        removeFromArray(this._name);       
    };
}
rockylhc - 25 Jul 2008 07:01 GMT
so....how if I want to add back the deleted one back to array? Let say while the btn0 was deleted, then if I press the 3rd button, btn3 will be deleted, and then btn0 add back to the array.
Noelbaland - 26 Jul 2008 01:30 GMT
OK, so it's been awhile since I've played around with arrays to this extreme
but I figured it out.  What you do is create a temporary array to store the
name of the current button that is being clicked.  You then remove that button
name from the fruit array.  When you click on another button, you again store
that button's name in the temp array and replace it with the button name that's
already in there. Sounds pretty simple - right? Well take a look at the logic
involved.

1) Start movie
Fruit array [btn0,btn1,btn2,btn3,btn4]  Temp array [empty]
2) Click on btn2
Is this the first button ever clicked on? YES
if(YES){
Store btn2 in Temp array and remove it from Fruit array
Temp array [btn2]  Fruit array [btn0,btn1,btn3,btn4]
}
3) Click on btn0
Is this the first button ever clicked on? NO
if(NO){
a) Store btn0 in Temp array
Temp array [btn2, btn0]

b) Remove it from Fruit array
Fruit array [btn1,btn3,btn4]

c) Replace it with btn2
Fruit array [btn1,btn2,btn3,btn4]

d) Remove btn2 from Temp array
Temp array [btn0]
}

4) Repeat 2 - 3 for subsequent button clicks.

Anyway - here's the script below that uses the logic above.  Hope it helps

var fruit:Array = ["btn0", "btn1", "btn2", "btn3", "btn4"];

// Temporary array that will store the name of the
// current button that is clicked on.
var temp:Array = new Array();

// This variable is used to determine whether a button
// has been clicked for the first time or not.
// See further comments for more info.
var firstClicked:Boolean = false;

function removeFromArray(target:String)
{
    for (i=0; i<fruit.length; i++) {
        // If this is NOT the first time we've clicked on
        // a button, then...
        if(firstClicked){
            // check the button name is in the array.  If so...
            if (fruit[i] == target){
                // remove it from the fruit array and replace it
                // with the first button in the temp array.
                fruit.splice(i,1,temp[0]);
                // Finally remove the first button in the temp array
                // leaving only the current button.
                temp.shift();
            }
        } else {
            // If this is the first time a button has been clicked, then
            // check that the name is in the fruit array and remove it.
            if (fruit[i] == target){
                fruit.splice(i,1);
            }
        }
    }
}

for(var i=0; i<5; i++) {
    var btnName:String = fruit[i];
    var btn:MovieClip = _root[btnName];
   
    btn.onRelease = function(){
        // Push the button name into the temp array
        temp.push(this._name);
       
        removeFromArray(this._name);
        trace("Temporary array = " + temp);
        trace("Fruit array = " + fruit);
       
        // So after the first button click, set this
        // to true so that all subsequent button clicks will
        // perform the swapping out routine in the
        // removeFromArray function.
        firstClicked = true;
    };
}
rockylhc - 26 Jul 2008 12:21 GMT
thanks for helping.....that's great....one thing that bothers me is, can the
name of button that going to be added from the temp array to the current array,
the position same with the starting one, or ascending one.
rockylhc - 26 Jul 2008 14:31 GMT
sorry i ask a stupid question
fruit.sort() do the thing.
rockylhc - 27 Jul 2008 23:15 GMT
After i looked into it, I found that if I click on the button few times, there will be duplicate name in the temp array, how can I make it not to store duplicate?
kglad - 28 Jul 2008 00:29 GMT
check your array to see if you're going to be adding a duplicate.
rockylhc - 28 Jul 2008 00:58 GMT
the array that I going to use is exactly same with the one I provided here, so
if I click few times on the same button, the temp array with add the same
button name. And I only hope that the temp only keep the recent one that I
clicked. How to do it?
kglad - 28 Jul 2008 01:28 GMT
it doesn't make sense to maintain an array that always will contain one
element.  you should use a variable if you only need to store one value.

but, in general, if you want to maintain an array of fixed length maintaining
only the most recently pushed elements of the array, use the shift() method.

what are you trying to accomplish?  asking specific questions is great when
you know the logic that's needed to accomplish your task.  but it doesn't
appear that you know what's needed.

so, without distilling what you're trying to do, explain what should happen if
btn0, btn3, btn2 are pressed in that order.  use more buttons if that isn't
enough to display the pattern.
rockylhc - 28 Jul 2008 01:57 GMT
I'm trying to make a list of button, when the mouse roll over them, each of
them will change color, when roll out, the color reset. So if the button
clicked, the button change color again and it will be reset to the default
color if another button is clicked. Maybe the method I'm trying to do in this
in wrong, am I?
Noelbaland - 28 Jul 2008 05:45 GMT
Well - now that we know what you want, then the following script will help you
get there.  In order for it to work though your buttons(btn1,btn2,etc) must be
movieclip symbols.  Inside each button you will need to have a movieclip symbol
with the instance name of 'bkg'.  This is the actual object that changes color
and can be whatever you like.

You can view my example
http://www.5degrees.co.nz/tests/SelectButtonExample.html and
http://www.5degrees.co.nz/tests/SelectButtonExample.zip it to see how it was
done.

var fruit:Array = new Array("btn0", "btn1", "btn2", "btn3", "btn4");

// Set the generic button color object
var btnColor:Color;

// Set colors for over and down states.  Change it to your colors
// Add another color if you want a different state
var orange:Number = 0xFF9900; // Rollout and Up state
var yellow:Number = 0xFFFF00; // Rollover and Down state

// This function takes the current movieclip(btn) that's been passed
// to it and...
function setButtonState(mc:MovieClip)
{
        // checks if it's in the fruit array.
    for(var i=0; i<fruit.length; i++){
                // If it is, then we change it's color to yellow
                // and disable the button
        if(fruit[i] == mc._name){
            btnColor = new Color(_root[mc._name].bkg);
            btnColor.setRGB(yellow);
            _root[mc._name].enabled = false;
        } else {
                        // We leave the rest of the buttons orange
                        // and enable the buttons
            btnColor = new Color(_root[btnArray[i]].bkg);
            btnColor.setRGB(orange);
            _root[btnArray[i]].enabled = true;
        }
    }
}

// Loop thru the current object and return...
for(var btn in this){
        // any movieclips with instance names where
        // the first 3 letters have "btn" in them
    if(substring(btn, 0, 3) == "btn"){
                // Store the names and make them a movieclip
                // object so that it can have event handlers
        var btn:MovieClip = this[btn];
       
                // Set to yellow on Rollover
        btn.onRollOver = function(){
            btnColor = new Color(this.bkg);
            btnColor.setRGB(yellow);
        }

                // Set back to orange on Rollover
        btn.onRollOut = function(){
            btnColor = new Color(this.bkg);
            btnColor.setRGB(orange);
        }

                // Call the setButtonState function and pass
                // in this button mc
        btn.onRelease = function(){
            setButtonState(this);
        }
    }
}
kglad - 28 Jul 2008 16:25 GMT
i know you're trying to help noel, but you're over-complicating this and
causing confusion.

the following is all that's needed:

var btnA:Array = [];  // list your button instance names here

var downCol:Number =  // assign your colors
var upCol:Number=
var overCol:Number=

for(var i=0;i<btnA;i++){
    btnA[i].col = new Color(btnA[i].bg);  // where bg is the movieclip in your
button whose color you want to change
    btnA[i].onRollOver=function(){
        this.col.setRGB(overCol);
    }
    btnA[i].onRollOut=function(){
        if(this!=lastClickedBtn){
            this.col.setRGB(upCol);
        }
    }
    btnA[i].onRelease=function(){
        lastClickedBtn.col.setRGB(upCol);
        lastClickedBtn=this;
    }
}
rockylhc - 28 Jul 2008 23:33 GMT
Thanks for being helpful kglad, the script that I tried doesn't work, maybe I didn't name it properly, can you help me to check my file?
http://rockylhc.110mb.com/new.fla
kglad - 29 Jul 2008 00:01 GMT
remove the quotes in btnA.  those are instance names, not strings.

and use btnA.length in the for-loop.
Noelbaland - 29 Jul 2008 03:53 GMT
Thanks for the heads up kglad!  Yeah my way was way too long. Thats what I like
about these forums is that someone will post script that solves a solution
another way. Or I should say - an easier way!  It's a learning lesson for all
levels of Flash users.  Good stuff!

Hopefully now, rockylhc has solved his problem.
rockylhc - 29 Jul 2008 06:11 GMT
Thanks everyone!
kglad - 29 Jul 2008 07:12 GMT
you're welcome.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.