|
-
[RESOLVED] if statement nested within another if statement
I accidently marked my other post resolved so I reposted with only the necessary code to make it easier to read. This is all of the code regarding my problem. Basically my second if statement inside the pwrbtn function isnt working. The lines
MovieClip(parent).pwrframe();
pwrcomp=true;
trace(pwrcomp);
are still running even though the boolean condition of the statement is set to true. Below is the code that is involved. Anyone who can take a second to look at this I would be greatly appreciated. I have stared at this for hours and haven't found the error.
Movie Clip
var pwrcomp:Boolean = false;
inv_power_btn.addEventListener(MouseEvent.CLICK,pw rbtn);
function pwrbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
if(pwrcomp == false)
{
MovieClip(parent).pwrframe();
pwrcomp=true;
trace(pwrcomp);
}
}
else
{
gotoAndStop(1);
pwr=false;
}
}
Timeline
function pwrframe()
{
gotoAndStop("signalframe");
trace("frame2");
}
-
Format your code.
If your various frame transitions result in this frame running again, the variable will be reset to false.
-
I was under the impression that if the variable is declared up at the top that it would be left alone. When the frame transitions is there the possibility that it would move back to the top of the code? Also when you say format what do you mean exactly? thanks for all your help today.
-
By 'format', I mean put your code in [ code ] or [ php ] tags so that it's readable.
When the frame script is executed again on entering the frame, it is all executed. It looks like you need the pwrcomp variable to be in a scope which is accessible to multiple frames. You can declare it as a dynamic property in a different frame
Code:
this.pwrcomp = false;
and then it will be a property on the main movie rather than a variable local to that one frame.
-
ok here is the code in format sorry about the readability issues.
Movie Clip
Code:
var pwrcomp:Boolean = false;
inv_power_btn.addEventListener(MouseEvent.CLICK,pwrbtn);
function pwrbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
if(pwrcomp == false)
{
MovieClip(parent).pwrframe();
pwrcomp=true;
trace(pwrcomp);
}
}
else
{
gotoAndStop(1);
pwr=false;
}
}
Main Timeline
Code:
function pwrframe()
{
gotoAndStop("signalframe");
trace("frame2");
}
-
Im afraid I don't quite follow you on the dynamic frame advice. I haven't programmed since 04 and got kind of thrown back into it. Bit rusty. Btw at 5pm cst I will be leaving work and will not be back till Friday at 8am cst so if i dont reply check back sometime friday if you get a chance. I appreciate all of your help in this.
-
I tried using the
Code:
this.pwrcomp = false;
inside of a different frame but it still resets when i leave the function and the code starts over. Is there a way I can declare the variable where it will not reinitialize when the code starts over? Here is what I have as of now:
Main Timeline
Code:
gotoAndStop("pwrframe");
function pwrframe()
{
gotoAndStop("signalframe");
trace("frame2");
}
function signalframe()
{
gotoAndStop("numberframe");
trace("frame3");
}
Movieclip
Code:
trace(pwrcomp);
var pwr:Boolean=false;
var btnnum:Number = -1;
var sendnum:Number = 10;
var desnum:Number;
//Functions activated by clicking buttons with mouse.
inv_power_btn.addEventListener(MouseEvent.CLICK,pwrbtn);
inv_lessthan_btn.addEventListener(MouseEvent.CLICK,signalDbtn);
inv_greaterthan_btn.addEventListener(MouseEvent.CLICK,signalUbtn);
inv_0_btn.addEventListener(MouseEvent.CLICK,zerobtn);
inv_1_btn.addEventListener(MouseEvent.CLICK,onebtn);
inv_2_btn.addEventListener(MouseEvent.CLICK,twobtn);
inv_3_btn.addEventListener(MouseEvent.CLICK,threebtn);
inv_4_btn.addEventListener(MouseEvent.CLICK,fourbtn);
inv_5_btn.addEventListener(MouseEvent.CLICK,fivebtn);
inv_6_btn.addEventListener(MouseEvent.CLICK,sixbtn);
inv_7_btn.addEventListener(MouseEvent.CLICK,sevenbtn);
inv_8_btn.addEventListener(MouseEvent.CLICK,eightbtn);
inv_9_btn.addEventListener(MouseEvent.CLICK,ninebtn);
inv_send_btn.addEventListener(MouseEvent.CLICK,sendbtn);
inv_clear_btn.addEventListener(MouseEvent.CLICK,clearbtn);
//Used to show power off screen at start.
gotoAndStop(1);
//Functions that activate the various screens.
function pwrbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
if(pwrcomp==false)
{
MovieClip(parent).pwrframe();
pwrcomp=true;
trace(pwrcomp);
}
}
else
{
gotoAndStop(1);
pwr=false;
}
}
function signalDbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(23);
MovieClip(parent).signalframe();
}
}
function signalUbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(24);
MovieClip(parent).signalframe();
}
}
function clearbtn(event:Event)
{
gotoAndStop(2);
pwr=true;
btnnum=-1;
}
function sendbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
desnum=btnnum+sendnum;
switch(desnum)
{
case 9:
gotoAndStop(25);
break;
case 10:
gotoAndStop(13);
break;
case 11:
gotoAndStop(14);
break;
case 12:
gotoAndStop(15);
break;
case 13:
gotoAndStop(16);
break;
case 14:
gotoAndStop(17);
break;
case 15:
gotoAndStop(18);
break;
case 16:
gotoAndStop(19);
break;
case 17:
gotoAndStop(20);
break;
case 18:
gotoAndStop(21);
break;
case 19:
gotoAndStop(22);
break;
}
}
}
function zerobtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(3);
btnnum=0;
}
}
function onebtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(4);
btnnum=1;
}
}
function twobtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(5);
btnnum=2;
}
}
function threebtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(6);
btnnum=3;
}
}
function fourbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(7);
btnnum=4;
}
}
function fivebtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(8);
btnnum=5;
}
}
function sixbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(9);
btnnum=6;
}
}
function sevenbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(10);
btnnum=7;
}
}
function eightbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(11);
btnnum=8;
}
}
function ninebtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
}
else
{
gotoAndStop(12);
btnnum=9;
}
}
Alternate frame within movie where i tried the above noted change
Code:
var pwrcomp:Boolean;
this.pwrcomp = false;
trace("dynamic")
trace(pwrcomp)
Thanks again for the assistance. I am learning quite a bit lol.
-
When you use the dynamic property pwrcomp you should NOT declare it as a variable. Remove the var pwrcomp line. When you have both, you actually have two different things. There's the pwrcomp property on the root, and there's a frame local variable named pwrcomp. If both are in scope, then an unqualified reference will refer to the variable.
I don't understand the sequence of your code. Are the Main timeline and MovieClip different? If they are, what is the sequence of frames (and what code is in each) in the movie in which you want to declare and use pwrcomp?
-
When i took that line out I got 1020 errors stating that pwrcomp was undefined in every line that it was used throughout the fla file. The movie clip was originally another fla that I inserted into the main timeline of this project as a movie clip. the program is basically a training program on educational clickers. The movie clip is a digitized clicker. The main timeline is a screen providing instructions. For example all this pwr stuff is code related to a screen telling the user to press the power button and when they do press the powerbutton on the clicker it advances the main timeline by a frame to the next activity. The movie clip itself also has its own frames which are used to create a different display screen on the clicker depending on what button you push.
-
You shouldn't have to use "this.pwrcomp" if it's already set, but it couldn't hurt to try on one line and see if that error goes away for that line. You are executing the frame which sets pwrcomp before the frames which try to use it, right?
This is why I hate frame coding.
-
all of my code is set on frame1 of the individual parts so I assume they execute at approximately the same time. Is there a way I can declare the boolean pwrcomp in a manner that it's value is not changed when the code cycles? like maybe externally of the fla file? What is the alternative to frame coding? LOL sorry, im a newb.
-
You said that there are only two parts: Main and the clicker clip. If all your code in the clicker clip is on frame 1, then the line which initializes pwrcomp is on that frame too? I was trying to suggest having at least two code frames in that movie: One which initializes stuff and is only run once, and one which is returned to when looping.
You can set the pwrcomp property of the clicker clip from the main timeline. That should get the initialization off the clicker clip.
The alternative to frame coding is classes. When you have a class, the methods and variables are part of that class, not local to frames.
-
if I created classes in an external as file located within the same folder would they avoid the initialization that is resetting everything? and how would that work exactly?
-
Yes, if most of your code was in the class and only some logic attached to buttons or the like was on frames it would avoid the resetting. First though, try setting the pwrcomp property of the clicker clip from the main timeline. That should also work.
If you wanted to create a class for your clicker, it should extend MovieClip, because you have multiple frames. Put any variables or functions that need to be accessible throughout the movie (or from another instance such as Main) into the class. Here's a sketch of some variables and methods you might want to include:
Code:
package {
//add imports here as necessary
public class Clicker extends MovieClip {
public var _pwrcomp:Boolean;
public function Clicker(){
pwrcomp = false;
}
public function set pwrcomp(b:Boolean):void{
_pwrcomp = b;
if (b){
dispatchEvent(new Event("poweron"));
}else{
dispatchEvent(new Event("poweroff"));
}
}
public function get pwrcomp():Boolean{
return _pwrcomp;
}
}
}
Now, you can keep most of your code in frames or stick it in the class. Try to determine whether each function applies to the whole Clicker, or just that frame.
With the dispatchEvent statements above, your Main instance can listen for poweron or poweroff and react accordingly.
pseudocode for Main
Code:
//assumes clicker is a Clicker placed through the IDE
clicker.addEventListener("poweron", powerPressed);
function powerPressed(e:Event):void{
trace("power's on!");
//do useful stuff here.
}
Last edited by 5TonsOfFlax; 10-30-2009 at 12:26 PM.
Reason: added getter
-
Ok so I built an external as file to store a variable in and it is almost working except I can't get the initial value to equal false so thus the nested loop of my pwrbutton isnt starting the first time. (From on end of the problem to the other it seems lol) I placed a trace in the top of the movie clip code to see what the initial value was and it traced the following: function Function() {} Here is the code that pertains to it. I bolded and colored the code in question to make it easier to read.
movie clip
Code:
var pwrcomp:globals = new globals();
var pwr:Boolean=false;
var btnnum:Number = -1;
var sendnum:Number = 10;
var desnum:Number;
trace(pwrcomp.GetPwrcomp)
//Functions activated by clicking buttons with mouse.
inv_power_btn.addEventListener(MouseEvent.CLICK,pwrbtn);
inv_lessthan_btn.addEventListener(MouseEvent.CLICK,signalDbtn);
inv_greaterthan_btn.addEventListener(MouseEvent.CLICK,signalUbtn);
inv_0_btn.addEventListener(MouseEvent.CLICK,zerobtn);
inv_1_btn.addEventListener(MouseEvent.CLICK,onebtn);
inv_2_btn.addEventListener(MouseEvent.CLICK,twobtn);
inv_3_btn.addEventListener(MouseEvent.CLICK,threebtn);
inv_4_btn.addEventListener(MouseEvent.CLICK,fourbtn);
inv_5_btn.addEventListener(MouseEvent.CLICK,fivebtn);
inv_6_btn.addEventListener(MouseEvent.CLICK,sixbtn);
inv_7_btn.addEventListener(MouseEvent.CLICK,sevenbtn);
inv_8_btn.addEventListener(MouseEvent.CLICK,eightbtn);
inv_9_btn.addEventListener(MouseEvent.CLICK,ninebtn);
inv_send_btn.addEventListener(MouseEvent.CLICK,sendbtn);
inv_clear_btn.addEventListener(MouseEvent.CLICK,clearbtn);
//Used to show power off screen at start.
gotoAndStop(1);
//Functions that activate the various screens.
function pwrbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
if(pwrcomp.GetPwrcomp==false)
{
MovieClip(parent).pwrframe();
pwrcomp.SetPwrcomp(true);
trace(pwrcomp.GetPwrcomp)
}
}
else
{
gotoAndStop(1);
pwr=false;
}
}
External AS file named globals
Code:
package
{
public class globals
{
public var testvar:Boolean = false;
public function globals()
{
}
public function SetPwrcomp(b:Boolean):void
{
testvar = b;
}
public function GetPwrcomp():Boolean
{
return testvar;
}
}
}
and here is the Main Timeline
Code:
gotoAndStop("pwrframe");
function pwrframe()
{
gotoAndStop("signalframe");
trace("frame2");
}
-
Since you are using plain functions instead of getter/setters, you have to use () to actually invoke the function. Otherwise, you're just referring to the function itself.
Also, since globals properties and methods are not static, they won't actually work as globals. You'd have to get the same instance of globals in order to get the values back out.
Code:
var pwrcomp:globals = new globals();
var pwr:Boolean=false;
var btnnum:Number = -1;
var sendnum:Number = 10;
var desnum:Number;
trace(pwrcomp.GetPwrcomp())
//Functions activated by clicking buttons with mouse.
inv_power_btn.addEventListener(MouseEvent.CLICK,pwrbtn);
inv_lessthan_btn.addEventListener(MouseEvent.CLICK,signalDbtn);
inv_greaterthan_btn.addEventListener(MouseEvent.CLICK,signalUbtn);
inv_0_btn.addEventListener(MouseEvent.CLICK,zerobtn);
inv_1_btn.addEventListener(MouseEvent.CLICK,onebtn);
inv_2_btn.addEventListener(MouseEvent.CLICK,twobtn);
inv_3_btn.addEventListener(MouseEvent.CLICK,threebtn);
inv_4_btn.addEventListener(MouseEvent.CLICK,fourbtn);
inv_5_btn.addEventListener(MouseEvent.CLICK,fivebtn);
inv_6_btn.addEventListener(MouseEvent.CLICK,sixbtn);
inv_7_btn.addEventListener(MouseEvent.CLICK,sevenbtn);
inv_8_btn.addEventListener(MouseEvent.CLICK,eightbtn);
inv_9_btn.addEventListener(MouseEvent.CLICK,ninebtn);
inv_send_btn.addEventListener(MouseEvent.CLICK,sendbtn);
inv_clear_btn.addEventListener(MouseEvent.CLICK,clearbtn);
//Used to show power off screen at start.
gotoAndStop(1);
//Functions that activate the various screens.
function pwrbtn(event:Event)
{
if(pwr == false)
{
gotoAndStop(2);
pwr=true;
if(pwrcomp.GetPwrcomp()==false)
{
MovieClip(parent).pwrframe();
pwrcomp.SetPwrcomp(true);
trace(pwrcomp.GetPwrcomp())
}
}
else
{
gotoAndStop(1);
pwr=false;
}
}
-
This piece of code is initializing and so I am getting the same results as earlier today except the code is in a different place now lol. Is there any way to get:
var pwrcomp:globals = new globals();
in a location that will not reinitialize?
Rather than scripting I need to just allocate the memory into something like a header.
-
That's the exact same problem you started with. The same suggestions I posted before still apply. Here's another approach though:
Code:
package
{
public class Globals
{
private static var _instance:Globals
public var testvar:Boolean = false;
public function Globals()
{
}
public function SetPwrcomp(b:Boolean):void
{
testvar = b;
}
public function GetPwrcomp():Boolean
{
return testvar;
}
public static function getInstance():Globals{
if (_instance == null){
_instance = new Globals();
}
return _instance;
}
}
}
Code:
var pwrcomp:Globals = Globals.getInstance();
Do not use "new Globals()" outside of the Globals class.
-
That _instance code in the above post fixed it! Thank you so much for the help. That made my Monday!
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
|