Using PNG/GIF Images as buttons using AS3

爱⌒轻易说出口 提交于 2020-01-17 03:07:30

问题


Friends,

I am new to AS3 so pardon me. I am trying to use images as buttons (PNG/GIF images as buttons(simple, toggled and multi-state) - Image change on mouse over, normal and pressed) using AS3 only. I tried to search. All I got is to setting icon of a Button. It would be really great if you can share the code snippet or pointers.

Thanks a lot.


回答1:


There are ways to do this without converting an image to a Movieclip. You can either embed your images inside your SWF using the [Embed] tag....or, a better way, you can load them using the Loader class.

You would load your image like this:

var myloader:Loader = new Loader();
myloader.load(new URLRequest("myImage"));

...and then you would get its BitmapData like this after the loading completes (use an event listener to catch this):

var myBitmapData:BitmapData = Bitmap(myloader.content).bitmapData;

A BitmapData object needs a Bitmap container in order for it to be placed on the stage, so you'll need to declare a Bitmap instance using the BitmapData as input.

var Bitmap:Bitmap = new Bitmap(myBitmapData);

Since that Bitmap can be added to the stage, you're good to go. You can use that as part of your button class. There's some more Bitmap information here at 8bitrocket.

This is a nice way of doing it for those of us who don't use Flash CS 5, or any other movieclip creators. I'm making a game in FlashDevelop using this technique and it works well.

Hope this helps as an "actionscript only" method. Good luck.




回答2:


take a png or jpg and convert it to a movieclip. Give it an instance name. In this example it will be myButton.

then assign it the following code:

myButton.buttonMode = true; // gives it a hand cursor

// first param is the event type, second param is the function called
myButton.addEventListener(MouseEvent.CLICK, onClicked);
myButton.addEventListener(MouseEvent.MOUSE_OVER, onOver);
myButton.addEventListener(MouseEvent.MOUSE_OUT, onOut);

function onClicked(evt:MouseEvent):void
{
    // do something
}

function onOver(evt:MouseEvent):void
{
    // button that was rolled over can be referenced here with
    // evt.target like evt.target.gotoAndPlay('over');
}

function onOut(evt:MouseEvent):void
{
    // button that was rolled off of can be referenced here with
    // evt.target like evt.target.gotoAndPlay('out');
}


来源:https://stackoverflow.com/questions/5435693/using-png-gif-images-as-buttons-using-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!