PDA

View Full Version : Use a class for similar but different buttons



Alber Kidd
May 12th, 2008, 05:00 AM
Hi,

I want to create a class for a load of buttons, submit, reset, and some others. I need to know how to setup my class so certain functions only apply to certain buttons. The problem is I cannot refer to my buttons by name only using the 'this' keword. Below is an example of what I am trying to achieve.

package{

import flash.display.MovieClip;
import flash.display.*;
import flash.events.*;
import flash.display.Stage;



public class button extends MovieClip {


public function button() {
mySubmitButton.addEventListener(MouseEvent.MOUSE_O VER, doSubmit)
this.addEventListener(MouseEvent.MOUSE_OUT, doReset)
}

function doSubmit(event:MouseEvent) {



}

function doReset (event:MouseEvent) {



}


}


}

amarghosh
May 12th, 2008, 07:52 AM
public class MyButton
{
public function MyButton(text:String)
{
//set up a text field and assign text;
//do all the drawing and mouse hover, mouse down, mouse up things u want on buttons here;
}
private function onMouseOver(e:MouseEvent):void {}
//other mouse handling methods;
}

//from other classes
var submitButton:MyButton = new MyButton("Submit");
submitButton.addEventListener(MouseEvent.CLICK, onSubmit);
function onSubmit(e:MouseEvent):void
{
//do stuff
}

Alber Kidd
May 12th, 2008, 08:20 AM
what does this do

var submitButton:MyButton = new MyButton("Submit");


The buttons are already created in the library is Submit the linkage class?

amarghosh
May 12th, 2008, 08:31 AM
The buttons are already created in the library is Submit the linkage class?

i thought u r trying to draw buttons with graphics library; "Submit" is not linkage, its the text that gets passed to MyButton constructor for creating a new button;
if u have created buttons in cs3, get the text on buttons using textField.text property and do the work accordingly;

or just use their instance name to assign different click handlers

Rezmason
May 12th, 2008, 12:26 PM
If you're assigning functions to different buttons, then somewhere you have a DisplayObject that contains those buttons in the display list. That's where you should put the event listeners.

What I've found useful (when possible) is to make a Library asset for each set of buttons or menu in your application, so that each button has an instanceName and a bunch of other properties (x, y, width, button type, rotation, color) that can be easily changed by the designer (instead of the programmer).

What sorts of properties do you need to assign to these buttons? If your buttons have a TextField in them, you can write a subclass of SimpleButton with a "text" getter and setter that inserts and returns the text in the TextField. Then all your buttons in your Library with a TextField can subclass your custom button class.

Alex Lexcuk
May 12th, 2008, 03:14 PM
//My_button_mc.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

dynamic public class My_button_mc extends MovieClip
{
var button_func:Function;
var up_mc,mid_mc:MovieClip;
public function My_button_mc(my_caption:String,my_func:Function) //конструктор
{
frame_4.gotoAndStop(1);
up_mc = new MovieClip();
mid_mc = new MovieClip();
up_mc.graphics.beginFill(0xFF);
up_mc.graphics.drawRect(0,0,frame_4.width,frame_4. height);
up_mc.alpha=0;
addChild(up_mc);
up_mc.buttonMode=true;
up_mc.addEventListener(MouseEvent.ROLL_OVER, on_roll);
up_mc.addEventListener(MouseEvent.ROLL_OUT, on_roll_out);
up_mc.addEventListener(MouseEvent.MOUSE_DOWN, on_down);
up_mc.addEventListener(MouseEvent.MOUSE_UP, on_up);

mur_caption.text=my_caption;

button_func=my_func;
}

private function on_roll(evt:MouseEvent):void
{
trace("on");
frame_4.gotoAndStop(2);
}
private function on_roll_out(evt:MouseEvent):void
{
trace("roll");
frame_4.gotoAndStop(0);

}
private function on_down(evt:MouseEvent):void
{
trace("press");
frame_4.gotoAndStop(3);
}
private function on_up(evt:MouseEvent):void
{
trace("up");
frame_4.gotoAndStop(1);
button_func();
}
}
}


it was use in
http://kind-armadillo.pochta.ru/FlaAC3/mur_cube.swf
http://kind-armadillo.pochta.ru/FlaAC3/cube.rar