A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Button inside movie clip to call function in main timeline

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    19

    [SOLVED] Button inside movie clip to call function in main timeline

    Hello again.

    I'm having a bit of a hard time with calling a function in the main timeline through a button in a movieclip. Is there anyway I can make this happen?

    The mc is in the main timeline and in it I added a button with this.removeMovieClip(); and so far it works, but when I try to call a function from the main timeline it just doesn't seem to work.

    Thank you in advance
    Last edited by Mylo Farmer; 10-06-2010 at 01:14 PM. Reason: Solved

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    and in it I added a button with this.removeMovieClip();
    Are you sure?

    The code, attached to the main timeline, should be something like:
    Actionscript Code:
    my_mc.my_btn.onRelease = function()
    {
     trace("button clicked");
     myFunction();
    }

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    thank you for the reply, but it still doesn't work. it won't even trace the button. I even tried adding _root. to it but I keep getting nothing.

  4. #4
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    Notice that both the MovieClip and the Button must exist in the same frame the code is defined.
    i.e., if you have
    Actionscript Code:
    trace(my_mc);
    trace(my_mc.my_btn);
    you have to get the correct paths

    Here's an example.
    Attached Files Attached Files

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    Nope, I still get nothing. Did it exactly as you put in your fla, but no luck. it won't trace the button at all, no matter what I do.

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    Would it make any difference that my button is on frame 15 of the mc?

  7. #7
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    Sure it would.
    Notice that both the MovieClip and the Button must exist in the same frame the code is defined.
    You can't assign code to something that doesn't exist.

    You may use something like:
    Actionscript Code:
    my_mc.onEnterFrame = function()
    {
        if (this.my_btn != undefined)
        {
            this.my_btn.onRelease = function()
            {
                trace("button clicked");
                myFunction();
            };
            delete this.onEnterFrame;
        }
    };
    This will make sure my_btn exists.
    Obviously the code and my_mc have to exist at the same time.

    Another alternative is using the first piece of code, and make both my_mc and my_btn exist, but hide my_btn by placing it out of the stage. (one keyframe out of stage, another keyframe in the desired position).

  8. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by nunomira View Post
    hi,

    Another alternative is using the first piece of code, and make both my_mc and my_btn exist, but hide my_btn by placing it out of the stage. (one keyframe out of stage, another keyframe in the desired position).
    Thank you for the reply, Nuno. As a matter of fact, I didn't quite understand this part. What exactly would be putting a keyframe out of the stage? Do you mean in the library?

  9. #9
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    Take a look at example2 to understand what I meant.
    example1 uses the onEnterFrame
    Attached Files Attached Files

  10. #10
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    Thank you for the examples, but neither work for me. Would you have a look at my fla to see if there's nothing blocking this code? (note, I removed any code to this button).

    It is on layer "desistir", mc name is "telasair" and bt is "botaosair".
    Attached Files Attached Files

  11. #11
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    You're attaching the MovieClip via attachMovie().
    You'll have to add the code after the MovieClip is attached.

    The layer you mention (desistir) also has that MovieClip, but that's not the one which is used as it doesn't even exist on frame 5.

  12. #12
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by nunomira View Post
    You're attaching the MovieClip via attachMovie().
    You'll have to add the code after the MovieClip is attached.

    The layer you mention (desistir) also has that MovieClip, but that's not the one which is used as it doesn't even exist on frame 5.
    Thank you for having a look at it. So you mean that if I add the code straight after the attachMovie() and move layer desistir to frame 5 I might have a chance at it?

    Would the code look something like this?

    Code:
    _root.leaveBtn.onRelease = function ()
    {
    	para_sons();
    	_root.attachMovie("sairjogo", "saindo", 755);
    	_root.saindo._x = 0;
        _root.saindo._y = 0;
    	quitgame = function ()
    {
        i = 0;
        for (;;) 
        {
            if (i >= nitens) 
            {
                return;
            }
            bloco["mc" + (i + 1)].campo.type = "dynamic";
            bloco["mc" + (i + 1)].campo.tabEnabled = false;
            if (bloco["mc" + (i + 1)]._currentframe != 2) 
            {
                bloco["mc" + (i + 1)].resp = bloco["mc" + (i + 1)].valid[0];
                bloco["mc" + (i + 1)].gotoAndStop(4);
            }
            ++i;
        }
    }
    ;
    telasair.onEnterFrame = function()
    {
        if (this.botaosair != undefined)
        {
            this.botaosair.onRelease = function()
            {
                trace("button clicked");
                quitgame();
            };
            delete this.onEnterFrame;
        }
    };
    	}

  13. #13
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    Nevermind the code above, I just realised I did something completely stupid lol. The actual code goes something like this:

    Code:
    _root.leaveBtn.onRelease = function()
    {
    	para_sons();
    	_root.attachMovie("sairjogo","saindo",755);
    	_root.saindo._x = 0;
    	_root.saindo._y = 0;
    	wtfsound = new Sound(this);
    	wtfsound.attachSound("wtf");
    	wtfsound.start(0,1);
    	_root.saindo.onEnterFrame = function()
    	{
    		if (this.botaosair != undefined)
    		{
    			this.botaosair.onRelease = function()
    			{
    				trace("button clicked");
    				quitgame();
    				this.removeMovieClip;
    			};
    			delete this.onEnterFrame;
    		}
    	};
    };
    quitgame = function ()
    {
    	i = 0;
    	for (; ; )
    	{
    		if (i >= nitens)
    		{
    			return;
    		}
    		bloco["mc" + (i + 1)].campo.type = "dynamic";
    		bloco["mc" + (i + 1)].campo.tabEnabled = false;
    		if (bloco["mc" + (i + 1)]._currentframe != 2)
    		{
    			bloco["mc" + (i + 1)].resp = bloco["mc" + (i + 1)].valid[0];
    			bloco["mc" + (i + 1)].gotoAndStop(4);
    		}
    		++i;
    	}
    };
    Thank you so much for your time and help, Nuno. I really appreciate it. It's finally working! Muito obrigado, de coração!

  14. #14
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003

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