Solved

flash as3 complicated button question

Posted on 2008-10-29
6
619 Views
Last Modified: 2013-11-11
say i have 10 movieclips on stage that all have button functionality.
i also have 2 additional buttons.
when i click one of the buttons i want five of the movie clips to be 'deactivated': unclickable and grayed out.
when i click the other button the other five should be deactivated and the first five should be re-activated.

any advice on how i could do that with as3? or if you could point me to a tutorial somewhere.

thanks.
0
Comment
Question by:flashdope
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 22837049
If you are not dynamically generating the 10 movieclip buttons, then you could try the following bit of code.  Replace each "MovieClip1", "MovieClip2", etc.... with the Instance Names of the movie clips in your movie.  I hope this is pretty self explanatory.  Let me know.

~Ryan
// changeGroupState(GroupNumber:Number, EnabledState:Boolean);
 

var group1:Array = new Array();

var group2:Array = new Array();

var pathToThis:MovieClip = this; // Path to Movie Clips
 

group1.push("MovieClip1");

group1.push("MovieClip2");

group1.push("MovieClip3");

group1.push("MovieClip4");

group1.push("MovieClip5");
 

group2.push("MovieClip6");

group2.push("MovieClip7");

group2.push("MovieClip8");

group2.push("MovieClip9");

group2.push("MovieClip10");
 

button1.onRelease = function():Void {

     changeGroupState(1, false);

     changeGroupState(2, true);

}
 

button2.onRelease = function():Void {

     changeGroupState(1, true);

     changeGroupState(2, false);

}
 

function changeGroupState(g:Number, b:Boolean):Void {

     var thisGroup:Array = pathToThis["group"+ g];

     for(var i:Number=0; i< thisGroup.length; i++){

          var thisClip:MovieClip = pathToThis[thisGroup[i]];

          thisClip.enabled = b;

          thisClip._alpha = (b) ? 100 : 50; // Slight fade out

     }

}

Open in new window

0
 

Author Comment

by:flashdope
ID: 22837186
thanks ryan,  i'll try this. one question: can one movie clip belong to more than one group?
0
 
LVL 1

Expert Comment

by:trypt
ID: 22840355
here's what i will do... in AS3

   

btn1.addEventListener(MouseEvent.CLICK, btnClick);

btn2.addEventListener(MouseEvent.CLICK, btnClick);

function btnClick(e:MouseEvent):void

{

	groupOff();

	groupOn(int(e.target.name.substr(3)));

}

function groupOff():void

{

	for (var i:int=0; i<10; i++)

	{

		this["g"+(i+1)].alpha=.2;

		this["g"+(i+1)].buttonMode=false;

		this["g"+(i+1)].mouseChildren=false;

	}

}

function groupOn($n:int):void

{

	for (var i:int=$n*5; i>$n*5-5; i--)

	{

		this["g"+i].alpha=1;

		this["g"+i].buttonMode=true;

		this["g"+i].mouseChildren=true;

	}

}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 14

Expert Comment

by:Designbyonyx
ID: 22847730
yes, you can assign any number of movieclips to any number of groups.  If you want to create another group, just create another array variable (see code below) - var groupN:Array = new Array(); - where N is the number of the group.  And to add a movieclip to a group, just use the syntax -  groupN.push("Movie_Clip_Name"); - again, where N is the number of the group, and Movie_Clip_Name is the Instance Name of your movie (as assigned in the Properties Panel).

It is important to note that if you want to assign a movieclip to multiple groups, then you must call the "disable function" FIRST (meaning, the second parameter of the function MUST be FALSE).  So for example, the code that I posted in my first comment would be incorrect, because in the action for button2, I called the "disable function" last.  So if you want to use my code from the first post, you must modify the actions for button2.  However, I will post the corrected code at the end of this post.

The same movie clip can belong to multiple groups.  In the code below, I have created a third group and assigned MovieClip3 to all three groups.

By the way, I did not mention in my first post that my code was AS2 compliant.  I will include both the AS2 and AS3 compliant code this time ;)  In the AS3 code, I took a slightly different approach to enabling and disabling the movies.  All you have to do is call the function enableGroup(N) and it will automatically disable all groups except for the one you specify, no matter how many groups you create.
// ------- AS2 Compliant --------------

// changeGroupState(GroupNumber:Number, EnabledState:Boolean);

 

var group1:Array = new Array();  // Group 1

var group2:Array = new Array();  // Group 2

var group3:Array = new Array();  // Group 3 

var pathToThis:MovieClip = this; // Path to Movie Clips

 

group1.push("MovieClip1");

group1.push("MovieClip2");

group1.push("MovieClip3");  // belongs to all groups

group1.push("MovieClip4");

group1.push("MovieClip5");

 

group2.push("MovieClip3");  // belongs to all groups

group2.push("MovieClip6");

group2.push("MovieClip7");

group2.push("MovieClip8");

group2.push("MovieClip9");

group2.push("MovieClip10");
 

group3.push("MovieClip3");  // belongs to all groups

group3.push("MovieClip4");

group3.push("MovieClip5");

group3.push("MovieClip8");

group3.push("MovieClip9");

 

button1.onRelease = function():Void {

     changeGroupState(1, false);  // Disable Group 1

     changeGroupState(3, false);  // Disable Group 3

     changeGroupState(2, true);   // Enable Group 2

}

 

button2.onRelease = function():Void {

     changeGroupState(2, false);  // Disable Group 2

     changeGroupState(3, false);  // Disable Group 3

     changeGroupState(1, true);   // Enable Group 1

}

 

function changeGroupState(g:Number, b:Boolean):Void {

     var thisGroup:Array = pathToThis["group"+ g];

     for(var i:Number=0; i< thisGroup.length; i++){

          var thisClip:MovieClip = pathToThis[thisGroup[i]];

          thisClip.enabled = b;

          thisClip._alpha = (b) ? 100 : 50; // Slight fade out

     }

}
 
 
 

// ------- AS3 Compliant --------------

// enableGroup( groupID:int );

 

var group1:Array = new Array();  // Group 1

var group2:Array = new Array();  // Group 2

var group3:Array = new Array();  // Group 3 

var pathToMCs:MovieClip = this; // Path to Movie Clips

 

group1.push("MovieClip1");

group1.push("MovieClip2");

group1.push("MovieClip3");  // belongs to all groups

group1.push("MovieClip4");

group1.push("MovieClip5");

 

group2.push("MovieClip3");  // belongs to all groups

group2.push("MovieClip6");

group2.push("MovieClip7");

group2.push("MovieClip8");

group2.push("MovieClip9");

group2.push("MovieClip10");
 

group3.push("MovieClip3");  // belongs to all groups

group3.push("MovieClip4");

group3.push("MovieClip5");

group3.push("MovieClip8");

group3.push("MovieClip9");
 

button1.addEventListener(MouseEvent.CLICK, buttonAction1);

button2.addEventListener(MouseEvent.CLICK, buttonAction2);
 

trace(typeof(group1));

 

function buttonAction1(e:MouseEvent):void {

     enableGroup(1);

	  /* The above is the same as doing the following:

	 changeGroupState(1, false);

	 changeGroupState(2, false);

	 changeGroupState(3, true);  // Note: disable all other groups before enabling a group

	 */

}

 

function buttonAction2(e:MouseEvent):void {

     enableGroup(2);

	 /* The above is the same as doing the following:

	 changeGroupState(1, false);

	 changeGroupState(3, false);

	 changeGroupState(2, true);  // Note: disable all other groups before enabling a group

	 */

}
 

function enableGroup(n:int):void {

	for(var i:int = 1; typeof(this["group"+ i]) == "object"; i++){

		// Disable ALL groups

		changeGroupState(i, false);	

	}

	// Enable the specified group

	changeGroupState(n, true);

}

 

function changeGroupState(g:int, b:Boolean):void {

     var thisGroup:Array = this["group"+ g];

     for(var i:int=0; i< thisGroup.length; i++){

          var thisClip:MovieClip = pathToMCs[thisGroup[i]];

          thisClip.enabled = b;

          thisClip.alpha = (b) ? 1 : .5; // Slight fade out

     }

}

Open in new window

0
 
LVL 14

Accepted Solution

by:
Designbyonyx earned 500 total points
ID: 22847769
Sorry, there was an erroneous trace action and misinformation in one of my comments.  Ignore the code in the last post, and use this.  Take care.
// ------- AS2 Compliant --------------

// changeGroupState(GroupNumber:Number, EnabledState:Boolean);

 

var group1:Array = new Array();  // Group 1

var group2:Array = new Array();  // Group 2

var group3:Array = new Array();  // Group 3 

var pathToThis:MovieClip = this; // Path to Movie Clips

 

group1.push("MovieClip1");

group1.push("MovieClip2");

group1.push("MovieClip3");  // belongs to all groups

group1.push("MovieClip4");

group1.push("MovieClip5");

 

group2.push("MovieClip3");  // belongs to all groups

group2.push("MovieClip6");

group2.push("MovieClip7");

group2.push("MovieClip8");

group2.push("MovieClip9");

group2.push("MovieClip10");

 

group3.push("MovieClip3");  // belongs to all groups

group3.push("MovieClip4");

group3.push("MovieClip5");

group3.push("MovieClip8");

group3.push("MovieClip9");

 

button1.onRelease = function():Void {

     changeGroupState(1, false);  // Disable Group 1

     changeGroupState(3, false);  // Disable Group 3

     changeGroupState(2, true);   // Enable Group 2

}

 

button2.onRelease = function():Void {

     changeGroupState(2, false);  // Disable Group 2

     changeGroupState(3, false);  // Disable Group 3

     changeGroupState(1, true);   // Enable Group 1

}

 

function changeGroupState(g:Number, b:Boolean):Void {

     var thisGroup:Array = pathToThis["group"+ g];

     for(var i:Number=0; i< thisGroup.length; i++){

          var thisClip:MovieClip = pathToThis[thisGroup[i]];

          thisClip.enabled = b;

          thisClip._alpha = (b) ? 100 : 50; // Slight fade out

     }

}

 

 

 

// ------- AS3 Compliant --------------

// enableGroup( groupID:int );

 

var group1:Array = new Array();  // Group 1

var group2:Array = new Array();  // Group 2

var group3:Array = new Array();  // Group 3 

var pathToMCs:MovieClip = this; // Path to Movie Clips

 

group1.push("MovieClip1");

group1.push("MovieClip2");

group1.push("MovieClip3");  // belongs to all groups

group1.push("MovieClip4");

group1.push("MovieClip5");

 

group2.push("MovieClip3");  // belongs to all groups

group2.push("MovieClip6");

group2.push("MovieClip7");

group2.push("MovieClip8");

group2.push("MovieClip9");

group2.push("MovieClip10");

 

group3.push("MovieClip3");  // belongs to all groups

group3.push("MovieClip4");

group3.push("MovieClip5");

group3.push("MovieClip8");

group3.push("MovieClip9");

 

button1.addEventListener(MouseEvent.CLICK, buttonAction1);

button2.addEventListener(MouseEvent.CLICK, buttonAction2);

 

function buttonAction1(e:MouseEvent):void {

     enableGroup(1);

          /* The above is the same as doing the following:

         changeGroupState(2, false);

         changeGroupState(3, false);

         changeGroupState(1, true);  // Note: disable all other groups before enabling a group

         */

}

 

function buttonAction2(e:MouseEvent):void {

     enableGroup(2);

         /* The above is the same as doing the following:

         changeGroupState(1, false);

         changeGroupState(3, false);

         changeGroupState(2, true);  // Note: disable all other groups before enabling a group

         */

}

 

function enableGroup(n:int):void {

        for(var i:int = 1; typeof(this["group"+ i]) == "object"; i++){

                // Disable ALL groups

                changeGroupState(i, false);     

        }

        // Enable the specified group

        changeGroupState(n, true);

}

 

function changeGroupState(g:int, b:Boolean):void {

     var thisGroup:Array = this["group"+ g];

     for(var i:int=0; i< thisGroup.length; i++){

          var thisClip:MovieClip = pathToMCs[thisGroup[i]];

          thisClip.enabled = b;

          thisClip.alpha = (b) ? 1 : .5; // Slight fade out

     }

}

Open in new window

0
 

Author Closing Comment

by:flashdope
ID: 31511350
thanks ryan for all that work
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now