在纯AS3项目中使用自定义图形资源

时间:2011-03-27 15:50:44

标签: flash actionscript-3

我在flash(swc)中创建了一些按钮状态,我想在纯AS3项目中使用它们。它们分别是neatButton_on和neatButton_off类的影片剪辑。我称之为:

public class neatButton extends neatModule
    {
        private var stateOn:neatButton_on;
        private var stateOff:neatButton_off;
        private var modContainer:Sprite = new Sprite();

        public function neatButton() 
        {
            addChild(modContainer);
            modContainer.x = 200;
            modContainer.y = 200;
            stateOff = new neatButton_off();
            stateOn = new neatButton_on();
            modContainer.addChild(stateOn);
            modContainer.addChild(stateOff);

            modContainer.addEventListener(MouseEvent.MOUSE_OVER, hover);
            modContainer.addEventListener(MouseEvent.MOUSE_OUT, unhover);
        }

        private function hover(e:MouseEvent):void
    {
        stateOff.visible = false;
    }

    private function unhover(e:MouseEvent):void
    {
        stateOff.visible = true;
    }
    }

我的问题是,这是最好的方法吗?我也使用了资产,特别是对于同一项目的不同状态,我将所有内容放在一个影片剪辑中,然后根据需要切换帧。比另一个快一点吗?有最好的做法吗?

2 个答案:

答案 0 :(得分:2)

您也可以使用基于SimpleButton类的资源而不是MovieClip。它们会自动改变翻转状态。

但你的做法并非完全错误。我会更改类的名称以大写字母开头,这更常规(所有类名以大写开头;变量和函数是camelCase,以小写开头,没有下划线)。所以:NeatButtonOnNeatButtonOff。 这样你就可以避免混淆什么是类,什么是变量。

答案 1 :(得分:1)

我将分享一个用于创建两个状态图像按钮的简单类。我使用Tweenlite在翻转时实现alpha补间。

    package com.b99.display.composite
    {
        import flash.display.*;
        import flash.events.MouseEvent;

        import com.greensock.*;

        /**
         * ...
         * @author Bosworth99
         */
        public class ImgButton extends Sprite
        {
            private var _canvas         :Sprite = new Sprite();
            private var _up             :Sprite;
            private var _over           :Sprite;
            private var _hit            :Shape;

            private var _intent         :String;

            public function ImgButton(intent:String) 
            {
                _intent = intent;

                super();
                init();     
            }

            private function init():void
            {
                constructButton();
                addEventHandlers();
            }

            private function constructButton():void
            {

                this.addChild(_canvas);

                switch (_intent) 
                {
                    case "button1":
                    {
                        _up     = new yourUpClip1()
                        _over   = new yourOverClip1();
                        break;
                    }
                    case "button2":
                    {
                        _up     = new yourUpClip2()
                        _over   = new yourOverClip2();
                        break;
                    }
                    case "button3":
                    {
                        _up     = new yourUpClip3()
                        _over   = new yourOverClip3();
                        break;
                    }
                }

                _canvas.addChild(_up);

                with (_over) 
                {
                    alpha   = 0;
                }
                _canvas.addChild(_over);

                _hit = new Shape();
                with (_hit) 
                {
                    graphics.beginFill(0x00FF40, 0);
                    graphics.drawRect(0, 0, _up.width + 5, _up.height +5);
                    graphics.endFill();
                    x       = -5;
                    y       = -5;
                }
                _canvas.addChild(_hit)

                _up.cacheAsBitmap       = true;
                _over.cacheAsBitmap     = true;
                _hit.cacheAsBitmap      = true;

                this.buttonMode         = true;
                this.mouseEnabled       = true;

            }

            private function addEventHandlers():void
            {
                _hit.addEventListener(MouseEvent.MOUSE_OVER,    over, false, 0, true);
                _hit.addEventListener(MouseEvent.MOUSE_OUT,     out, false, 0, true);
            }

            private function over(e:MouseEvent):void 
            {
                TweenLite.to(_over, .2,     {alpha:1   } );
            }
            private function out(e:MouseEvent):void 
            {
                TweenLite.to(_over, .2,     {alpha:0   } );
            }

            public function destroy():void
            {
                _hit.removeEventListener(MouseEvent.MOUSE_OVER, over);
                _hit.removeEventListener(MouseEvent.MOUSE_OUT,  out);

                _canvas.removeChild(_up);
                _up = null;         

                _canvas.removeChild(_over);
                _over = null;       

                _canvas.removeChild(_hit);
                _hit = null;

                this.removeChild(_canvas)
                _canvas = null;

                this.parent.removeChild(this);
            }
    //+++++++++++++++++++++++++++++++  end ++++++++++++++++++++++++++++++++++++++++
        }

    }

您只需要发送一个字符串,该字符串被送入switch语句。此类可以通过这种方式用于多个案例。然后杀死,只需在父母中调用_imgButton.destroy()。

这个类基本上与你的类相似,只是增加了一些功能;)

欢呼声