;

PDA

Click to See Complete Forum and Search --> : how to addressing the MC as a button in AS3


fifin04
08-17-2006, 12:37 AM
Hi...

Okay I just keep wondering how to address the mcBTN in AS3.0 and assign the action to the button MC....
eg previously...

btn.onRelease = Function(){

gotoAndPlay(framelabel);
}

tq

Fall_X
08-17-2006, 05:53 AM
btn.addEventListener("click",someFunction);

Note that someFunction should be a function that you can reference afterwards or you won't be able te remove the event listener (which can cause GC problems, apparantly), so don't use an inline function() {} as in your example.

cancerinform
08-17-2006, 09:04 AM
When you have a movieclip and want to use it as a button, the cursor will not automatically become a hand. You need to determine that. Here is an example with myBut as MovieClip:

myBut.buttonMode = true;// creates the hand cursor
myBut.addEventListener("click", onClick);
function onClick (obj:Object)
{
trace("Hello World");
trace(obj.target);
}
The function must always have one argument. The argument refers to the event.

fifin04
08-20-2006, 01:27 PM
When you have a movieclip and want to use it as a button, the cursor will not automatically become a hand. You need to determine that. Here is an example with myBut as MovieClip:

myBut.buttonMode = true;// creates the hand cursor
myBut.addEventListener("click", onClick);
function onClick (obj:Object)
{
trace("Hello World");
trace(obj.target);
}
The function must always have one argument. The argument refers to the event.

Thanks for the tips cancerinform....but from what I explore is it I have to put the class I write inside the package..correct me if I'm wrong...so I try with the code u give ..but it seems error..is it I have to put inside package..as well..(just wanna ******** this confuse)
...by the way thanks for ur concern ...

cancerinform
08-20-2006, 01:58 PM
No this code is for a MovieClip on stage and the script in a frame on the main timeline. If you want to create a class it is slightly more complex. :)

fifin04
08-22-2006, 04:36 AM
No this code is for a MovieClip on stage and the script in a frame on the main timeline. If you want to create a class it is slightly more complex. :)

Hi..cancerinform...tq for ur explaination...

So it means I can still code on frame just like usual...Actually i just want to familiarize with AS3...as example shown in flash AS3 docs...

package{
import flash.display.Sprite;

public class Example extends Sprite{
public function Example(){
var child:ChildSprite = new ChildSprite();
addChild(child);
//is it for mc symbol I have to addChild also?...
}
}
}

import flash.display.Sprite;
import flash.events.MouseEvent;

class ChildSprite extends Sprite{
public function ChildSprite(){
graphics.beginFill(0xFF0000);
graphics.drawRect(0,0,100,100);
graphics.endFill();
addEventListener(MouseEvent.CLICK,_listener.clickH andler);
}
}




okay..what I want to know is how to addChild for MC symbol on stage...then assign onClick...hope someone can give some shed on light on it...

Thanks

cancerinform
08-22-2006, 08:42 AM
You have 2 possibilities.
1. Create a movie with a movieclip in the library and link it to the class ChildSprite.

package
{
import flash.display.MovieClip;
public class ChildSprite extends MovieClip
{
public function ChildSprite ()
{
this.buttonMode = true;
this.addEventListener("click", onClick);
}
private function onClick (obj:Object)
{
trace("Hello World");
}
}
}
Then create a document class.

package
{
import flash.display.MovieClip;
import ChildSprite;
public class Example extends MovieClip
{
private var myBut:MovieClip;
public function Example()
{
var myBut:ChildSprite = new ChildSprite();
addChild(myBut);
myBut.x = 100;
myBut.y = 100;
}
}
}
In the Document class field in the fla add: Example and test movie.

2. Create a movie with a movieclip in the library and link it to the class ChildSprite_2, which is basically empty.

package
{
import flash.display.MovieClip;
public class ChildSprite_2 extends MovieClip
{
public function ChildSprite_2 ()
{
}
}
}
Then create a document class.

package
{
import flash.display.MovieClip;
import ChildSprite_2;
public class Example_2 extends MovieClip
{
private var myBut:MovieClip;
public function Example_2 ()
{
var myBut:ChildSprite_2 = new ChildSprite_2 ();
addChild(myBut);
myBut.x = 100;
myBut.y = 100;
myBut.buttonMode = true;
myBut.addEventListener("click", onClick);
}
private function onClick (obj:Object)
{
trace("Hello World");
}
}
}
This last example is closer to the original stage example. :)

A third possibility is this to mark the movieclip as button.

package
{
import flash.display.MovieClip;
public class ChildSprite_2 extends MovieClip
{
public function ChildSprite_2 ()
{
this.buttonMode = true;
}
}
}
and then an above script as document class without the buttonmode.