0

对不起,如果这个问题有点模糊,但这最近让我发疯了。这没什么太复杂的,但我想做的就是让变量“ 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);

4

1 回答 1

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);
于 2012-04-28T12:07:12.790 回答