PDA

View Full Version : Selected button in menu



deathbydesignnz
January 24th, 2008, 01:56 AM
hey, i was wondering if anyone would be keen to convert this to AS3 for me? ive had a go but can only get the rollovers/out working not the selected button function.. I tried using event.currentTarget but i dont know how to reset the previous selected button..

Heres the AS2 script:

/*
Created by Helen Triolo 2006/05/13 for flash-creations.com
As described at http://flash-creations.com/notes/groupofbuttons.php
*/

// create an array of all nav buttons in group
var groupinfo:Array = [birds, dogs, bears, giraffes, mice];

// create constants to define start and stop of grow and shrink sequences
// (shrink should be exactly the same sequence as grow, in reverse frame order)
var GROWSTART:Number = 10;
var GROWSTOP:Number = 19;
var SHRINKSTART:Number = 20;
var SHRINKSTOP:Number = 29;

// create a variable to track the currently selected button
var activebtn:MovieClip;

// doRollOver: start the rollover action or process,
// unless the button is currently selected
function doRollOver() {
if (this != activebtn) {
// if shrinking, send to corresponding frame in grow
if (this._currentframe > SHRINKSTART && this._currentframe < SHRINKSTOP) {
this.gotoAndPlay(GROWSTART + SHRINKSTOP - this._currentframe);
} else {
this.gotoAndPlay(GROWSTART);
}
}
}

// doRollOut: start the rollout action or process,
// unless the button is currently selected
function doRollOut() {
if (this != activebtn) {
// if growing, send to corresponding frame in shrink
if (this._currentframe > GROWSTART && this._currentframe < GROWSTOP) {
this.gotoAndPlay(SHRINKSTART + GROWSTOP - this._currentframe);
} else {
this.gotoAndPlay(SHRINKSTART);
}
}
}

// doClick: 1) return previously selected button to normal, 2) show visual
// indication of selected button, 3) update activebtn
function doClick() {
activebtn.gotoAndStop(SHRINKSTOP); // return previously selected to normal

delete this.onEnterFrame; // stop activity on selected mc
this.gotoAndStop(GROWSTOP); // change appearance of selected mc

activebtn = this; // update pointer to current selection
}

// assign functions to each event for each button in the group
function init() {
for (var mc in groupinfo) {
groupinfo[mc].onRollOver = doRollOver;
groupinfo[mc].onRollOut = doRollOut;
//groupinfo[mc].onRelease = doClick;
}
}

init();

deathbydesignnz
January 24th, 2008, 03:35 AM
got it working heres the code for anyone interested..

import flash.display.*;
import flash.events.*;
// create an array of all nav buttons in group
var menuData:Array = new Array();

menuData.push(menu1);
menuData.push(menu2);

// create a variable to track the currently selected button
var activebtn = new MovieClip;
var selectd:Boolean = false
var clip = new MovieClip;

// doRollOver: start the rollover action or process,
// unless the button is currently selected
function doRollOver(event:MouseEvent):void {
if(event.currentTarget != activebtn){
event.currentTarget.gotoAndPlay(1);
}
}

// unless the button is currently selected
function doRollOut(event:MouseEvent):void {
if(event.currentTarget != activebtn){
event.currentTarget.gotoAndPlay(11);
}
}

function doClick(event:MouseEvent):void {
activebtn.gotoAndPlay(11);
activebtn = event.currentTarget;
activebtn.gotoAndStop(10);
}

// assign functions to each event for each button in the group
function init() {
var i=0;
var btn;
for (i=0;i<2;i++) {
btn = menuData[i];
btn.addEventListener(MouseEvent.MOUSE_OVER, doRollOver);
btn.addEventListener(MouseEvent.MOUSE_OUT, doRollOut);
btn.addEventListener(MouseEvent.CLICK, doClick);
}
}

init();

mchazzle
March 31st, 2008, 01:23 PM
this is SOOooo useful.
except im like a NOOB in flash AS-anything
if you could PLease explain how the code works

thankyou thankyouthankyouthankyou thankyou thankyou :D

i guess i cant just copy and paste that..coz that didnt work..

mchazzle
April 1st, 2008, 12:46 PM
i trieddddd

i think my codes are all messed up
im trying to do something in a way i shouldnt be doing i think.

someone help me pleaseee

jmalko
September 26th, 2008, 03:45 PM
is there any way to set a button to default here? For instance i want the "Home" button to be active upon init.

jmalko
September 26th, 2008, 04:20 PM
found a possibly ghetto solution:


import flash.display.*;
import flash.events.*;
// create an array of all nav buttons in group
var menuData:Array = new Array();

menuData.push(menu1);
menuData.push(menu2);

// create a variable to track the currently selected button
var activebtn = new MovieClip();
var activehome = new Object();
var selectd:Boolean = false
var clip = new MovieClip;

// doRollOver: start the rollover action or process,
// unless the button is currently selected
function doRollOver(event:MouseEvent):void {
if (activehome == menu1) {
if (event.currentTarget != menu1){
event.currentTarget.gotoAndPlay(1);
} else if(event.currentTarget != activebtn){
event.currentTarget.gotoAndPlay(1);
}
}

// unless the button is currently selected
function doRollOut(event:MouseEvent):void {
if (activehome == menu1) {
if (event.currentTarget != menu1){
event.currentTarget.gotoAndPlay(11);
} else if(event.currentTarget != activebtn){
event.currentTarget.gotoAndPlay(11);
}
}

function doClick(event:MouseEvent):void {
activebtn.gotoAndPlay(11);
activebtn = event.currentTarget;
activebtn.gotoAndStop(10);
if (activehome == menu1) {
menu1.gotoAndPlay(11);
}
activehome = null;

}

// assign functions to each event for each button in the group
function init() {
var i=0;
var btn;
for (i=0;i<2;i++) {
btn = menuData[i];
btn.addEventListener(MouseEvent.MOUSE_OVER, doRollOver);
btn.addEventListener(MouseEvent.MOUSE_OUT, doRollOut);
btn.addEventListener(MouseEvent.CLICK, doClick);
activehome = menu1;
menu1.gotoAndStop(10);
}
}

init();


Thank you original poster... you saved me a ton of time!

davelk
September 28th, 2008, 01:26 PM
nice one, solved a problem ive had for days now

davelk
September 28th, 2008, 02:00 PM
Ok, still having problems...

each of my buttons uses nextFrame() and prevFrame() with an ENTER_FRAME listener to give them smooth rollover and rollout animations. This means that my conditional statements are usually within the function thats triggered by the ENTER_FRAME listener not the MouseEvent listeners

I'd really appreciate some suggestions