在as3中创建一个旋转滚轮,在停止时显示文本

时间:2015-07-01 18:10:48

标签: actionscript-3 flash flash-cs6 roulette-wheel-selection

我正在尝试创建一个旋转轮,当它停在特定颜色时会显示优惠券代码。现在它在页面底部显示颜色,但我想添加与车轮停止时出现的每个车轮片段相关的特定优惠券代码提前谢谢!

以下是当前代码:

package  
{
    import flash.display.Sprite;    
    import flash.display.Shape;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import com.greensock.TweenMax;

public final class Main extends Sprite
{
    private var speed:Number = 0;
    private var paddles:Vector.<Sprite> = new Vector.<Sprite>();
    private var line:Shape;
    private var lastPaddle:String;

    public final function Main():void
    {
        paddles.push(wheel.p1, wheel.p2, wheel.p3, wheel.p4, wheel.p5, wheel.p6);
        listeners('add');
    }       

    private final function listeners(action:String):void 
    {
        if(action == 'add')
        {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
            stage.addEventListener(MouseEvent.MOUSE_UP, spinWheel);
        }
        else
        {
            stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDraw);
            stage.removeEventListener(MouseEvent.MOUSE_UP, spinWheel);
        }
    }

    private final function startDraw(e:MouseEvent):void
    {
        line = new Shape();
        addChild(line);

        line.graphics.moveTo(mouseX, mouseY);
        line.graphics.lineStyle(5, 0x00000, 0.3);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
    }

    private final function drawLine(e:MouseEvent):void
    {
        line.graphics.lineTo(mouseX, mouseY);
    }

    private final function spinWheel(e:MouseEvent):void
    {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawLine);
        listeners('rm');

        speed = line.height * 0.1; 
        removeChild(line);
        line = null;

        stage.addEventListener(Event.ENTER_FRAME, spin);
    }

    private final function spin(e:Event):void
    {
        /* Rotate Wheel */

        wheel.rotationZ += speed;

        /* Detect Value */

        for(var i:int = 0; i < 6; i++)
        {
            if(indicator.hArea.hitTestObject(paddles[i]))
            {
                lastPaddle = paddles[i].name;
            }
        }

        speed -= 0.1;

        if(speed <= 0)
        {
            stage.removeEventListener(Event.ENTER_FRAME, spin);
            speed = 0;
            run(lastPaddle);
            listeners('null');
        }
    }

    function run(action:String):void
    {
        switch(action)
        {
            case 'p1':
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x644D9B, tintAmount:1}});
                break;
            case 'p2':
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xFFCC00, tintAmount:1}});
                break;
            case 'p3':
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x4789C2, tintAmount:1}});
                break;
            case 'p4':
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF15D5D, tintAmount:1}});
                break;
            case 'p5':
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x90CC6C, tintAmount:1}});
                break;
            case 'p6':
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF29C69, tintAmount:1}});
                break;
            }
        }
    }
}

编辑:: 这是我在wheel.p1.addChild(myText);区域末尾添加function run(action:String):void的建议的更新代码。这是正确的做法还是有更好的方法。我也无法将文本显示在实时版本中。

package 
{
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.MouseEvent;
    import flash.events.Event;
import com.greensock.TweenMax;
import flash.text.TextFormat;
import flash.text.TextField;

public final class Main extends Sprite;
{
    private var speed:Number = 0;
    private var paddles:Vector.<Sprite> = new Vector.<Sprite>();
    private var line:Shape;
    private var lastPaddle:String;

    public final function Main():void
    {
        paddles.push(wheel.p1, wheel.p2, wheel.p3, wheel.p4, wheel.p5, wheel.p6);
        listeners('add');
    }

    private final function listeners(action:String):void
    {
        if (action == 'add')
        {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
            stage.addEventListener(MouseEvent.MOUSE_UP, spinWheel);
        }
        else
        {
            stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDraw);
            stage.removeEventListener(MouseEvent.MOUSE_UP, spinWheel);
        }
    }

    private final function startDraw(e:MouseEvent):void
    {
        line = new Shape();
        addChild(line);

        line.graphics.moveTo(mouseX, mouseY);
        line.graphics.lineStyle(5, 0x00000, 0.3);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
    }

    private final function drawLine(e:MouseEvent):void
    {
        line.graphics.lineTo(mouseX, mouseY);
    }

    private final function spinWheel(e:MouseEvent):void
    {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawLine);
        listeners('rm');

        speed = line.height * 0.1;
        removeChild(line);
        line = null;

        stage.addEventListener(Event.ENTER_FRAME, spin);
    }

    private final function spin(e:Event):void
    {
        /* Rotate Wheel */

        wheel.rotationZ +=  speed;

        /* Detect Value */

        for (var i:int = 0; i < 6; i++)
        {
            if (indicator.hArea.hitTestObject(paddles[i]))
            {
                lastPaddle = paddles[i].name;
            }
        }

        speed -=  0.1;

        if (speed <= 0)
        {
            stage.removeEventListener(Event.ENTER_FRAME, spin);
            speed = 0;
            run(lastPaddle);
            listeners('null');
        }
    }

    function run(action:String):void
    {
        switch (action)
        {
            case 'p1' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x644D9B, tintAmount:1}});
                var myText:TextField = new TextField();
                myText.text = "10per";
                wheel.p1.addChild(myText);

                myText.textColor = 0xFF0000;
                myText.x = 350;
                myText.y = 485;
                break;
            case 'p2' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xFFCC00, tintAmount:1}});
                break;
            case 'p3' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x4789C2, tintAmount:1}});
                break;
            case 'p4' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF15D5D, tintAmount:1}});
                break;

            case 'p5' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x90CC6C, tintAmount:1}});
                break;

            case 'p6' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF29C69, tintAmount:1}});
                break;
           }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是我需要它的最终代码!感谢那些帮助过的人。

package 
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
import flash.text.TextFormat;
import flash.text.TextField;

public final class Main extends Sprite
{
    private var speed:Number = 0;
    private var paddles:Vector.<Sprite> = new Vector.<Sprite>();
    private var line:Shape;
    private var lastPaddle:String;

    public final function Main():void
    {
        paddles.push(wheel.p1, wheel.p2, wheel.p3, wheel.p4, wheel.p5, wheel.p6);
        listeners('add');
    }

    private final function listeners(action:String):void
    {
        if (action == 'add')
        {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
            stage.addEventListener(MouseEvent.MOUSE_UP, spinWheel);
        }
        else
        {
            stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDraw);
            stage.removeEventListener(MouseEvent.MOUSE_UP, spinWheel);
        }
    }

    private final function startDraw(e:MouseEvent):void
    {
        line = new Shape();
        addChild(line);

        line.graphics.moveTo(mouseX, mouseY);
        line.graphics.lineStyle(5, 0x00000, 0.3);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
    }

    private final function drawLine(e:MouseEvent):void
    {
        line.graphics.lineTo(mouseX, mouseY);
    }

    private final function spinWheel(e:MouseEvent):void
    {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawLine);
        listeners('rm');

        speed = line.height * 0.1;
        removeChild(line);
        line = null;

        stage.addEventListener(Event.ENTER_FRAME, spin);
    }

    private final function spin(e:Event):void
    {
        /* Rotate Wheel */

        wheel.rotationZ +=  speed;

        /* Detect Value */

        for (var i:int = 0; i < 6; i++)
        {
            if (indicator.hArea.hitTestObject(paddles[i]))
            {
                lastPaddle = paddles[i].name;
            }
        }

        speed -=  0.1;

        if (speed <= 0)
        {
            stage.removeEventListener(Event.ENTER_FRAME, spin);
            speed = 0;
            run(lastPaddle);
            listeners('null');
        }
    }

    function run(action:String):void
    {
        switch (action)
        {
            case 'p1' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x644D9B, tintAmount:1}});

                    var myFormat:TextFormat = new TextFormat();
                        myFormat.size = 30;


                    var myText:TextField = new TextField();
                        myText.defaultTextFormat = myFormat;
                        myText.text = "10% off your order with coupon code:perc10";
                        addChild(myText);

                        myText.textColor = 0xFFFFFF;
                        myText.width = 624;
                        myText.height = 41;
                        myText.x = 43;
                        myText.y = 457;
                break;
            case 'p2' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xFFCC00, tintAmount:1}});
                    var myFormat:TextFormat = new TextFormat();
                        myFormat.size = 27;


                    var myText:TextField = new TextField();
                        myText.defaultTextFormat = myFormat;
                        myText.text = "$5 off when you spend $20+ with coupon code:5iveoff";
                        addChild(myText);

                        myText.textColor = 0x6D6D6D;
                        myText.width = 676;
                        myText.height = 38;
                        myText.x = 22;
                        myText.y = 459;

                break;
            case 'p3' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x4789C2, tintAmount:1}});
                    var myFormat:TextFormat = new TextFormat();
                        myFormat.size = 30;


                    var myText:TextField = new TextField();
                        myText.defaultTextFormat = myFormat;
                        myText.text = "20% off your order with coupon code:2ZERO";
                        addChild(myText);

                        myText.textColor = 0xFFFFFF;
                        myText.width = 618;
                        myText.height = 41;
                        myText.x = 43;
                        myText.y = 458;


                break;
            case 'p4' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF15D5D, tintAmount:1}});
                    var myFormat:TextFormat = new TextFormat();
                        myFormat.size = 27;


                    var myText:TextField = new TextField();
                        myText.defaultTextFormat = myFormat;
                        myText.text = "$10 off when you spend $30+ with coupon code:10doll";
                        addChild(myText);

                        myText.textColor = 0xFFFFFF;
                        myText.width = 672;
                        myText.height = 38;
                        myText.x = 20;
                        myText.y = 459;

                break;

            case 'p5' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x90CC6C, tintAmount:1}});
                    var myFormat:TextFormat = new TextFormat();
                        myFormat.size = 30;


                    var myText:TextField = new TextField();
                        myText.defaultTextFormat = myFormat;
                        myText.text = "15% off your order with coupon code:fifperc";
                        addChild(myText);

                        myText.textColor = 0xFFFFFF;
                        myText.width = 618;
                        myText.height = 41;
                        myText.x = 50;
                        myText.y = 458;

                break;

            case 'p6' :
                TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF29C69, tintAmount:1}});
                    var myFormat:TextFormat = new TextFormat();
                        myFormat.size = 27;


                    var myText:TextField = new TextField();
                        myText.defaultTextFormat = myFormat;
                        myText.text = "$15 off when you spend $40+ with coupon code:fifdolla";
                        addChild(myText);

                        myText.textColor = 0xFFFFFF;
                        myText.width = 672;
                        myText.height = 38;
                        myText.x = 15;
                        myText.y = 459;;
                break;
        }
    }
}

}