A Flash Developer Resource Site

View Poll Results: Was this post clear and to the point?

Voters
1. You may not vote on this poll
  • Wait what? say that again....complete jibberish

    0 0%
  • i don't think u knwo what yoru talking about

    0 0%
  • you could clarify your post better

    1 100.00%
  • VERY CLEAR POST!

    0 0%
Multiple Choice Poll.
Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: array and indexing movie clips

  1. #1
    Senior Member
    Join Date
    Oct 2009
    Posts
    112

    array and indexing movie clips

    In the following code, the on1,on2,on3,on4,on5 are instance names given to 5 buttons. all five of the buttons ARE SUCCESFULLY responding to the Press1 function below.

    Code:
    var ONarray:Array=new Array("on1","on2","on3","on4","on5");
    for (var i:uint; i < ONarray.length; i++) {
        var ON:MovieClip=getChildByName(ONarray[i]) as MovieClip;
    	stop();
    	ON.buttonMode = true;
    	ON.addEventListener(MouseEvent.MOUSE_DOWN, Press1);
    }
    
    function Press1(e:MouseEvent):void{
    "FUNCTION HERE"
    }
    The problem: when each button is clicked it needs to send out its number (i'm using binary sockets for that).
    the problem is i dont' want to copy the code for each button (cause there's eventually gonna be nearly 200 buttons), so in my function, when the button is clicked, it needs to write two things: it's instance name, and the fact that it was pressed:

    Code:
    binarySocket.writeUTFBytes("ONarray[i]"+"Pressed");
    when i try this i traced it and it says "undefined Pressed" when it should say something like "on3 Pressed"
    when i trace just [i] by itself, it always comes out as the number 5....no matter which button is clicked.
    any ideas? i think im really close. thanks for any advice

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    any reason your are not initializing 'i' ?

  3. #3
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    Quote Originally Posted by AttackRabbit View Post
    any reason your are not initializing 'i' ?
    to be honest i never heard of initializing i...can u elaborate? thanks for the quick reply

  4. #4
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    when i write a for loop i initialize the counting var


    for( var i : int = 0 ; i < somevalue ; i ++ ) {


    }
    and you might try to cast the return value for this

    MovieClip( getChildByName(ONarray[i]) );

  5. #5
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    i changed the line
    for (var ......etc.... so that it matched yours, and i still am having the exacat same problem

    [i] isnt calling the instance names (on1,on2,on3,on4,on5) of each button....it always comes out as the number 5.

    as to the second part, I had to keep it my way because I need the "var ON:MovieClip(getChildByName)" part because it enaables the EventListener to work on each button in the array. when I changed it to how you had it, my EventListener stopped working, because ON becomes undefined

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The reason i is always 5 is that the scope of i is the entire frame. After the loop, i is going to remain 5 forever.

    You should use e.currentTarget.name to get the name of the button pressed.

    Also, why do you put the instancenames into your array instead of the buttons themselves? If you had put the buttons into your array, you wouldn't have to go through the expensive getChildByName function.

  7. #7
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    Quote Originally Posted by 5TonsOfFlax View Post
    The reason i is always 5 is that the scope of i is the entire frame. After the loop, i is going to remain 5 forever.

    You should use e.currentTarget.name to get the name of the button pressed.

    Also, why do you put the instancenames into your array instead of the buttons themselves? If you had put the buttons into your array, you wouldn't have to go through the expensive getChildByName function.
    oh right...where would i insert that current Target code? im gonna play around wiht it now and ill let u know how it goes.

    and i have to put the instance names...they're actual MovieClips...i couldn't think of any other way to do it. also, they all need to respond to the exact same code, but give off a different number depending on which one is clicked, which is why i need i.
    lol did that make sense?

  8. #8
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    i couldn't figure out where to put the currnetTarget code, i assume its in the loop but it says e is undefined. i tried replacing e wiht ON, ONarray, event, i, and got the same error for each....

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You need to use the currentTarget stuff inside the event listener function; that's the only place where you have an event to look at.

    You do NOT need to put the names in the array. Simply remove the quotes and you are putting the actual clips in the array, which makes much more sense and is simpler.

    Code:
    var ONarray:Array=new Array(on1,on2,on3,on4,on5);
    stop();
    for (var i:uint = 0; i < ONarray.length; i++) {
        var ON:MovieClip= ONarray[i];
        ON.buttonMode = true;
        ON.addEventListener(MouseEvent.MOUSE_DOWN, Press1);
    }
    
    function Press1(e:MouseEvent):void{
      trace(DisplayObject(e.currentTarget).name+" was pressed");
    }

  10. #10
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    damn that was so easy! thanks ive seriously been workin on this all day man now i can go to sleep haha

  11. #11
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    is there a way to reset the last clicked currentTarget?

    something like "(DisplayObject(e.currentTarget).name).resetAfterE vent();

    i tried updateAfterEvent(); but that didnt solve the problem...

    basically i have multiple event listeners, one for MOUSE_DOWN, MOUSE_UP, and DOUBLE_CLICKED.
    then i have a timer event, where everytime the MOUSE_DOWN lasts for about 3 seconds, the binary socket sends out another number.

    for all my event listeners, it has been sending out the right number, but when i use the timer event, it always sends out the last clicked button.

    TO SUMMARIZE:
    for example, ill hold down "on1" and it will say "on1 was held"

    then ill hold down on2, and it will still say "on1 was held"

  12. #12
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    Code:
    import flash.utils.Timerl
    import flash.events.TimerEvent;
    
    var downTimer : Timer;
    var selectedItem : MovieClip;
    
    var ONarray:Array=new Array(on1,on2,on3,on4,on5);
    stop();
    for (var i:uint = 0; i < ONarray.length; i++) {
        var ON:MovieClip= ONarray[i];
        ON.buttonMode = true;
        ON.addEventListener(MouseEvent.MOUSE_DOWN, Press1);
        ON.addEventListener(MouseEvent.MOUSE_UP, onMouseUp );
    };
    
    function Press1(e:MouseEvent):void{
      downTimer  = new Timer( 3000 , 0 );
      downTimer.addEventListener(  TimerEvent.TIMER , handleTimer );
      downTimer.start();
      selectedItem  = e.currentTarget;
    //do other things
    };
    
    function handleTimer( evt : TimerEvent ) : void {
    //do something
       trace( selectedItem );
    };
    
    function onMouseUp( evt : Event ) : void {
       downTimer.stop();
       downTimer.removeEventListener(  TimerEvent.TIMER , handleTimer );
       downTimer = null;
    //do more things
    };
    Last edited by AttackRabbit; 11-10-2009 at 10:21 AM.

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Show the code. Any answer without seeing the code is just going to be guesswork.

    I'm guessing that it's another scope issue. The question about resetting the last clicked currentTarget does not make any sense to me. currentTarget is a property of the event, and does not exist outside of the event handler. Each time an event handler is called, the currentTarget is set appropriately.

    Your code
    Code:
    (DisplayObject(e.currentTarget).name).resetAfterEvent();
    is attempting to call the non-existent method resetAfterEvent on the currentTarget's name. If the method did exist, it would be on the currentTarget. updateAfterEvent is a method of event, so you must have tried that differently too. Not that it's related to anything you're trying to do.

    Here's one way to accomplish the hold-down functionality. Keep a lastDown variable to hold the last movie which had a mouseDown, a single Timer, and a stage level mouseUp listener.
    Code:
    var lastDown:MovieClip = null;
    var holdTimer:Timer = new Timer(3000);
    holdTimer.addEventListener(TimerEvent.TIMER_COMPLETE, held);
    
    var ONarray:Array=new Array(on1,on2,on3,on4,on5);
    stop();
    for (var i:uint = 0; i < ONarray.length; i++) {
        var ON:MovieClip= ONarray[i];
        ON.buttonMode = true;
        ON.addEventListener(MouseEvent.MOUSE_DOWN, Press1);
    }
    
    function Press1(e:MouseEvent):void{
      lastDown = MovieClip(e.currentTarget);
      trace(lastDown.name+" was pressed");
      holdTimer.start();
    }
    
    stage.addEventListener(MouseEvent.MOUSE_UP, cancelHold);
    
    function cancelHold(e:MouseEvent):void{
      lastDown = null;
      holdTimer.reset();
    }
    
    function held(e:TimerEvent):void{
      trace(lastDown.name+" was held);
    }
    Note that if you press one of the clips, then move the mouse off it while continuing to hold the mouse, that still counts here. To prevent that, you'd have to add a MouseEvent.MOUSE_OUT listener to each of your clips which does the same thing as cancelHold. In fact, use the same function.

  14. #14
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    thanks for all the responses
    Last edited by mattwatts15; 11-10-2009 at 10:58 AM.

  15. #15
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    5 tons of flax your code was really helpful, all problems are solved.

    for future reference, what was that that you did?
    var lastDown:MovieClip = null;
    does that mean that the movie clip is not currently being touched/clicked on?
    and then in the later part of the code is it setting it as the current target?

    whatever it means, it works! so thanks again

  16. #16
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    var lastDown:MovieClip = null;
    That line does two things. First, it declares the lastDown variable as a MovieClip. Then, it initializes that variable to null. Strictly speaking, initializing it is not necessary as the default value for an Object type variable is null, and there's no way the code will reference it before it's properly set anyway. I just like to be explicit.

    The lastDown variable is intended to hold a reference to the last movieclip which was touched, as long as it is being held down. Setting it to null means that there is no such movieclip, meaning nothing is being held down. In the press1 function, we set it to the clip which was touched. It should remain with that value until either the timer fires, or the mouse is lifted. If the timer fires, then the user held down for the duration of the timer. If the mouse is lifted, then we reset lastDown and the timer to be ready to try again.

  17. #17
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    oh ok sweet ur an AS3 genius. i bet u created it or somethin

  18. #18
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    im havin the same problem now with an array of sliders. everytime i click on any random slider, the LAST slider in the array is the only one that moves. even if i'm clicking on a different slider somewhere else on the screen, and moving the mouse upward, the last slider in the array will mimick the mouse's movement and move up, while the one i actually clicked on wont' move at all.

    it's got to be similar to the problems i've had before, i need a way to let it know that it cant just pick up the last slider in the array, it needs to move the one thats being clicked on.

    Code:
    var FADERarray:Array=new Array("fader1","fader2","fader3","fader4","fader5");
    
    for (var iFADER:uint; iFADER< FADERarray.length; iFADER++) {
        var FADER:MovieClip=getChildByName(FADERarray[iFADER]) as MovieClip;
    	FADER.addEventListener(MouseEvent.MOUSE_DOWN, dragScroll);
    	addEventListener(Event.ENTER_FRAME, loop);	stage.addEventListener(MouseEvent.MOUSE_UP, dropScroll);
    	FADER.buttonMode = true;	
    	var bounds:Rectangle = new Rectangle(FADER.x,795,0,205);
    	var draging:Boolean=false;
    }
    
    function dragScroll(evt:MouseEvent):void{
    	FADER.startDrag(false,bounds);
    	draging=true;
    }
    
    function loop(e:Event):void{	
    	svalue.text=String(Math.round((FADER.y-bounds.bottom)/(bounds.top-bounds.bottom)*8.00));
    	
    }
    
    function dropScroll(evt:MouseEvent):void{
    	FADER:stopDrag();
    	draging=false;
    }
    
    }
    lookin though the code again, i think the problem may be with the bounds, perhaps here:

    Code:
    svalue.text=String(Math.round((FADER.y-bounds.bottom)/(bounds.top-bounds.bottom)*8.00));
    or here:
    Code:
    var bounds:Rectangle = new Rectangle(FADER.x,795,0,205);

  19. #19
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    This is the exact same problem. FADER is a variable, after the loop, FADER will always be the last thing it was set to, which is the clip with the last name in your array. In dragScroll, you have to use evt.currentTarget to get the one which is being moused down on. In loop... I have no idea what you're trying to do with loop, but you shouldn't use FADER in it.

    and in dropScroll, you have a syntax error (":" instead of "."), and you shouldn't be using FADER.

    And again, it's simpler if you put the actual clips in your array instead of their names.

    I think you want to keep a reference to the one currently dragged, and you are using FADER instead of this.

    You have a variable which should be outside the loop inside it (draging (which is misspelled)). You also have some addEventListener lines which should only be done once inside the loop. Don't do things that should only be done once inside a loop.

    And I think you're trying to have a bounds for each fader, but you only have one variable for it.

    Code:
    var FADERarray:Array=new Array(fader1,fader2,fader3,fader4,fader5);
    var currentFader:MovieClip;
    var dragging:Boolean=false;
    addEventListener(Event.ENTER_FRAME, loop);
    stage.addEventListener(MouseEvent.MOUSE_UP, dropScroll);
    
    for (var iFADER:uint; iFADER< FADERarray.length; iFADER++) {
        var FADER:MovieClip=FADERarray[iFADER];
        FADER.addEventListener(MouseEvent.MOUSE_DOWN, dragScroll);
        FADER.buttonMode = true;	
        FADER.bounds = new Rectangle(FADER.x,795,0,205);  //works because it's a dynamic class.  otherwise declare this property.
    }
    
    function dragScroll(evt:MouseEvent):void{
      currentFader = MovieClip(evt.currentTarget);
      currentFader.startDrag(false,currentFader.bounds);
      dragging=true;
    }
    
    function loop(e:Event):void{	
       var bounds:Rectangle = currentFader.bounds;
       svalue.text=String(Math.round((currentFader.y-bounds.bottom)/(bounds.top-bounds.bottom)*8.00));
    }
    
    function dropScroll(evt:MouseEvent):void{
      currentFader.stopDrag();
      dragging=false;
    }

  20. #20
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    ok, well now in the OUTPUT im getting an error in response to the loop function:
    Code:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    	at D3264NEW_fla::MainTimeline/loop()
    i cant remember why i named that function loop, but basically it has something to do wiht the imaginary line/rectangle that all the sliders are on, and their limits.

    does that mean i need to make seperate "bounds" variables/rectangles for each slider? it wouldnt take long but im tryin to learn how to do things with as little code as possible cause this is the first of many projeects i will be workin on.

    also, when i removed the quote marks in teh array, i got an error, that it wasnt able to access any of the movieClips, so i put them back.

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