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

2023-09-08 15:23:49 作者:昵稱加載中***

我在闪光灯(SWC)创建了一些按钮的状态,我想使用它们在纯AS3项目。他们分别与类neatButton_on和neatButton_off的影片剪辑。我称他们是这样的:

I created some button states in flash (swc), and I want to use them in a pure AS3 project. They are movie clips with class of neatButton_on and neatButton_off respectively. I call them like this:

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

我的问题是,这是做到这一点的最好方法是什么?我也用的资产,尤其是对同一个项目,在那里我已经放在一个影片剪辑的一切,然后切换帧需要的不同状态。是一种方式比其他的快?有没有最好的做法?

My question is, is this the best way to do this? I've also used assets, especially for different states of the same item, where I've put everything in one movie clip and then switched frames as needed. Is one way faster than the other? is there a best practice?

推荐答案

我将分享我用来创建两个国家级图像按钮的简单类。我用TweenLite的以实现阿尔法吐温上翻滚。

I'll share a simple class I use for creating two state image buttons. I use Tweenlite to effect a alpha tween on rollover.

    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()。

You just need to send in a string that gets fed into a switch statement. This class can be used for multiples cases this way. And then to kill, simply call _imgButton.destroy() in the parent.

本类本质上是与你相似,只是增加了一些功能;)

This class is essentially similar to yours, just with some added functionality ;)

欢呼声