A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: another "if" statement inside an "if" statement???

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    15

    Unhappy another "if" statement inside an "if" statement???

    Hi Everyone,

    I have a scenario where I need to write an if statement inside an if-else statement. But no matter how I write it it keeps giving me a syntax error...
    The syntax error is "1083: syntax error: else is unexpected"

    Actionscript Code:
    var bgMusicURL:URLRequest = new URLRequest("background music.mp3");
    var bgVoiceURL:URLRequest = new URLRequest("bracey Voice.mp3");
    var bgVoice:Sound = new Sound(bgVoiceURL);
    var bgMusic:Sound = new Sound(bgMusicURL);
    var channel1:SoundChannel = new SoundChannel();
    var channel2:SoundChannel = new SoundChannel();
    var musicPosition:Number;
    var voicePosition:Number;
    var musicDimmer:SoundTransform = new SoundTransform(.2, 0);
    var volumeControle:Number = SoundMixer.soundTransform.volume;
    var buttonsArray:Array = new Array(playMusic_btn, playVoice_btn, pause_btn, replay_btn, stop_btn);
    var volumeButtonsArray:Array = new Array(volUp_btn, volDown_btn);

    //Functions
    function buttonsFunction(evt:Event):void {
        if (evt.target.name == playMusic_btn.name) {
            channel1.stop();
            channel1 = bgMusic.play();
        } else if (evt.target.name == playVoice_btn.name) {
            channel2.stop();
            channel1.soundTransform = musicDimmer;
            channel2 = bgVoice.play();
        } else if (evt.target.name == pause_btn.name) {
            musicPosition = channel1.position;
            voicePosition = channel2.position;
            channel1.stop();
            channel2.stop();
        } else if (evt.target.name == replay_btn.name) {
            channel1.stop();
            channel2.stop();
            channel1 = bgMusic.play(musicPosition);
            channel1.soundTransform = musicDimmer;
            channel2 = bgVoice.play(voicePosition);
        } else if (evt.target.name == stop_btn.name) {
            channel1.stop();
            channel2.stop();
        }
    }

    function volumeFunction(evt:Event):void {
        if (evt.target.name == volUp_btn.name) {
            volumeControle += .1;
            if (volumeControle > 1) {        [COLOR="Red"]<<----------this is the if that is giving me trouble [/COLOR]
                volumeControle = 1;}
           
        }
        SoundMixer.soundTransform = new SoundTransform(volumeControle, 0);
        trace("volume up: " + volumeControle);
        //end volume up function
        else if (evt.target.name == volDown_btn.name) {
            volumeControle -= .1;
            if (volumeControle < 0) {                           [COLOR="Red"]<<-----------same thing[/COLOR]
                volumeControle = 0;}
           
        }
        SoundMixer.soundTransform = new SoundTransform(volumeControle, 0);
        trace("volume down: " + volumeControle);
            //end volume down function
    }


    //Listeners
    for (var i:Number = 0; i < buttonsArray.length; i++) {
        buttonsArray[i].addEventListener(MouseEvent.CLICK, buttonsFunction);
    }
    for (var ii:Number = 0; ii < volumeButtonsArray.length; ii++) {
        volumeButtonsArray[ii].addEventListener(MouseEvent.CLICK, volumeFunction);
    }


    No matter how or where I place the braces, it keeps giving me errors. As you can see I am new to AS and any help with some brief explanation would greatly help me out.

    Thank you very much in advance
    Last edited by aeson; 09-22-2010 at 06:46 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I can't even tell what you think the braces mean. That code makes no sense.

    Where did you want these lines to go? In the outer if, or in the else for the outer if, or somewhere else?
    Code:
        SoundMixer.soundTransform = new SoundTransform(volumeControle, 0);
        trace("volume up: " + volumeControle);
        //end volume up
    And why do you have them twice? You know what, since it'll happen at the end of the function regardless, remove the first time.

    In this line and the one like it, what do you think the "{" is doing?
    Code:
            volumeControle += .1; {      <------------starting here
    I think you want this, but it's really impossible to tell.
    Code:
    function volumeFunction(evt:Event):void {
        if (evt.target == volUp_btn) {
            volumeControle += .1;
            if (volumeControle > 1) {
                volumeControle = 1;
            }
            trace("volume up: " + volumeControle);
        } else if (evt.target == volDown_btn) {
            volumeControle -= .1;
            if (volumeControle < 0) {
                volumeControle = 0;
            }
            trace("volume down: " + volumeControle);
        }
        SoundMixer.soundTransform = new SoundTransform(volumeControle, 0);
    }

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    15
    Sorry mate, tried what you suggested but still the same error.

    See I just want to know how you would write another if statement inside an if-else statement, before the else part starts...
    Thats it...
    If you could show me just write a two-line statement for me to see how you would syntax wise correctly write that, I could research it further from there.

    example:

    Actionscript Code:
    if(bla == bla) {
                            <------------how would I write another if statement here?
    }else if (bla == bla){

    }

  4. #4
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    You would just write it the same way!

    if (blabla == blabla)
    {

    if (otherblabber == otherblabber)
    {

    }

    } else if (someblabber == someblabber) {

    etc

    }

  5. #5
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    And you're problem stems from this:

    function volumeFunction(evt:Event):void {
    if (evt.target.name == volUp_btn.name) {
    volumeControle += .1;
    if (volumeControle > 1) { <<----------this is the if that is giving me trouble
    volumeControle = 1;}

    }
    SoundMixer.soundTransform = new SoundTransform(volumeControle, 0);
    trace("volume up: " + volumeControle);
    //end volume up function

    else if (evt.target.name == volDown_btn.name) {
    volumeControle -= .1;
    if (volumeControle < 0) { <<-----------same thing
    volumeControle = 0;}

    }

    The part in bold can't be there. There can't be any commands between the conditions.

    if (something)
    {

    }
    //YOU CAN'T PUT ANYTHING HERE IN BETWEEN CONDITIONS
    else if (somethingelse) {

    }

    So yeah your problem is not "ifs" inside "ifs"

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yeah, what Beathoven said. There's nothing special about if-else. It's a statement just like any other.

    My code still looks correct, though I haven't tried compiling. What line does it say the error is on?

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    15
    line 51 seems to be containing the error according to the compiler

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    And which one is 51? I'm not going to count them.

  9. #9
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by 5TonsOfFlax View Post
    And which one is 51? I'm not going to count them.
    LMAO

  10. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    15
    lol man this is getting embarrassing...
    Line 51 is where else if line starts

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Sounds like Beathoven is right and you didn't remove the offending lines (see my posted code, nothing between "}" and "else").

  12. #12
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    K once more, post your code the way you have it at the moment.

    Don't worry about comments.

  13. #13
    Junior Member
    Join Date
    Sep 2010
    Posts
    15
    I swear you two have taught me more common sense than in this past month of AS learning combined.
    That was exactly where the problem was.
    I cant thank you guys enough. I honestly mean it. Thank you and once again Thank you.

    (PS not a gram of pride left in my bones lol, and sorry for being so annoying)
    Aeson

  14. #14
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Everybody learns, when you're in the heat of the moment freaking out about your code (trust me everybody does), you often overlook what's being said to you. Best is to stay calm.

    Soon you will be posting questions and answering yourself before anybody has the chance to reply lol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center