-
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.
-
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);
}
-
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){
}
-
You would just write it the same way!
if (blabla == blabla)
{
if (otherblabber == otherblabber)
{
}
} else if (someblabber == someblabber) {
etc
}
-
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"
-
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?
-
line 51 seems to be containing the error according to the compiler
-
And which one is 51? I'm not going to count them.
-
Originally Posted by 5TonsOfFlax
And which one is 51? I'm not going to count them.
LMAO
-
lol man this is getting embarrassing...
Line 51 is where else if line starts
-
Sounds like Beathoven is right and you didn't remove the offending lines (see my posted code, nothing between "}" and "else").
-
K once more, post your code the way you have it at the moment.
Don't worry about comments.
-
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
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|