×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    126

    Dynamically adding a mouse click event to button movieclip

    Hi,
    I'm trying to load a set of button movieclips (already located in the Library) dynamically. Their position and other variables are defined externally in an XML file.

    I've tried to "generate" an onRelease() event handler using "this[mc].onRelease = function()" but it doesn't seem to work properly and I'm really not sure this is the right way to go.

    Here is a code snipset, after the xml is loaded into an array Items:
    Code:
      
    var nIcons:Number = Items.length;
    var xStep:Number  = Stage.width / (2*nIcons);
    var xPos:Number   = xStep;
    var yPos:Number   = Stage.height - 70;
    for (var i:Number = 0; i < nIcons; i++)
    {
    	var name:String = Items[i].Name;
    	var mc:String = name + "_mc";
    	this.attachMovie(mc, mc_mc, this.getNextHighestDepth());
    	this[mc]._x = xPos;
    	this[mc]._y = yPos;
    	this[mc].myid = mc;
    	this[mc].onRelease = function()
    	{
    		trace(" click " + myid);
    	}
    	xPos += 2*xStep;
    }
    The thing is that the trace always generates the same result - which is myid of the last loaded icon.

    Please advise,

    Thanks in advance,

    Ranco

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    46

    Re: Dynamically adding a mouse click event to button movieclip

    You are trying to use onRelease on a movieclip.
    It's only for buttons ...

    You could try onMouseDown and onMouseUp too maybe ?

    There are many different kind of events in Flash Lite ... I always had to check a bunch of them before finding the proper one.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    126

    Re: Dynamically adding a mouse click event to button movieclip

    Quote Originally Posted by Thommasc View Post
    You are trying to use onRelease on a movieclip.
    It's only for buttons ...

    You could try onMouseDown and onMouseUp too maybe ?

    There are many different kind of events in Flash Lite ... I always had to check a bunch of them before finding the proper one.
    I'm talking about button symbols; they are sometimes refered to as movieclips; anyway, not Button controls. and onRelease() is available for these.

    Ranco

  4. #4
    Nokia Developer Champion
    Join Date
    Jul 2008
    Location
    Pakistan
    Posts
    1,065

    Smile Re: Dynamically adding a mouse click event to button movieclip

    One of the easiest way to pass through this problem is hitTest API & a logic. Just write a function which test that which button the user hits via mouse and return its value to onRelease function .Write something like this:
    Code:
    // chk bt returns the index of item which is pressed
    function chk_bt() {
    	for (var i:Number =0; i< nIcons ; i++) {
    // u can replace the nIcon variable with the one having the number of total dynamic buttons created
    	        var name:String = Items[i].Name;
    	        var mc:String = name + "_mc";	
                    temp = eval(mc); // or _root[mc] if they are in root or this[mc] 
    		if (temp.hitTest(_xmouse, _ymouse)) {
    			break;
    		}
    	}
    	return i;
    }
    Now , write onRelease function like this :
    Code:
    this[mc].onRelease = function()
    	{
    		trace(" click " + chk_bt());
    	}
    Wish u luck ..


    Best Regards,
    SajiSoft

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    126

    Re: Dynamically adding a mouse click event to button movieclip

    Quote Originally Posted by sajisoft View Post
    One of the easiest way to pass through this problem is hitTest API & a logic. Just write a function which test that which button the user hits via mouse and return its value to onRelease function .Write something like this:

    Code:
    this[mc].onRelease = function()
    	{
    		trace(" click " + chk_bt());
    	}
    Wish u luck ..


    Best Regards,
    SajiSoft
    Great, seems like something that could work. thanks a lot. I'll try it tonight!
    BR,
    Ranco

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    126

    Re: Dynamically adding a mouse click event to button movieclip

    Unfortunately buttons doesn't have hiTest, so this idea didn't work...
    Maybe I should put the button inside a movieclip?
    any other idea?

    Thanks,
    Ranco

  7. #7
    Nokia Developer Champion
    Join Date
    Jul 2008
    Location
    Pakistan
    Posts
    1,065

    Smile Re: Dynamically adding a mouse click event to button movieclip

    Quote Originally Posted by ranco View Post
    Unfortunately buttons doesn't have hiTest, so this idea didn't work...
    Maybe I should put the button inside a movieclip?
    any other idea?

    Thanks,
    Ranco
    Not a big deal.You can write one of ur own hitBt function in less then a minute like this:
    Code:
    function hitBt(mc:MovieClip)
    {
    var bol:Boolean = false;
    if (_xmouse >= mc._x && _xmouse <= (mc._x +mc._width) && _ymouse >= mc._y && _ymouse <= (mc._y + mc._height))
    {
    bol = true;
    }
    return bol;
    }
    Thats it . Wish u luck.

    Best Regards,
    SajiSoft

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    126

    Re: Dynamically adding a mouse click event to button movieclip

    Quote Originally Posted by sajisoft View Post
    Not a big deal.You can write one of ur own hitBt function in less then a minute like this:
    Code:
    function hitBt(mc:MovieClip)
    {
    var bol:Boolean = false;
    if (_xmouse >= mc._x && _xmouse <= (mc._x +mc._width) && _ymouse >= mc._y && _ymouse <= (mc._y + mc._height))
    {
    bol = true;
    }
    return bol;
    }
    Thats it . Wish u luck.

    Best Regards,
    SajiSoft

    Working great. Thanks a lot!
    Ranco

Similar Threads

  1. Mouse click Event
    By abhimanyu1 in forum Symbian
    Replies: 2
    Last Post: 2009-10-09, 08:10
  2. Dynamically Adding Controls
    By Sohil in forum Symbian
    Replies: 6
    Last Post: 2007-02-20, 14:09
  3. CLogEvent problem
    By silviuccia in forum Symbian
    Replies: 2
    Last Post: 2006-12-22, 15:28
  4. How can i get a form's back button event?
    By advocatee in forum Symbian
    Replies: 1
    Last Post: 2003-07-28, 04:09
  5. Replies: 0
    Last Post: 2002-12-11, 04:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •