带图像的自定义按钮

发布于 2024-08-20 22:45:29 字数 240 浏览 3 评论 0原文

您好,我正在尝试在 actionscript 3 中创建一个按钮,该按钮将由自定义图像(来自我的 HDD 的 car.jpg )制作。是否可以使用 SimpleButton 类来做到这一点(我的意思是有没有办法加载图像并将其附加到按钮?)或者还有其他方法吗? 如果我想在按钮中也有动画,当我从按钮上滚动鼠标指针时,我该怎么办?

ps:我只想使用actionscript 3(我的意思是所有项目都是作为链接到Document类的.as文件完成的)。

Hi i'm trying to create a button in actionscript 3 that will be made by a custom image ( a car.jpg from my HDD ). Is it possible to do that with SimpleButton class ( i mean is there a way to load the image and attach it to the button ?) or is there another way?
If i would like to have animation also in the button when let's say i roll over the mouse pointer from the button what do i have to do?

ps: i would like to use only actionscript 3 ( i mean all the project is done as a .as file that is linked to Document class ).

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

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

发布评论

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

评论(1

梦行七里 2024-08-27 22:45:29

尽管 George Profenza 已经在评论下指出了最佳解决方案,但如果您好奇如何实现 SimpleButton 类,您可能需要查看 as3 文档参考示例:

adobe livedocs - SimpleButton 示例

我还编写了一个简单的示例,仅在一个“复杂的按钮”,但使用 SimpleButton 类,因此您可以了解如何扩展该类,并为每个状态提供自己的图形。代码如下:

// this goes in your app
var button:MySimpleButton = new MySimpleButton();
addChild(button);

MySimpleButton.as

package  
{
    import flash.display.DisplayObject;
    import flash.display.SimpleButton;
    import flash.display.Sprite;

    public class MySimpleButton extends SimpleButton 
    {
        private var upAlpha : Number = 1;
        private var overAlpha : Number = 0.5;

        public function MySimpleButton(upState : DisplayObject = null, overState : DisplayObject = null, downState : DisplayObject = null, hitTestState : DisplayObject = null)
        {
            upState = new ButtonImgDisplayState( upAlpha);
            overState = new ButtonImgDisplayState( overAlpha);
            downState = new ButtonImgDisplayState( upAlpha);
            hitTestState = new ButtonImgDisplayState( upAlpha);

            super(upState, overState, downState, hitTestState);
        }
    }
}

ButtonImgDisplayState.as

package  
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.net.URLRequest;

    public class ButtonImgDisplayState extends Sprite 
    {

        public function ButtonImgDisplayState(_alpha:Number)
        {
            var my_loader : Loader = new Loader();
            my_loader.load(new URLRequest("car.jpg"));
            addChild(my_loader);

            this.alpha = _alpha;
        }
    }
}

SimpleButton 的作用是让您无需设置侦听器,但您有义务绕过单独的 DisplayObjects 的状态,这使得您在处理状态之间的转换时按钮变得更加严格。

希望您觉得这很有用。

Although George Profenza already pointed the best solution under the comment if feel curious how you could implement the SimpleButton class you might want to take a look into th as3 document reference examples:

adobe livedocs - SimpleButton Example

I have also wrote a simple example that only makes a "simple button" in a "complicated button" but makes use of SimpleButton class, so you can take a look how you can extend the class, and give to each state it's own graphic. Here is the code:

// this goes in your app
var button:MySimpleButton = new MySimpleButton();
addChild(button);

MySimpleButton.as

package  
{
    import flash.display.DisplayObject;
    import flash.display.SimpleButton;
    import flash.display.Sprite;

    public class MySimpleButton extends SimpleButton 
    {
        private var upAlpha : Number = 1;
        private var overAlpha : Number = 0.5;

        public function MySimpleButton(upState : DisplayObject = null, overState : DisplayObject = null, downState : DisplayObject = null, hitTestState : DisplayObject = null)
        {
            upState = new ButtonImgDisplayState( upAlpha);
            overState = new ButtonImgDisplayState( overAlpha);
            downState = new ButtonImgDisplayState( upAlpha);
            hitTestState = new ButtonImgDisplayState( upAlpha);

            super(upState, overState, downState, hitTestState);
        }
    }
}

ButtonImgDisplayState.as

package  
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.net.URLRequest;

    public class ButtonImgDisplayState extends Sprite 
    {

        public function ButtonImgDisplayState(_alpha:Number)
        {
            var my_loader : Loader = new Loader();
            my_loader.load(new URLRequest("car.jpg"));
            addChild(my_loader);

            this.alpha = _alpha;
        }
    }
}

The thing about the SimpleButton is to spare you to set listeners, but you are obligated to go around the states that are separated DisplayObjects making you button a more rigid thing when dealing with transitions between states.

Hope you find this useful.

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