A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Calling indices in Multiple Array

  1. #1
    Junior Member
    Join Date
    Mar 2015
    Posts
    3

    Calling indices in Multiple Array

    Hi,
    I'm a newbie and I am trying to set up a multiple array where I can call on the mc object and then add an EventListtener and getting into all kinds of strife.

    My version of Action-script is 3 and I'm running Flash CS6 Here is my code;

    import flash.events.Event;

    Code:
    {
    	var myPieces:Array = new Array(6); // there are six rows
    		myPieces[0] = [pc1_mc,targpc1_mc,160.30,162.90];
    		myPieces[1] = [pc2_mc,targpc2_mc,291.50,162.90];
    		myPieces[2] = [pc3_mc,targpc3_mc,425.50,162.90];
    		myPieces[3] = [pc4_mc,targpc4_mc,147.65,304.50];
    		myPieces[4] = [pc5_mc,targpc5_mc,291.00,305.15];
    		myPieces[5] = [pc6_mc,targpc6_mc,435.40,303.85];
    
    	for (var row:int=0; row <=5; row++)
    	{
    		for (var column:int=0; column <=3;column++)
    		{
    			forEach (object in(var i[0] in myPieces));
    			{
    				i.buttonMode = true;
    				i.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    				i.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    			}
    				function pickupObject(event:MouseEvent):void 
    			{
    				event.target.startDrag(true);
    			}
    				function dropObject(event:MouseEvent):void 	
    			{
    				event.target.stopDrag();
    			}
    		}
    	}
    }
    I'm getting several errors on row 16
    Error 1084: Syntax error: expecting rightparen before each.
    Error 1084: expecting identifier before rightparen.
    Error 1084: exptecting identifier before var.

    Would appreciate any support.

    Thanks in advance.
    Regards
    Tanya

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    Attach your *.fla so we dont need to have to try and mimick your file, graphics and clips etc.
    Please then explain what you require help with.

  3. #3
    Junior Member
    Join Date
    Feb 2015
    Location
    France
    Posts
    5
    you write
    for (var column:int=0; column <=3;column++)
    {
    forEach (object in(var i[0] in myPieces));
    {
    i.buttonMode = true;

    not good
    @+gustave02

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hi,

    I may not be an AS3 expert, but that code looks a bit erroneous to me. I believe you're trying to access the first column of all the rows, which contains the MovieClips. If you think it this way, that you want to go through each row and simply fetch the first column-value of that row, then you'd have the code simplified to something like this:

    Code:
    var myPieces:Array = new Array(6); // there are six rows
    		myPieces[0] = [pc1_mc,targpc1_mc,160.30,162.90];
    		myPieces[1] = [pc2_mc,targpc2_mc,291.50,162.90];
    		myPieces[2] = [pc3_mc,targpc3_mc,425.50,162.90];
    		myPieces[3] = [pc4_mc,targpc4_mc,147.65,304.50];
    		myPieces[4] = [pc5_mc,targpc5_mc,291.00,305.15];
    		myPieces[5] = [pc6_mc,targpc6_mc,435.40,303.85];
    
    	for (var row:int=0; row < myPieces.length; row++)
    	{
    		var i:MovieClip = myPieces[row][0];
    		
    		i.buttonMode = true;
    		i.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    		i.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    		
    		function pickupObject(event:MouseEvent):void 
    		{
    			event.target.startDrag(true);
    		}
    		function dropObject(event:MouseEvent):void 	
    		{
    			event.target.stopDrag();
    		}
    	}
    myPieces.length simply returns the number of rows, which is 6, but your condition >= 5 also works just fine. We make a variable i and store the first cell/column-value for the current row in the for loop, making it contain your variables one at a time. Then we can simply add event listeners to those movieclips and voilą, it works

    Your code didn't really make any logical sense as you were traversing (going through) all the rows and all the columns, yet while doing so it seemed like you were trying access all the objects in first column and adding those listeners to them -- but that code syntax looked wrong, in my opinion. But salute to you for trying given that you're a newbie as you stated yourself

    Unless there are movieclips inside pc_mc movieclips to which you're trying add those listeners. In that case, to my surprise, the code above still works, and that's because you're not dragging those movieclips (pc_mc's), rather in your dragging functions it says: event.target.startDrag(true); -- and event.target will return the movieclip you clicked on/pressed down, which is why the above code should be sufficient for you regardless of how one might interpret your problem

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  5. #5
    Junior Member
    Join Date
    Mar 2015
    Posts
    3

    Response - Including Flash File

    Quote Originally Posted by Nig 13 View Post
    Hi,

    I may not be an AS3 expert, but that code looks a bit erroneous to me. I believe you're trying to access the first column of all the rows, which contains the MovieClips. If you think it this way, that you want to go through each row and simply fetch the first column-value of that row, then you'd have the code simplified to something like this:

    Code:
    var myPieces:Array = new Array(6); // there are six rows
    		myPieces[0] = [pc1_mc,targpc1_mc,160.30,162.90];
    		myPieces[1] = [pc2_mc,targpc2_mc,291.50,162.90];
    		myPieces[2] = [pc3_mc,targpc3_mc,425.50,162.90];
    		myPieces[3] = [pc4_mc,targpc4_mc,147.65,304.50];
    		myPieces[4] = [pc5_mc,targpc5_mc,291.00,305.15];
    		myPieces[5] = [pc6_mc,targpc6_mc,435.40,303.85];
    
    	for (var row:int=0; row < myPieces.length; row++)
    	{
    		var i:MovieClip = myPieces[row][0];
    		
    		i.buttonMode = true;
    		i.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    		i.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    		
    		function pickupObject(event:MouseEvent):void 
    		{
    			event.target.startDrag(true);
    		}
    		function dropObject(event:MouseEvent):void 	
    		{
    			event.target.stopDrag();
    		}
    	}
    myPieces.length simply returns the number of rows, which is 6, but your condition >= 5 also works just fine. We make a variable i and store the first cell/column-value for the current row in the for loop, making it contain your variables one at a time. Then we can simply add event listeners to those movieclips and voilą, it works

    Your code didn't really make any logical sense as you were traversing (going through) all the rows and all the columns, yet while doing so it seemed like you were trying access all the objects in first column and adding those listeners to them -- but that code syntax looked wrong, in my opinion. But salute to you for trying given that you're a newbie as you stated yourself

    Unless there are movieclips inside pc_mc movieclips to which you're trying add those listeners. In that case, to my surprise, the code above still works, and that's because you're not dragging those movieclips (pc_mc's), rather in your dragging functions it says: event.target.startDrag(true); -- and event.target will return the movieclip you clicked on/pressed down, which is why the above code should be sufficient for you regardless of how one might interpret your problem

    Hope this helps
    I copied your code over and it is still not working.. The file itself is too large (image size) so here is a link to my cloud storage:

  6. #6
    Junior Member
    Join Date
    Feb 2015
    Location
    France
    Posts
    5
    Last edited by gustave02; 03-02-2015 at 11:27 AM.

  7. #7
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hi,

    I know gustave02 has already fixed your FLA (props for that ), but I would like to point out that the reason my code didn't work for you, is because you have duplicate codes on actionscript and targets layers -- if you could remove the code from one of them, then it'll work However, since we ARE using event.target, the inner-most MovieClip will start dragging, and the registration point for that one isn't centered, which is why the pieces won't follow the cursor accurately. To fix this problem, you can change event.target to event.currentTarget:

    Code:
    		function pickupObject(event:MouseEvent):void 
    		{
    			event.currentTarget.startDrag(true);
    		}
    		function dropObject(event:MouseEvent):void 	
    		{
    			event.currentTarget.stopDrag();
    		}
    currentTarget will give you the MovieClip we added the event listener to
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  8. #8
    Junior Member
    Join Date
    Feb 2015
    Location
    France
    Posts
    5
    i modified the fla with currentTarget
    @+ gustave02

  9. #9
    Junior Member
    Join Date
    Mar 2015
    Posts
    3

    This Newby is so confused.

    Thank you to both gustar02 and Nig13, for hanging in there and in particular for Nig13, your patience in trying to explain to me what is happening with the code, but when I run each it isn't happening.

    I have since gone back to basics (I have some limited Pascal and Python experience) and want to keep the code as simple as possible.

    What I really want to do is test to see if the current target is in proximity to targ and then drop it. Hence the convoluted multiple array I tried to use.

    I've gone back to a single array to allow pickup and drop, but keep coming up with errors about the brackets. This is driving me crazy.

    Here is my new code which allows me to pick up and drop object successfully:
    Code:
    import flash.events.Event;
    
    {
    	var pieces = [pc1_mc,pc2_mc,pc3_mc,pc4_mc,pc5_mc,pc6_mc]
    
    	for each (var i in pieces) 
    	{
    		
    		i.buttonMode = true;
    		i.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    		i.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    	}
    		
    		function pickupObject(event:MouseEvent):void 
    		{
    			event.currentTarget.startDrag(true);
    		}
    		function dropObject(event:MouseEvent):void 	
    		{
    			event.currentTarget.stopDrag();
    		}
    }
    Now when I add these lines of code to see if the piece is in proximity, I start getting syntax error 1087

    Code:
    if (i.hitTestObject(targpc1_mc))
    		{
    			pc1_mc.x=117.50; // x coordinate for pc1
    			pc1_mc.y=143.00; // y coordinate for pc1
    			pc1_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);		
    		}
    I could of course then write these extra lines of code (if this was working) for each piece and matching target. But I don't believe this is the most efficient method.

    You ongoing support and instruction is appreciated.

    Here is my latest version.

    https://app.box.com/s/yqmdnpvtd8qize03224pj5ugjg5dmgn8

  10. #10
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I just stuff refrences to the movie clip in an array using a for loop ant the Array.push(mc) during the loops iteration you could attach a eventlistner that changes to whatever function u want that listener to listen on .......heck I'd probably use a second array for the xy coordiantes if I wanted to dynamically assign the mc movie clips positions. probabably wouldn't have a huge impact on hardware of VM performance.

    --calmchess
    ~calmchess~

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