2012-04-27 79 views
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); 

+0

基元(數字,int等)都是按值傳遞的。你試圖獲得的是通過參考。爲了得到這個,你需要傳入一個對象。 – meddlingwithfire 2012-04-27 22:15:59

回答

0

好了,於是我研究的對象類,並發現我可以通過使物體在函數引用變量。這裏是更新的工作腳本:

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); 
相關問題