使用 AS3 使用 PNG/GIF 图像作为按钮

发布于 2024-10-27 13:57:08 字数 156 浏览 4 评论 0原文

朋友们,

我是 AS3 的新手,请原谅我。我正在尝试仅使用 AS3 将图像用作按钮(PNG/GIF 图像作为按钮(简单、切换和多状态)- 鼠标悬停、正常和按下时图像更改)。我尝试去寻找。我所得到的只是设置按钮的图标。如果您可以分享代码片段或指针,那就太好了。

多谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-11-03 13:57:08

有多种方法可以在不将图像转换为影片剪辑的情况下执行此操作。您可以使用 [Embed] 标签将图像嵌入到 SWF 中......或者更好的方法是使用 Loader 类加载它们。

您将像这样加载图像:

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

...然后在加载完成后您将得到如下所示的 BitmapData(使用事件侦听器来捕获它):

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

BitmapData 对象需要一个 Bitmap 容器才能将其放置在阶段,因此您需要使用 BitmapData 作为输入来声明 Bitmap 实例。

var Bitmap:Bitmap = new Bitmap(myBitmapData);

由于可以将该位图添加到舞台上,因此您可以开始了。您可以将其用作按钮类的一部分。还有更多位图信息 这里是 8bitrocket。

对于我们这些不使用 Flash CS 5 或任何其他影片剪辑创建器的人来说,这是一个很好的方法。我正在使用这种技术在 FlashDevelop 中制作游戏,效果很好。

希望这作为“仅动作脚本”方法有所帮助。祝你好运。

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.

薄暮涼年 2024-11-03 13:57:08

获取 png 或 jpg 并将其转换为影片剪辑。为其指定一个实例名称。在此示例中,它将是myButton

然后为其分配以下代码:

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');
}

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');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文