dcsimg
A Flash Developer Resource Site

Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 53

Thread: if then statements confusion

  1. #21
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In the case of typing one letter at a time, processToken can throw an error because the match line actually returns null if there's no match rather than an empty array.
    Code:
      if (!embeddedNums || (embeddedNums.length < 2)){
        return false;
      }
    If that doesn't fix it, try to find out which line has the error.

  2. #22
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    didnt fix the problem but like i said i don't think it will be a huge deal because when this program is completed, I will only ever be sending full blocks (or whatever you would call one of these --> "s30+2.") and i will never be typing them character by character in that text box. in fact that text box is only for testing and will be eliminated when the program is finished...

    is ther a way to figure out which line of code has an error though? i tried debugin the file but wasnt sure how to do it (i hit ctrl-shift-enter but didnt know what to do when it opened it up in debugging mode).

  3. #23
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You should be able to just play with it as normal in debug mode, and when the error occurs, the error message will tell you what line.

    If that doesn't work, you can always put in traces between lines to determine the last one that happens before the error. It's a small method, so that's not too terrible.

    The whole idea of using the buffer was to prevent errors when only part of a message is delivered, so I would like to solve the issue.

  4. #24
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    ok. in debug mode it says
    Code:
    MainTimeline::frame1:37
    i assume that means line 37?
    if it is line 37, that would be the bolded part in the following:
    Code:
    var ONarray:Array=new Array("s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11","s12","s13","s14",							"s15","s16","s17","s18","s19","s20","s21","s22","s23","s24","s25","s26","s27",
    							"s28","s29","s30","s31");
    var onLastDown:MovieClip=null;
    var socketBuffer:String = "";
    s.addEventListener(ProgressEvent.SOCKET_DATA, onDATA);
    function onDATA(e:ProgressEvent):void{
      var d:String=s.readUTFBytes(s.bytesAvailable);
      socketBuffer = socketBuffer.concat(d);
      var tokens:Array = socketBuffer.split(".");
      var done:Boolean = false;
      while (tokens.length > 1 && !done){
        var token:String = tokens.shift();
        if (!processToken(token)){
           socketBuffer = token;
           done = true;
        }
      }
    }
    
    function processToken(token:String):Boolean{
      var embeddedNums:Array = token.match(/\d+/g);
      if (!embeddedNums  || (embeddedNums.length < 2)){
        return false;
      }
      var clipIndex:int = Number(embeddedNums[0]) -1; //because arrays start at 0.
      var frameIndex:int = Number(embeddedNums[1]);
      this[ONarray[clipIndex]].gotoAndStop(frameIndex);
      return true;
    }
    I'm not sure thats accurate so here's what I did with trace:


    first i put traces in about every other line of code - basically i traced all the variables. then i typed in "s2+2." character by character to my INPUT text box, and the trace output said :
    Code:
    s
    2+2.
    22+2+22+2.
    22+2+22+2,
    false
    22+2+22+2
    22,2,22,2
    in addition to the s1,s2,s3 etc. movie clips, i also have L1,L2,L3 movie clips. when i finished typing "s2+2." L22 randomly jumped to frame 2! so somehow because of the way the 2's are being put next to each other in the buffer or whatever, its obviously picking up a "22" in there. still it seems wierd that it picked L22 to go to frame 2, instead of s22....

    other than that mixup, i figured out why i was getting an error before:
    i have some movie clips in the array that havent been assigned an "s#" yet, so they have wierd names like "redButton" - which means it has no number. So it was when i was typing "redButton+2" that I was getting the error.... since redButton will be eliminated and replaced with an "s#" i just realized this error is irrelavant. sorry i should have told u that to begin with because i bet u already could've told me that......so im gonna forget about that error now but im stil confused about that L22-s22 mixup

    if necessary ill get rid of the L's and give them an s#

  5. #25
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Since you have no L anythings in your ONarray, it should be impossible for this code to send them anywhere. The code doesn't even look at the letters passed in. You could send "q2+2." and it should still send s2 to frame 2.

    I don't quite know what to make of your traces. What does each line represent? It kinda looks like the same data is read out of the socket more than once.

    Try these traces:
    Code:
    function onDATA(e:ProgressEvent):void{
      trace("socketBuffer going in:'"+socketBuffer+"'");
      var d:String=s.readUTFBytes(s.bytesAvailable);
      trace("from socket:'"+d+"'");
      socketBuffer = socketBuffer.concat(d);
      trace("socketBuffer after concat:'"+socketBuffer+"'");
      var tokens:Array = socketBuffer.split(".");
      trace("tokens: "+tokens);
      var done:Boolean = false;
      while (tokens.length > 1 && !done){
        var token:String = tokens.shift();
        trace("token:'"+token+"'");
        if (!processToken(token)){
           socketBuffer = token;
           done = true;
        }
      }
    }

  6. #26
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    ok, heres the more organized trace results
    Code:
    s
    socketBuffer going in :''
    from socket:''
    socketBuffer after concat:''
    tokens: 
    s
    socketBuffer going in :''
    from socket:'2'
    socketBuffer after concat:'2'
    tokens: 2
    s
    socketBuffer going in :'2'
    from socket:'2+'
    socketBuffer after concat:'22+'
    tokens: 22+
    s
    socketBuffer going in :'22+'
    from socket:'2+2'
    socketBuffer after concat:'22+2+2'
    tokens: 22+2+2
    s
    socketBuffer going in :'22+2+2'
    from socket:'2+2.'
    socketBuffer after concat:'22+2+22+2.'
    tokens: 22+2+22+2,
    token:'22+2+22+2'
    22,2,22,2
    and yea some parts are getting read out of the buffer more than once
    like in the part i bolded above....i never typed a 22, i typed s 2 + 2 .

  7. #27
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Look at those lines labeled 'from socket'. Your sending program is actually sending redundant stuff:
    2
    2+
    2+2
    2+2.

    Given those incoming packets, the socketBuffer does hold what I would expect.

    I think part of the problem might be your sending program. It apparently never sent 's', and sent the rest of it duplicated for every character. Are you clearing the input after each send? If not, it will send like this:
    s
    s2
    s2+
    s2+2
    s2+2.

    But we want it to send this:
    s
    2
    +
    2
    .

    Now, I did think of a bug that's unrelated to that. If the last info in a socket data is a partial message, it will be thrown out with the code so far. Here's a fix:
    Code:
    function onDATA(e:ProgressEvent):void{
      trace("socketBuffer going in:'"+socketBuffer+"'");
      var d:String=s.readUTFBytes(s.bytesAvailable);
      trace("from socket:'"+d+"'");
      socketBuffer = socketBuffer.concat(d);
      trace("socketBuffer after concat:'"+socketBuffer+"'");
      var tokens:Array = socketBuffer.split(".");
      trace("tokens: "+tokens);
      var done:Boolean = false;
      while (tokens.length > 1 && !done){
        var token:String = tokens.shift();
        trace("token:'"+token+"'");
        if (!processToken(token)){
           socketBuffer = token;
           done = true;
        }
      }
      if (!done){
        socketBuffer = tokens.shift(); //set socketbuffer to unprocessed last token.
      }
    }

  8. #28
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    k thanks and I'll work on the fixin the code for the other program.

    how come when i try and make seperate movie clips on a different layer with the same code it doesn't work? i don't duplicate any of the variables or functions in the code, I change them slightly:
    Code:
    in this case I added "M" to the begining of each variable or changed the function name all together
    var METERarray:Array=new Array("x400","x401","x402","x403","x404","x405","x406",
    							   "x407","x408","x409","x410","x411","x412","x413", "x414","x415");
    
    var MsocketBuffer:String = "";
    s.addEventListener(ProgressEvent.SOCKET_DATA, meterDATA);
    function meterDATA(e:ProgressEvent):void{
    	var Md:String=s.readUTFBytes(s.bytesAvailable);
    	MsocketBuffer = MsocketBuffer.concat(Md);
    	var Mtokens:Array = MsocketBuffer.split(".");
    	var Mdone:Boolean = false;
    	while (Mtokens.length > 1 && !Mdone){
        var Mtoken:String = Mtokens.shift();
    	if (!MprocessToken(Mtoken)){
           MsocketBuffer = Mtoken;
           Mdone = true;
        }
      }
    }
    
    function MprocessToken(Mtoken:String):Boolean{
    	var MembeddedNums:Array = Mtoken.match(/\d+/g);
    	trace(MembeddedNums);
    	if (!MembeddedNums  || (MembeddedNums.length < 2)){
    		return false;
    	}
    	var MclipIndex:int = Number(MembeddedNums[0]) -1; //because arrays start at 0.
    	var MframeIndex:int = Number(MembeddedNums[1]);
    	this[METERarray[MclipIndex]].gotoAndStop(MframeIndex);
    	return true;
    }
    
    for (var Mi:uint; Mi < METERarray.length; Mi++) {
        var METER:MovieClip=getChildByName(METERarray[Mi]) as MovieClip;
    	METER.stop();
    	var MclipName:String=METER.name;
    	
    }
    The reason why I cant put all the movie clips on the same layer with the same code because then the same EventListeners will apply to them when clicked on and stuff. For the clips in METERarray, they cannot be clicked, i only want them to change when they recieve "x415+5" etc.

    anyway when I input those numbers, I get this error:
    Code:
    x
    socketBuffer going in:''
    from socket:'419+5.'
    socketBuffer after concat:'419+5.'
    tokens: 419+5,
    token:'419+5'
    419,5
    TypeError: Error #1010: A term is undefined and has no properties.
    	at new_fla::MainTimeline/processToken()
    	at new_fla::MainTimeline/onDATA()
    thats weird, because onDATA is the function used in the code for the other layer (the one we've been working on). the name of the function for THIS layer is meterDATA..

    could it be beecause the onDATA function is trying to find an instance in it's array that matches "x415" but can't find one? that would make sense, because "x415" is in the METERarray on the separate layer

  9. #29
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You have two listeners on the same socket. They will both be triggered, but since the first one eats all the data, the second one (meterDATA) won't get anything to process.

  10. #30
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    is there a way to get around that? like
    Code:
    if(readUTFBytes NOT FOUND in ONarray){
            goto(meterDATA)
    }
    or is it better to open up another socket for the meterDATA?

  11. #31
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you only need to have one active at a time, then just add and remove the listeners appropriately. Or better yet, swap which array is being used.

    Of course, we could redesign it to skip the array entirely, since you seem to want to pass actual clip names instead of just indexes as you originally implied. If we did that, you wouldn't need two different listeners.

  12. #32
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    you can tell it to skip arrays? ok ill work on that but if im hopeless at it ill probly ask for ur help again.

    do u know why i'm getting an error here:
    Code:
    mastButton.addEventListener(MouseEvent.buttonDown, rightCLICK);
    function rightCLICK(e:MouseEvent):void{
    	if(e.buttonDown.return=false){
    		s.writeUTFBytes("mastButton"+" was righCLICKED");
    	}
    }
    i thought that the buttonDown function returned a "false" if any other mouse button is pressed besides the left. my error says
    Code:
    Syntax Error: expecting identifier before return

  13. #33
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    here are the changes i made so far in an attempt to skip over the METERarray.
    as you can see i'm trying to include both arrays in the main part of the function but just leaving the METER part out of the ON EventListeners

    the changes are in bold:
    Code:
    var ONarray:Array=new Array("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10",
    	"x11","x12","x13","x14","x15","x16","x17","x18","x19",
    	"x20","x21","x22","x23","x24","x25","x26","x27","x28","x29","x30");
    var METERarray:Array=new Array("x400","x401","x402","x403","x404","x405","x406",
    	"x407","x408","x409","x410");
    var onLastDown:MovieClip=null;
    var socketBuffer:String = "";
    s.addEventListener(ProgressEvent.SOCKET_DATA, onDATA);
    function onDATA(e:ProgressEvent):void{
      trace("socketBuffer going in:'"+socketBuffer+"'");
      var d:String=s.readUTFBytes(s.bytesAvailable);
      trace("from socket:'"+d+"'");
      socketBuffer = socketBuffer.concat(d);
      trace("socketBuffer after concat:'"+socketBuffer+"'");
      var tokens:Array = socketBuffer.split(".");
      trace("tokens: "+tokens);
      var done:Boolean = false;
      while (tokens.length > 1 && !done){
        var token:String = tokens.shift();
        trace("token:'"+token+"'");
        if (!processToken(token)){
           socketBuffer = token;
           done = true;
        }
      }
      if (!done){
        socketBuffer = tokens.shift(); //set socketbuffer to unprocessed last token.
      }
    }
    
    function processToken(token:String):Boolean{
    	var embeddedNums:Array = token.match(/\d+/g);
    	trace(embeddedNums);
    	if (!embeddedNums  || (embeddedNums.length < 2)){
    		return false;
    	}
    	var clipIndex:int = Number(embeddedNums[0]) -1; //because arrays start at 0.
    	var frameIndex:int = Number(embeddedNums[1]);
    	this[ONarray[clipIndex]].gotoAndStop(frameIndex);
    	return true;
    	this[METERarray[clipIndex]].gotoAndStop(frameIndex);
    	return true;
    }
    
    for (var i:uint; i < ONarray.length; i++) {
    	var ON:MovieClip=getChildByName(ONarray[i]) as MovieClip;
    	ON.buttonMode = true;
    	ON.addEventListener(MouseEvent.MOUSE_DOWN, onPRESS);
    	ON.addEventListener(MouseEvent.MOUSE_UP, onRELEASE);
    	ON.doubleClickEnabled=true;
    	ON.addEventListener(MouseEvent.DOUBLE_CLICK, onDC);	
    	ON.stop();
    	var clipName:String=ON.name;
    }
    
    for (var Mi:uint; Mi < METERarray.length; Mi++){
    	var METER:MovieClip=getChildByName(METERarray[Mi]) as MovieClip;
    	METER.stop();
    }
    i tried a couple other things as well, but with this code the error is that it can't change objects in teh METERarray to flahs.Display.MovieClips

    i'm still working on it but let me know if u see any obvious errors

    would some kind of filter work well for this? filter(callback:Function, thisObject:* = null):Array
    Last edited by mattwatts15; 11-18-2009 at 12:14 PM. Reason: filter array

  14. #34
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Here's some code that does not use an array. To use this, you must pass the actual clip name+frame.
    Code:
    var socketBuffer:String = "";
    s.addEventListener(ProgressEvent.SOCKET_DATA, onDATA);
    function onDATA(e:ProgressEvent):void{
      trace("socketBuffer going in:'"+socketBuffer+"'");
      var d:String=s.readUTFBytes(s.bytesAvailable);
      trace("from socket:'"+d+"'");
      socketBuffer = socketBuffer.concat(d);
      trace("socketBuffer after concat:'"+socketBuffer+"'");
      var tokens:Array = socketBuffer.split(".");
      trace("tokens: "+tokens);
      var done:Boolean = false;
      while (tokens.length > 1 && !done){
        var token:String = tokens.shift();
        trace("token:'"+token+"'");
        if (!processToken(token)){
           socketBuffer = token;
           done = true;
        }
      }
      if (!done){
        socketBuffer = tokens.shift(); //set socketbuffer to unprocessed last token.
      }
    }
    
    function processToken(token:String):Boolean{
    	var embeddedNums:Array = token.split('+');
    	trace(embeddedNums);
    	if (!embeddedNums  || (embeddedNums.length < 2)){
    		return false;
    	}
    	var clipName:String = embeddedNums[0];
    	var frameIndex:int = Number(embeddedNums[1]);
            if (this[clipName]){
      	  this[clipName].gotoAndStop(frameIndex);
    	  return true;
            }else{
              trace("no such clip: '"+clipName+"'");
              return false;
            }
    }
    Doing this, you should not need to make a new function for processing your meter clips. But this DOES now depend on your socket sending the proper name. Previously, sending "q2+3" would result in the clip with the name equal to the value in the second place in ONArray going to frame 3. Now, it will explicitly look for "q2" and try to send it to frame 3. The good news is that you don't have to build the arrays for the socket anymore, and you don't have to use a naming scheme. The bad news is that your sending program must know something about the internals of your movie.

  15. #35
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    well I still have to build the arrays for the EventListeners but that doenst seem to be a problem.

    i just tried ur method and here was the trace output i got
    Code:
    x
    socketBuffer going in:''
    from socket:'20+5.'
    socketBuffer after concat:'20+5.'
    tokens: 20+5,
    token:'20+5'
    20,5
    that looks good because it ends with 20,5 but unfortunately, x20 did not go to frame 5 like it should have

    btw what does concat mean?

  16. #36
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Where are you tracing the 'x'? It's not in the code I posted, and wherever it is, that's preventing the "x" from going in the buffer, which is preventing the message from working, because there is no clip named "20".

  17. #37
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    ah in another layer, i had

    s.readUTFBytes(1);

    so it must have been picking up the x
    Last edited by mattwatts15; 11-18-2009 at 01:06 PM.

  18. #38
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't see where in that code it could be happening. The only trace without an identifiable label is in processTokens, which would necessarily be called after traces in onDATA.

    Just for the hell of it, add a label to that trace so we can identify it.
    Code:
    trace("embeddedNums: "+embeddedNums);
    Something must be causing that trace, and if it's always the first letter that you send which appears there, it is definitely coming from the socket.

  19. #39
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    check my previous post...sorry i edited it so you probly didnt see,

    i had s.readUTFBytes(1); in anotehr layer so it was always picking off the x

    so its all working now!

    can you check my question on post #32 i think u may have missed that one also
    thanks

  20. #40
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your question about skipping arrays? All I meant by 'skip the array' was to rewrite it so that the array wasn't necessary. I didn't mean any sort of array-specific process.

    If you mean why you were getting an error with this code:
    Code:
    mastButton.addEventListener(MouseEvent.buttonDown, rightCLICK);
    function rightCLICK(e:MouseEvent):void{
    	if(e.buttonDown.return=false){
    		s.writeUTFBytes("mastButton"+" was righCLICKED");
    	}
    }
    it's because you are attempting to use return as a property of a Boolean. buttonDown isn't a function, it's a property. Not that it matters here. Even if it were a function, that's not how you test the return value. And then you were using = instead of ==, but the compiler didn't get there.
    Here's how to rewrite it to do what I think you were trying to do.
    Code:
    mastButton.addEventListener(MouseEvent.buttonDown, rightCLICK);
    function rightCLICK(e:MouseEvent):void{
    	if(!e.buttonDown){
    		s.writeUTFBytes("mastButton"+" was righCLICKED");
    	}
    }

Tags for this Thread

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