Action when certain buttons in a button group are pressed

GordigGordig Posts: 10,053

Here's the code I have:

function hasfad() {			for (i=0; i<swtch.length; i++) {		var fad = opt.indexOf(Scene.getNode(switches[i]).name);				if (swtch[i].down && fad == 0) {			loint.enabled = true;			hiint.enabled = true;		}		else {			loint.enabled = false;			hiint.enabled = false;		}	}}

The script starts with an array of names for valid selections (I'm designing this script to work only with products I have designed for this purpose), and then a set of radio buttons  within a button group, corresponding to the products in the scene that have the valid names. All that works fine. The problem when I'm testing this script is that fad = 0 for the first and fourth buttons, but the script only enables the two properties when the fourth button is pressed, not the first one. I'm having trouble grasping the logic behind that. If I PRINT fad within the if condition, it prints for both buttons 1 and 4, but not for buttons 2 and 3, so clearly the conditions are being met.

Also, is there a smarter way to indicate which button is selected? I haven't been able to get anywhere with the  [buttongroup].checkedButton, and there's no documentation for it. If I print it, I get:

function checkedButton() {	[native code]}

Every other operation parameter (name, index, etc.) is undefined. Does anyone know how this method works? I've also looked into putting the switch name/id/whatever into the function call, but I can't seem to get a handle on how that actually works.

I really hate asking so many questions, but...well, you've seen the documentation.

Post edited by Gordig on

Comments

  • GordigGordig Posts: 10,053

    Some additional wierdness: if I remove "&& fad == 0" from the if conditions, it will print correctly for every button I press, but still only enable the parameters on the fourth button. This is a stumper.

  • GordigGordig Posts: 10,053

    Well, I solved the problem by nesting if(fad==0) within the first if loop, which is an...unsatisfying resolution. Still welcoming responses to my other questions, though.

  • Richard HaseltineRichard Haseltine Posts: 100,842

    You have at least two global variables in there, which may cause confusion - why not pass them as parameters? Also, why are you declaring fad as a variable inside the loop? Ideally

    fad = opt.indexOf(Scene.getNode(switches[i]).name);

    should be split down over several lines - there's a lot of stuff happening in that line which is not goign to help troubleshooting.

    As for the condition, swtch[i].down && fad == 0 is two sets of logical operators ("and" and "equality") so I think it may be taking the first pair ( swtch[i].down && fad ) as a lofgical operation, then comparing the result to 0 and taking the result of that as the switch - bracketing the second two causes that pair to be evaluated first, then compared to the first.

Sign In or Register to comment.