0

So I have a scene which is an options menu; in the scene I have a volume slider; it works fine (I can slide it and the volume would go down/up), but when I leave the scene, it gives me an error (only happens if I play with the slider):

TypeError: Error #1009: Cannot access a property or method of a null object reference.

And it points at:

var myVolume:Number=V_Slider.V_Knob.x/mySliderLength;

and when I re-enter, the volume slider goes back to its original position. I'm very new to Flash and AS3, so any help I can get is very much appreciated.

Here is the code for the volume slider:

    var dragging:Boolean=false; 
    var mySliderLength:uint=240;   
    var boundingBox:Rectangle=new Rectangle(0,0,mySliderLength,0); 

V_Slider.V_Knob.addEventListener(MouseEvent.MOUSE_DOWN, dragKnob);    
stage.addEventListener(MouseEvent.MOUSE_UP, releaseKnob);    
V_Slider.V_Knob.buttonMode=true;

function dragKnob(myEvent:Event):void { 
    V_Slider.V_Knob.startDrag(false, boundingBox); 
    dragging=true; 
    V_Slider.V_Knob.addEventListener(Event.ENTER_FRAME, adjustVolume);    
}  

function releaseKnob(myEvent:Event):void { 
    if (dragging) { 
        V_Slider.V_Knob.stopDrag(); 
        dragging=false; 
    }   
}  

function adjustVolume(myEvent:Event):void { 
    var myVolume:Number=V_Slider.V_Knob.x/mySliderLength;
    var myTransform:SoundTransform=new SoundTransform(myVolume); 
    if (BGM_SC!=null) { 
        BGM_SC.soundTransform=myTransform; 
    }    
}  
Armali
  • 18,255
  • 14
  • 57
  • 171
Penafore
  • 1
  • 1

1 Answers1

0

It is a common problem man you can look for that. When u change you scene you need to remove your Events and when you come back to that scene u need to add them again. In this case you u can use this

function workEvents(action:String):void
{
  if(action == "add")
  {
    V_Slider.V_Knob.addEventListener(MouseEvent.MOUSE_DOWN, dragKnob);    
    stage.addEventListener(MouseEvent.MOUSE_UP, releaseKnob);   
  } 
  else  
  {
    V_Slider.V_Knob.removeEventListener(MouseEvent.MOUSE_DOWN, dragKnob);    
    stage.removeEventListener(MouseEvent.MOUSE_UP, releaseKnob);     
  }  
}

when u use that gotoAndStop/gotoAndPlay(1,"Scene 2");
also use this: workEvents("rm");
this will remove your events and that problem should end :)

I hope it helps :))

halilcakar
  • 1,628
  • 1
  • 12
  • 18
  • Hmm, it still pops up, I added a trace on the workEvents function and it shows up but the error still pops up(it pops up every like 20 times per second) – Penafore Aug 21 '15 at 12:03
  • Hmm, You probably move all adding and removing events (on your Scene) inside workEvents() function – halilcakar Aug 21 '15 at 15:00
  • I mean if u have other addEventListener's/removeEventListener's move them inside workEvents() function on your Scene – halilcakar Aug 21 '15 at 15:17