简体   繁体   English

闪光按钮链接和切换

[英]Flash Button Linking and Toggle

Clicking one of 3 buttons shows price output in total.text each button shows a different price. 单击3个按钮之一显示总价格输出。文本每个按钮显示不同的价格。

code currently used for each button: 当前用于每个按钮的代码:

1    btn1.addEventListener(MouseEvent.CLICK, myButtonClick);
2    
3    function myButtonClick(ev:MouseEvent):void
4    {
5    var a:Number = Number(99) * Number(1.2);
6    total.text = String(a);
7    }
8
9   btn2.addEventListener(MouseEvent.CLICK, myButtonClick);
10   
11    function myButtonClick(ev:MouseEvent):void
12    {
13    var b:Number = Number(99) * Number(1.4);
14    total.text = String(b);
15    }
16
17  btn3.addEventListener(MouseEvent.CLICK, myButtonClick);
18   
19    function myButtonClick(ev:MouseEvent):void
20    {
21    var c:Number = Number(99) * Number(1.6);
22    total.text = String(c);
23    }
24

it works fine, but i really want to Add the value of button 2 and/or 3 if toggled to the value of button 1 and display it in total.text 它工作正常,但我真的想添加按钮2和/或3的值(如果切换到按钮1的值并在total.text中显示)

eventually, i am trying to get choice of 5 buttons to toggle 1 of. 最终,我试图获得5个按钮的选择来切换1。 that toggle will set the initial Number(99) value with something..99 or what not. 该切换会将初始Number(99)值设置为something..99或其他值。 and then if i toggle 1 button in a second set of 5 buttons, it will set value in the second Number(1.2). 然后,如果我在第二组5个按钮中切换1个按钮,它将在第二个Number(1.2)中设置值。 and from there. 然后从那里。 the two toggled btn2 and btn3 would multiply total number in total.text. 这两个切换后的btn2和btn3将使total.text中的总数相乘。

i see where i can make a whole bunch of buttons with values calculated. 我看到我可以在其中制造一堆带有计算值的按钮。 but im sure there is a cleaner way to do that. 但我肯定有更清洁的方法可以做到这一点。 i am a total novice in flash and just kinda looking for a direction here. 我是Flash方面的新手,只是在这里寻找方向。 it would be awesome. 这一定非常棒。

Edit: Did a little research. 编辑:做了一点研究。 hope I'm heading in right direction 希望我朝着正确的方向前进

ok, added variable number like 好的,添加了可变数字,例如

var modifier1:Number = 99;
var modifier2:Number = 1.2;

then called on those from the function like 然后从像

var a:Number = Number(modifier1) * Number(modifier2);

now researching on how to make the modifier1 actually link up to toggle state of a button and assign it a value like 99 for toggled and 1 for not. 现在研究如何使modifier1实际链接到按钮的切换状态,并为其分配一个值,例如99表示已切换,1表示未设置。

I'd recommend creating a small class for all your buttons, it shall contain its value . 我建议为您的所有按钮创建一个小类,其中应包含其value You also need an object that will contain a list of these buttons, let's call it Application . 您还需要一个包含这些按钮列表的对象,我们将其称为Application When user clicks on Button1, it will set a field inside Application indicating the toggle - product of all values or just the button that was pressed. 当用户单击Button1时,它将在Application内设置一个字段,指示切换-所有值的乘积或仅是按下的按钮。 This then sets the actual text to whatever was passed (I've called it valueText) . 然后,将实际文本设置为传递的任何内容(我将其称为valueText)

http://puu.sh/d1hP7/e25228352b.png http://puu.sh/d1hP7/e25228352b.png

Here's some quick code, I haven't tested if it actually works, but It should be pretty easy to follow: 这是一些快速代码,我尚未测试它是否确实有效,但是应该很容易理解:

class Button {

    private var app:Application;
    public var value:Number;

    public function Button(app:Application, value:Number) {
        this.app = app;
        this.value = value;
        this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
    }
    private function mouseDown(e:MouseEvent):void {
        app.displayText(value);
    }
}

class Application {

    private var buttonList:Vector.<Button> = new Vector.<Button>();
    public var isToggleOn:Boolean = false;

    public function Application() {
        buttonList.push(new Button(this, 0.98));
        buttonList.push(new Button(this, 0.75));
        buttonList.push(new Button(this, 0.88));
    }

    public function getProduct():Number {
        var sum:Number = 0;
        for(var button:Button in buttonList) {
            sum += button.value;
        }
        return sum;
    }

    public function displayText(value:Number):void {
        valueText.text = isToggleOn ? getProduct() : value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM