0

Let me set the stage because it's too much code to post everything:

I have a Main.as that is setting up my SoundController.as so I can trigger sounds from any other class (SoundController.as has all the functions needed to call all my sounds as needed)

I have a ControlPanel.as that can access these sounds by using docRef.soundControl.laserFire or whatever other function name I want, and in this case the laserFire sound would trigger once.

So here is my question. I want to let this laser sound effect finish playing before you can fire another laser. So in SoundController.as I've set up the following pieces of code:

private var _laserPlaying:Boolean = false;

internal function laserFire():void {
    _sfxChannel = _laser.play(25);
    _laserPlaying=true;
    _sfxChannel.addEventListener(Event.SOUND_COMPLETE, laserFinished);
}

internal function laserFinished(event:Event):void {
    _sfxChannel.removeEventListener(Event.SOUND_COMPLETE, laserFinished);
    _laserPlaying=false;
}

public function get laserPlaying():Boolean {
    return _laserPlaying;
}

public function set laserPlaying(value:Boolean):void {
    _laserPlaying = value;
}

Now in my ControlPanel class in the enterFrameHandler function I want to do an

if (docRef.soundControl.laserPlaying()==false)

or something to that effect so I can check when the sound is done and allow the player to once again press the trigger to fire the laser. So far any variant I've tried on this either gives me an error (in this case 1195; Attempted access of inaccessible method laserPlaying through a reference with static type SoundController) or it actually compiles but after firing the first laser shot it never allows the trigger to be pressed again. So I'm obviously doing something wrong and am hoping someone can help.

Let me just state that the laser sound is playing just fine that first time, so don't worry about all the code I'm not bothering to show to make that portion of the code work. However, if more info is needed to understand how I'm making anything work just let me know. And Thanks in advance!

FlashNoob468
  • 175
  • 1
  • 17
  • Just a thought, without diving into your code: Your soundChannel has a read-only ‘position’ property. When you attempt to fire a laser just check the channel position. If it's greater than 0 (which would mean that it’s playing, unless you had stopped it midway) don’t let your function fire. – Craig Jan 22 '17 at 05:27
  • Use "global" variable via static class member as I suggested here: http://stackoverflow.com/a/41651064/4687633 – Organis Jan 22 '17 at 05:33
  • @Craig unless I'm misunderstanding, that sounds like another way to do what I'm trying to do, however, that still only works in the SoundController class. I still need a way to pass that info over to my ControlPanel class which is where I'm trying to do that "if" check. – FlashNoob468 Jan 22 '17 at 05:46
  • @Organis I'm not understanding how that would work in my instance. I thought I was creating something that I could access globally with the getter and setter being a public function? Are you saying just change my _laserPlaying variable to static public var _laserPlaying:Boolean = false; ? – FlashNoob468 Jan 22 '17 at 05:53
  • Static public class members are exactly available globally through all your application. Normally I would recommend to organize your application in terms of hierarchy, but that involves a lot of explaining. – Organis Jan 22 '17 at 11:10

2 Answers2

0

If your soundController AND ControlPanel are instantiated in Main:

handle the firing event, in ControlPanel, like this:

if(MovieClip(parent).soundController.soundChannel.position==0)
 {
    MovieClip(parent).soundController.laserfire();
 }else{do nothing;}

Of course use proper instance names.

If this doesn't work you'll have to make your code a little easier to understand.

Craig
  • 814
  • 1
  • 6
  • 9
0

Sorry for wasting everyone's time. I ended up figuring out what I needed to do to make this work. This probably won't be of much use to anyone else since my setup was probably unique to my layout and not something that anyone else will try, but here is what I did anyway.

In my ControlPanel.as I added the following code:

private var _soundController:SoundController;

And then I have a function that waits for the ControlPanel to be added to the stage and once that occurs I fired:

_soundController = new SoundController(docRef);

Now by adding those I was able to simply call:

if(!_soundController.laserPlaying) { do stuff; }

It now seems to wait for laserPlaying to be false and then moves on as intended.

FlashNoob468
  • 175
  • 1
  • 17
  • If it really won't be of use to anyone there really is nothing wrong with deleting your question. I've done that a few times as I don't want to add to the murk that is the Internet. – Neal Davis Jan 23 '17 at 05:21