A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: onMouseDown listener to an attachMovie; harder than you think.

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3

    onMouseDown listener to an attachMovie; harder than you think.

    Heres the gist of my problem...
    1. A Grid is filled out with molecules, using a loop;
    2. User clicks on molecule to bring up its virtual co-ords


    Now i have a loop:
    Code:
    for (var i = 0; i < _root.gp.input; i++)
    {
    // Makes a new instance of the MC "Molecule"
    	this.attachMovie("Molecule","molecule" + i,i);
    
    // Add a listener to the new molecule
    	(molecule + i).onMouseDown = function()
    	{
    		trace("you clicked " + "molecule" + i);
    
    	};
    }

    This is the tricky bit -> ("molecule" + i).onMouseDown = function()

    How can i attach code to a new instance of a movie?

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

    Try
    PHP Code:
    this["molecule" i].onMouseDown 

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    3

    Arrow UPDATE - Still not working

    Using this code:
    Code:
    	// Show Coordinates
    	this["molecule" + i].onMouseDown = function()
    	{
    		trace("you clicked " + "molecule" + i);
    
    	};
    and the Trace Output is:

    Code:
    you clicked molecule5
    you clicked molecule5
    you clicked molecule5
    you clicked molecule5
    you clicked molecule5
    So ive made 5 instances of the movieclip (0 to 4), and clicking on any shows the ("molecule" + i) where i is the incrementer in the loop, and 5 is the defined delimiter.
    I need to make any molecule be identified when clicked on. So the value should be unique, not the same regardless of which molecule like it currently is.

    Thanks in advance anyone who reads this post or offers a hand.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    For you to reference each attached clip/button try
    PHP Code:
    this["molecule" i].Num i;
    this["molecule" i].onPress = function()
        {
            
    trace("you clicked molecule" this.Num);
        }; 
    Last edited by fruitbeard; 07-02-2013 at 07:56 AM. Reason: typo

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thank you Fruitbeard! It worked! I owe you my life sir!

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I sent a private message to your other question, but not sure if it was sent or not!!

    But here is your answer
    PHP Code:
    _root.gp.control.onRelease = function()
    {
        for (var 
    0molecules.lengthj++)
        {
            
    _root["molecule" j].removeMovieClip();
            
    //this["molecule" + j].removeMovieClip();
        
    }
    }; 
    one of those will work depending where you have the stuff embedded.

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