Discuss Anything: Button Animation With Rollover State That Stays When User Clicks A Button - Discuss Anything

Jump to content


Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
Page 1 of 1

Button Animation With Rollover State That Stays When User Clicks A Button Rate Topic: -----

#1 User is offline   Yusuke96 

  • Newbie
  • Pip
  • Group: Kontributors
  • Posts: 4
  • Joined: 25-March 07
  • Current Mood: Current Mood:

Posted 25 March 2007 - 10:56 AM

Quote

We are going to need animation and I don't like to use time-line animation so I use AS to code my animation. A very good reason to not use timeline animation is this: Say in my example (located here: http://www.creativen...ithScalingBox/) say I made the little red squares animate with a time-line animation and when the user rolls over the button I tell it to start. Say the red square is half way through its animation and I rollOff of it. The red square will automatically "jump" to the rollOut animation which will make it appear to change size from say half size to it's whole size and then start to shrink. That would look really bad. But if we use code to do the sizing this will not happen because the code does not care what size it currently is, it will just tell it to become large or small.

Hope I didn't loose you there. My point is we are going to need some imported code to make the AS animation. I like to use Fuse. You can get fuse at www.mosesSupposes.com. In this tutorial/article I will show you how to use it.

Make a directory named "com" in the root directory of your project and in there place the mosesSupposes directory that you just downloaded. Trust me, it is very simple to use (Fuse CAN get much more complex but that is for another article).

Create your buttons

In order to have rollOvers we need buttons. To do this I created a simple rectangle, selected it and hit F8 to make it a MovieClip called button, and gave it an instance name of button0. I opened up that button and made a red rectangle (again use the example as a guide) and then selected it, hit F8 to select it and called it scaler with an instance name of scaler. Then in my root I selected button0 and copied it twice so now there are three buttons on the stage with instance names of button0, button1, and button2.

Now lets code!

Import Fuse
In order to use Fuse for animation we have to tell Flash that it exists. This is pretty simple:
// import Fuse
import com.mosesSupposes.fuse.*;
// set up  zigoEngine to use just simple shortcut animation
ZigoEngine simpleSetup(Shortcuts);


That was simple wasn't it?

Now lets code the buttons!

Coding the Buttons

When a button is clicked we need to know which one it is, and so does everyone else because when we make functions to scale the buttons down (actually just scales down the red MovieClip in the button) we don't want to scale down the button that was just clicked. Lets declare that variable now:
// declare the variable that will be the button that is clicked (MovieClip)
var buttonClicked:MovieClip;


Now lets create an Array that holds the names of our buttons so we can assess them in a for loop:
// create the array to hold all the names of the movieClips
var myButtonsArray:Array = new Array(this["myButton0"], this["myButton1"], this["myButton2"]);


Now lets add the code for the first button (button0):
//
myButton0.onRelease = function():Void  {
 // create the variable that we pass into the scaleButtonDown func
 var exception = this;
 // run the scale buttons down function
 scaleButtonsDown(exception);
 // set the buttonClicked to this
 buttonClicked = this;
};
//

The above code creates an exception variable with a value of this (this IS button0) and passes it to the scaleButtonsDown function so that function will scale down all buttons except the "exception" variable.

it also sets the buttonClicked to this (again, this refers to itself [button0]).

Now we will create the rollOver and RollOut functions for button0:
myButton0.onRollOver = function():Void  {
 // run scale buttons up func and pass in this
 scaleButtonsUp(this);
};
//
myButton0.onRollOut = function():Void  {
 // run scale buttons down function
 scaleButtonsDown();
};
//


Now before we create the functionality for the other two buttons lets make the scaleButtonsUp and scaleButtonsDown functions because it will become very simple to understand what is happening with only code for one button.
// scale buttons up function
scaleButtonsUp = function (args):Void {
 // scale the args (remember the button passed in this) to 100 over 1 seconds
 args.scaler.scaleTo(100, 1);
};

This function takes the args (remember we passed in this [button0]) and scales the button's scaler to 100% over one second. This occurs when the user rolls over the the button.

Simple hu? Now lets make the scale buttons down function, it is a little more complex but easy to understand.
// scale buttons down func
scaleButtonsDown = function ():Void {
 // loop through myButtonsArray
 for (var i:Number = 0; i<myButtonsArray.length; i++) {
  // scale all buttons down over a 1 second period
  myButtonsArray[i].scaler.scaleTo(0, 1);
  // make the buttonClicked scale to 100 over zero seconds
 }
   buttonClicked.scaler.scaleTo(100, 0);
};

this function creates a loop that will loop as many times as we have items in our myButtonsArray (that'll be three times because our array has 3 items, mybutton0, mybutton1, and myButton2).

In this loop we tell myButtonArray[i] scale to 0% in one second .

Outside the loop we tell buttonClicked (remember we set this to THIS when the button was clicked?).

Then all you have to do is copy the code for button0 and paste it and make it the code for button1 (do the same for button 2)
//
myButton1.onRelease = function():Void  {
 buttonClicked = this;
 var exception = this;
 scaleButtonsDown(exception);
};
//
myButton1.onRollOver = function():Void  {
 scaleButtonsUp(this);
};
//
myButton1.onRollOut = function():Void  {
 scaleButtonsDown(null);
};
//
myButton2.onRelease = function():Void  {
 buttonClicked = this;
 var exception = this;
 scaleButtonsDown(exception);
};
//
myButton2.onRollOver = function():Void  {
 scaleButtonsUp(this);
};
//
myButton2.onRollOut = function():Void  {
 scaleButtonsDown(null);
};

The very last thing you need to do it to tell all the buttons on the stage to scale down their scaler movieClips as we don't want them big until the user rolls over them:
// scale all buttons down at start of movie
scaleButtonsDown();

That's it we are done and now you have buttons that do something on rollOver and something on rollOut but when clicked the rollOver state stays on that button.

Here is all of the code together:
import com.mosesSupposes.fuse.*;
// set up zigoEngine
ZigoEngine.simpleSetup(Shortcuts);
// declare the variable that will be the button that is clicked (MovieClip)
var buttonClicked:MovieClip;
// create the array to hold all the names of the movieClips
var myButtonsArray:Array = new Array(this["myButton0"], this["myButton1"], this["myButton2"]);
//
myButton0.onRelease = function():Void  {
 // create the variriable that we pass into the scaleButtonDown func
 var exception = this;
 // run the scale buttons down functino
 scaleButtonsDown(exception);
 // set the buttonClicked to this
 buttonClicked = this;
};
//
myButton0.onRollOver = function():Void  {
 // run scale buttons up func and pass in this
 scaleButtonsUp(this);
};
//
myButton0.onRollOut = function():Void  {
 // run scale buttons down function
 scaleButtonsDown();
};
//
myButton1.onRelease = function():Void  {
 buttonClicked = this;
 var exception = this;
 scaleButtonsDown(exception);
};
//
myButton1.onRollOver = function():Void  {
 scaleButtonsUp(this);
};
//
myButton1.onRollOut = function():Void  {
 scaleButtonsDown(null);
};
//
myButton2.onRelease = function():Void  {
 buttonClicked = this;
 var exception = this;
 scaleButtonsDown(exception);
};
//
myButton2.onRollOver = function():Void  {
 scaleButtonsUp(this);
};
//
myButton2.onRollOut = function():Void  {
 scaleButtonsDown(null);
};
//
// scale buttons down func
scaleButtonsDown = function ():Void {
 // loop through myButtonsArray
 for (var i:Number = 0; i<myButtonsArray.length; i++) {
  trace(myButtonsArray[i]._name);
  // scale all buttons down over a 1 second period
  myButtonsArray[i].scaler.scaleTo(0, 1);
  // make the buttonClicked scale to 100 over zero seconds
  
 }
 buttonClicked.scaler.scaleTo(100, 0);
};
// scale buttons up function
scaleButtonsUp = function (args):Void {
 // scale the args (remember the button passed in this) to 100 over 1 seconds
 args.scaler.scaleTo(100, 1);
};
// scale all buttons down at start of movie
scaleButtonsDown();

Notice from jlhaslip:
This tutorial is not Original material. quoted. Warned.
http://www.actionscript.org/resources/arti...tton/Page1.html

0

Share this topic:


Page 1 of 1


Similar Topics Collapse

  Topic Forum Started By Stats Last Post Info
New Replies Icon Image Board Software With Password.
Image Board with user login.
Website Discussion elaks 
  • 4 Replies
  • 4,496 Views
New Replies Icon Increasing Revenue, Clicks And Earnings Google Adsense / Adwords Zubair1 
  • 1 Reply
  • 2,336 Views
Hot Topic (New) Icon $_post Without A Form
Trying to post a value with using a submit button
PHP Programming fffanatics 
  • 15 Replies
  • 5,165 Views
New Replies Icon What Religion Are You?
Discussion about trap17 users religion
Regional 100janovski 
  • 8 Replies
  • 763 Views
New Replies Icon Who Took Doomachine2000 As A Username? Questions & Queries doom145 
  • 0 Replies
  • 1,285 Views
New Replies Icon Microsoft To Let Pc Users Turn Off Ie Web Browser Web Browsers Saint_Michael 
  • 7 Replies
  • 624 Views
Hot Topic (New) Icon Avg Anti-virus Question
Users please read
Software spawn_syxx9 
  • 21 Replies
  • 12,668 Views
New Replies Icon Remove Window Animation Computers affhotspot 
  • 1 Reply
  • 1,406 Views
New Replies Icon Flash Cs3 Making A Button That Opens A Movie In A New Window. Web Design trimtab1 
  • 1 Reply
  • 618 Views
New Replies Icon Buttons Suggestions xXsyreXx 
  • 4 Replies
  • 1,043 Views
New Replies Icon Social Network Pays Users 100% Ad Revenue
Flixya.com has a lot of potential
Make Money Online mudanoman 
  • 12 Replies
  • 2,189 Views
New Replies Icon My Animations Flash Animations 50Dime 
  • 5 Replies
  • 3,181 Views
New Replies Icon Userban Tool From Dynamicdrive.com - Take A Look :) Freebie Stuff CyberWizard 
  • 1 Reply
  • 501 Views
New Replies Icon Radio Button HTML, XML etc.. kvarnerexpress 
  • 6 Replies
  • 4,225 Views
New Replies Icon I Have Install Windows 2003 Server
I want multiple user to login my server
Computer Networks callme 
  • 8 Replies
  • 2,225 Views
New Replies Icon Php Form Data And Conditional Statements
My third tutorial
Tutorials ghostrider 
  • 5 Replies
  • 3,033 Views
Locked Topic Icon Invalid Username And Password
Any help would be much appreciated
Free Web Hosting: Misc. Requests Totalwar 
  • 4 Replies
  • 1,438 Views
Locked Topic Icon Problems Activating Hosting Account
username and password invalid
Questions & Queries reatum 
  • 10 Replies
  • 2,548 Views
New Replies Icon Firefox's Answer To Ie's Phishing Filter?
users of the sacred browser can breathe once more!
Computer Security Issues & Exploits electriic ink 
  • 5 Replies
  • 1,152 Views
New Replies Icon Michael Crichton - State Of Fear
The new book.
Books Vapor 
  • 1 Reply
  • 3,631 Views

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Google Fan :-) We Recommend Firefox.     Customize / Settings Connect Us on facebook