A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: [RESOLVED] Noob trying to learn arrays ...

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    38

    resolved [RESOLVED] Noob trying to learn arrays ...

    Hi everyone.

    Been beginning to learn AS3 (no prior programming experience). I'm in the process of trying to make a simple array and I'm having trouble.

    I have four objects, and I just want to do something simple, like scale an object on a mouse over (and shrink on a mouse out) to start.

    Here's my starting code:

    Code:
    var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]);
    for each(var box:MovieClip in myArray) {
    	box.addEventListener(MouseEvent.MOUSE_OVER, boxOver):
    	box.addEventListener(MouseEvent.MOUSE_OUT, boxOut);
    	function boxOver(event) {
    		//scaling code
    	}
    }
    I'm getting this error on this third line of the array: 1078: Label must be a simple identifier.

    What am I doing wrong? Is my code even correct?

  2. #2
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    Code:
    function setupVars():void
    {
      var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]);
      for(var i:Number = 0; i < myArray.length; i++)
      {
        myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
        myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
       }
    }
    
    function boxOver(ev:Event):void
    {
      // something 
    }
    
    function boxOut(ev:Event):void
    {
      // something else
    }
    for each is valid way of working with arrays but since you have no experience, i'd say it would be better if you learned how to work with arrays this way. I think it gives you far more control.

    ok things to consider
    myArray.length == 4
    BUT actually targeting array values are going to be:
    myArray[0] == box1_mc
    myArray[1] == box2_mc
    myArray[2] == box3_mc
    myArray[3] == box4_mc
    myArray[4] == null/undefined

    so that for loop starts 'i' at 0. as long as 'i' is less than myArray.length then add one to the 'i'

    myArray[i] pulls out one instance of what is in there. So you end up cycling through the entire array and when i == 4 the loop stops.

    whats nice about knowing this method is that if there is a reason you need to call one particular item from your array then you can use myArray[3] and single it out.


    Finally when working with event listeners you can do them one of 2 ways. have a function out side the bounds of the function that is adding the listener:
    Code:
    function firstfunct()
    {
       something.addEventListener(someevent, eventFunct);
    }
    
    function eventFunct(e:Event)
    {
      // what you want to happen
    }
    or you can create the function in the event listener:

    Code:
    function firstfunct()
    {
       something.addEventListener(someevent, function(){ trace("cant comment here ^_^") });
    }

  3. #3
    Member
    Join Date
    Nov 2007
    Posts
    38
    Hmm. That looks easy enough. I must have read the chapter on arrays in Wiley's Bible like 4 times, and your post was infinitely simpler to understand than the book. lol.

    Thanks a lot. I'll test it out and see what happens!

  4. #4
    Member
    Join Date
    Nov 2007
    Posts
    38
    Well, I think I'm understanding your code, but it doesn't seem to be working, and I'm not sure why. Maybe I included something incorrectly. As you'll see, I'm trying to just fade the alpha of each, to do something easy to begin with:

    Code:
    function setupVars():void {
      var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]);
      for(var i:Number = 0; i < myArray.length; i++) {
        myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
        myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
    	myArray[i].alpha = 0
    	myArray[i].buttonMode = true;
       }
    }
    var boxTween:Tween;
    var box2Tween:Tween;
    function boxOver(ev:Event):void {
      	boxTween = new Tween(myArray, "alpha", None.easeNone, 0, 1, .5, true);
    }
    
    function boxOut(ev:Event):void {
    	box2Tween = new Tween(myArray, "alpha", None.easeNone, 1, 0, .5, true);
    }
    I'm getting Access of Undefined Property errors for each value in the array, Access of Undefined Property i, and Access of Undefined Property myArray.

  5. #5
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    Ahh one other thing if forgot to mention is the difference between public and private variables.

    so lets say you have something like this:

    Code:
    // usually nice to define all of these at the top = ]
    var boxTween:Tween;
    var box2Tween:Tween;
    
    function setupVars():void {
     var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]); 
     for(var i:Number = 0; i < myArray.length; i++) {
        myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
        myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
    	myArray[i].alpha = 0
    	myArray[i].buttonMode = true;
       }
    }
    
    function boxOver(ev:Event):void {
            // ev is a target of what got an event listener one property is currentTarget which is one specific instance of myArray
      	boxTween = new Tween(ev.currentTarget, "alpha", None.easeNone, 0, 1, .5, true);
    }
    
    function boxOut(ev:Event):void {
    	box2Tween = new Tween(ev.currentTarget, "alpha", None.easeNone, 1, 0, .5, true);
    }
    another thing you are going to want to know about is private and public variables:
    http://livedocs.adobe.com/flash/9.0/...=00000043.html

    scroll down to " Understanding variable scope "
    its really important that you understand that section.

  6. #6
    Member
    Join Date
    Nov 2007
    Posts
    38
    Okay, got it. So I should define the variables first thing. But if they're outside the function, they're global, right? So why does it matter if they're at the top? Just easy to store them first I suppose.

    I'm still getting access of undefined property errors for the movie clips.

    This is getting frustrating.

    Thanks for all your help here deadlock. It's much appreciated.

  7. #7
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    when are the box_mc clips defined? are they on stage in the timeline?

  8. #8
    Member
    Join Date
    Nov 2007
    Posts
    38
    Okay, nevermind that. I got it. Careless mistake on my part. I must have undid something or something, because the code wasn't right. But I fixed it. No compiler errors. However, now the functions don't do anything on the MOUSE_OVER. Ah!!

    I need to take a break from this for a little while.

  9. #9
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    make sure and throw a trace in the boxOver()
    trace("am i working?");

  10. #10
    Member
    Join Date
    Nov 2007
    Posts
    38
    Yeah, I have a trace in there. Nothing's happening. Except lots of confusion and anger in my head. lol

  11. #11
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    Code:
    // usually nice to define all of these at the top = ]
    var boxTween:Tween;
    var box2Tween:Tween;
    
    function setupVars():void {
    
    ////// try this
     var myArray:Array = new Array();
     myArray.push(box1_mc, box2_mc, box3_mc, box4_mc);
    /////////////////
    
     for(var i:Number = 0; i < myArray.length; i++) {
        myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
        myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
    	myArray[i].alpha = 0
    	myArray[i].buttonMode = true;
       }
    }
    
    function boxOver(ev:Event):void {
            // ev is a target of what got an event listener one property is currentTarget which is one specific instance of myArray
      	boxTween = new Tween(ev.currentTarget, "alpha", None.easeNone, 0, 1, .5, true);
    }
    
    function boxOut(ev:Event):void {
    	box2Tween = new Tween(ev.currentTarget, "alpha", None.easeNone, 1, 0, .5, true);
    }

  12. #12
    Member
    Join Date
    Apr 2004
    Posts
    78
    I think your Array syntax is off. Shouldn't it be:

    Code:
    var myArray:Array = [box1_mc, box2_mc, box3_mc, box4_mc];
    trace(myArray);

  13. #13
    Junior Member
    Join Date
    Dec 2004
    Location
    Lithuania, Vilnius
    Posts
    2
    Code:
    var users:Array = ["Todd", "Jimmy", "Susan"];
    
    enter_btn.addEventListener(MouseEvent.CLICK, onClick);
    
    function onClick(event:MouseEvent):void
    {
       for(var i:Number = 0; i < users.length; i++)
       {
          if(users[i] == users_txt.text)
         {
             trace("access granted");
         }
       }
    }

  14. #14
    Member
    Join Date
    Nov 2007
    Posts
    38
    Re-visiting this now. Created a new fla from scratch. I attached it, in case anyone still gives a crap about it.

    If not, here's just the code. I have two squares on the stage.

    Code:
    import fl.transitions.*; 
    import fl.transitions.easing.*;
    
    function setupVars():void {
    	var myArray:Array = [box1_mc, box2_mc];  
    	for(var i:Number = 0; i < myArray.length; i++) {
            myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
            myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
    	myArray[i].buttonMode = true;
    	}
    }
    var box:Tween;
    var box2: Tween;
    function boxOver(ev:Event):void {
    	 box = new Tween(ev, "width", Elastic.easeIn, 100, 130, .5, true);
    	 box = new Tween(ev, "height", Elastic.easeIn, 100, 130, .5, true);
    	 trace("yay, it works");
    }
    
    function boxOut(ev:Event):void {
    	 box2 = new Tween(ev, "width", Elastic.easeOut, 130, 100, .5, true);
    	 box2 = new Tween(ev, "height", Elastic.easeOut, 130, 100, .5, true);
    	 trace("yay, this one works, too");
    }
    When I MOUSE_OVER, still, nothing. The buttonMode = true doesn't even work.
    Attached Files Attached Files
    Last edited by buttbutt; 11-20-2007 at 09:46 AM.

  15. #15
    The problem with your code is that your never calling the function setupVars.
    Add "setupVars();" to the end of your code on a new line, without the quotes.
    The functions are there, but you just have not started the one function which starts the rest of your code.
    Last edited by tariqm; 11-20-2007 at 10:51 AM.

  16. #16
    Member
    Join Date
    Nov 2007
    Posts
    38
    Of course! Oh Jesus.
    I go nuts trying to learn the complex stuff, and then completely forget the simple things. Ha.

    Thanks tariqm.

  17. #17
    Member
    Join Date
    Nov 2007
    Posts
    38
    Oh, now what's the problem?
    I added an ENTER_FRAME event listener to activate the function, but it's still not working.

    I get this:
    ArgumentError: Error #1063: Argument count mismatch on boxes_fla::MainTimeline/setupVars(). Expected 0, got 1.

  18. #18
    Looks like your passing something into your parameters for setupVars() when the function expects nothing to be passed into it. Can you show your code?

  19. #19
    Member
    Join Date
    Nov 2007
    Posts
    38
    Code:
    import fl.transitions.*; 
    import fl.transitions.easing.*;
    
    addEventListener(Event.ENTER_FRAME, setupVars);
    function setupVars():void {
    	var myArray:Array = [box1_mc, box2_mc];  
    	for(var i:Number = 0; i < myArray.length; i++) {
        myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
        myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
    	myArray[i].buttonMode = true;
    	}
    }
    var box:Tween;
    var box2: Tween;
    function boxOver(ev:Event):void {
    	 box = new Tween(ev, "width", Elastic.easeIn, 100, 130, .5, true);
    	 box = new Tween(ev, "height", Elastic.easeIn, 100, 130, .5, true);
    	 trace("yay, it works");
    }
    
    function boxOut(ev:Event):void {
    	 box2 = new Tween(ev, "width", Elastic.easeOut, 130, 100, .5, true);
    	 box2 = new Tween(ev, "height", Elastic.easeOut, 130, 100, .5, true);
    	 trace("yay, this one works, too");
    }

  20. #20
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    you should only have to setupVars() once.

    But the reason for that error and anytime that you see Expected 0 got 1 means that you have setupVars( ZERO items here ) but the compiler is expecting: setupVars( ONE thing here)

    in your case the compiler is expecting setupVars(evn:Event) from your enterFrame event. any function that is triggered from an event needs to have an Event var defined in the function.

    but mouse event and mouse over will happen any time you mouse hovers over the boxes, so if you make setup vars work on an enterframe, you in tern create a problem of adding event listeners to the objects that already have them.

    something you may even want to consider doing (after you get it to work ; P ) is working with the event listeners in this fashion:

    Code:
    function setupVars():void {
    	var myArray:Array = [box1_mc, box2_mc];  
    	for(var i:Number = 0; i < myArray.length; i++) {
            myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
    	myArray[i].buttonMode = true;
    	}
    }
    var box:Tween;
    var box2: Tween;
    function boxOver(ev:Event):void {
             ev.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, boxOver);
             ev.currentTarget.addEventListener(MouseEvent.MOUSE_OUT, boxOut);
    	 box = new Tween(ev, "width", Elastic.easeIn, 100, 130, .5, true);
    	 box = new Tween(ev, "height", Elastic.easeIn, 100, 130, .5, true);
    	 trace("yay, it works");
    }
    
    function boxOut(ev:Event):void {
             ev.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, boxOut);
             ev.currentTarget.addEventListener(MouseEvent.MOUSE_OVER, boxOver);
    	 box2 = new Tween(ev, "width", Elastic.easeOut, 130, 100, .5, true);
    	 box2 = new Tween(ev, "height", Elastic.easeOut, 130, 100, .5, true);
    	 trace("yay, this one works, too");
    }
    the reason for doing it like that is just a small performance/resource consideration, something you might want to keep in mind when these as3 projects get larger.

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