如何使用函数参数来引用变量?

时间:2012-04-27 21:59:11

标签: actionscript-3 function variables parameters refer

对不起,如果这个问题有点模糊,但这最近让我疯了。它不是太复杂,但我想要做的就是变量' targetVariable '受公式影响。实际问题在于引用的变量是' masterVolume '在这种情况下,不会受到公式的影响,而是 targetVariable '代替。我运行' makeSlider '函数在脚本的底部。这是代码:


var masterVolume:Number = 0;
var panning:Number = 0;

function makeSlider(sliderType, X, Y, targetVariable) {

    var sliderHandle:MovieClip = new sliderType();

    addChild(sliderHandle);
    sliderHandle.x = X;
    sliderHandle.y = Y;

    var dragging:Boolean = false;

    stage.addEventListener(Event.ENTER_FRAME, updateSlider);

    function updateSlider(e:Event):void {
        panning = (mouseX/(stage.stageWidth/2))-1;

        targetVariable = ((sliderHandle.x-bar.x)/bar.width);

        output.text = masterVolume.toString();
        if (dragging == true && mouseX >= bar.x && mouseX <= (bar.x + bar.width)) {
            sliderHandle.x = mouseX;
        }
    }
    sliderHandle.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);

    function beginDrag(e:MouseEvent):void {
        dragging = true;
    }

    stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);

    function endDrag(e:MouseEvent):void {
        dragging = false;
    }
}

function playSound(target, intensity:Number, pan:Number) {
    var sound:Sound = new target();
    var transformer:SoundTransform = new SoundTransform(intensity, pan);
    var channel:SoundChannel = new SoundChannel();

    channel=sound.play();

    channel.soundTransform = transformer;
}

stage.addEventListener(MouseEvent.MOUSE_DOWN, make);

function make(e:MouseEvent):void {
    playSound(test, masterVolume, panning);
}

makeSlider(SliderHandle, bar.x, bar.y, masterVolume);

1 个答案:

答案 0 :(得分:0)

好的,所以我研究了Object类,发现我可以通过使变量成为函数中的对象来引用变量。这是更新的工作脚本:

var panning:Number = 0;
var masterVolume:Number = 0;

function makeSlider(sliderType, barType, soundType, hitBoxScale:Number, X, Y, targetVariable) {

    var reference:Object = { targetVariable: 0 };

    var slider:MovieClip = new sliderType;
    var newBar:MovieClip = new barType;

    addChild(newBar);
    newBar.x = X;
    newBar.y = Y;
    addChild(slider);
    slider.x = X;
    slider.y = Y;

    var dragging:Boolean = false;

    stage.addEventListener(Event.ENTER_FRAME, updateSlider);

    function updateSlider(e:Event):void {
        panning = (mouseX/(stage.stageWidth/2))-1;
        reference.targetVariable = (slider.x-newBar.x)/newBar.width;
        if (dragging == true && mouseX >= newBar.x && mouseX <= (newBar.x + newBar.width)) {
            slider.x = mouseX;
        }
        if (reference.targetVariable <= 0.01) {
            output.text = ("None");
        }
        if (reference.targetVariable >= 0.99) {
            output.text = ("Max");
        }
        if (reference.targetVariable > 0.01 && reference.targetVariable < 0.99) {
            output.text = (Math.round((reference.targetVariable*100))+"%").toString();
        }
    }
    stage.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);

    function beginDrag(e:MouseEvent):void {
        if (mouseY >= newBar.y-hitBoxScale && mouseY <= (newBar.y + newBar.height)+hitBoxScale) {
            dragging = true;
        }
    }
    slider.addEventListener(MouseEvent.MOUSE_DOWN, beginDragFromSlider);

    function beginDragFromSlider(e:MouseEvent):void {
        dragging = true;
    }

    stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);

    function endDrag(e:MouseEvent):void {
        if (dragging == true) {
            playSound(soundType, reference.targetVariable, 0);
        }
        dragging = false;
    }
    stage.addEventListener(MouseEvent.MOUSE_DOWN, make);

    function make(e:MouseEvent):void {
        if (dragging == false) {
            playSound(test, reference.targetVariable, panning);
        }
    }
}

function playSound(target, intensity:Number, pan:Number) {
    var sound:Sound = new target();
    var transformer:SoundTransform = new SoundTransform(intensity, pan);
    var channel:SoundChannel = new SoundChannel();

    channel=sound.play();

    channel.soundTransform = transformer;
}

makeSlider(defaultSlider, defaultBar, volumeIndicator, 10, 134, 211, masterVolume);