flash interactivity_behavior_button
Adding the button behavior
A large number of Flash developers used clip symbols as buttons in ActionScript 1 and 2. By defining the onRelease event they become clickable. In ActionScript 3 the same behavior can be obtained by activating the buttonMode property on a Sprite or MovieClip occurrence.
In a new document let’s create a Sprite symbol using the technique described in the chapter entitled Symbols. We associate the class name Button with it using the linking properties panel, then we transform the shape contained in it into a new clip.
We give it the instance name backgroundButton. On top of the clip we create a dynamic textfield that we call myCaption:
Then we stack the instances of Button vertically:
// sections var captions:Array = new Array ( "Homepage", "What’s New?", "Photos", "Links", "Contact" ); // creation of container var container:Sprite = new Sprite(); container.x = 20; addChild ( container ); function createMenu ():void { // number of sections var lng:int = captions.length; var myButton:Button; for ( var i:int = 0; i< lng; i++ ) { // instantiation of Button myButton = new Button(); myButton.y = 20 + i * (myButton.height + 10); // adding to the display list container.addChild ( myButton ); } } createMenu();
In order to activate the Button behavior on objects other than SimpleButton we need to activate the buttonMode property:
// activation of the Button behavior myButton.buttonMode = true;
Then we add the text:
function createMenu ():void { // number of sections var lng:int = captions.length; var myButton:Button; for ( var i:int = 0; i< lng; i++ ) { // instantiation of Button symbol myButton = new Button(); myButton.y = 20 + i * (myButton.height + 10); // activation of Button behavior myButton.buttonMode = true; // allocation of content myButton.myCaption.text = captions[i]; // adding to the display list container.addChild ( myButton ); } }
By using objects other than the SimpleButton class to create Buttons we need to take certain behaviors into consideration like the reactivity of overlaid objects to the mouse. The way that objects react to mouse events has changed with ActionScript 3. We will now look at these subtleties.
????
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42



