Skip navigation
Currently Being Moderated

Can't get onRelease to work for an image mc

Apr 29, 2012 10:30 PM

Tags: #actionscript #2.0

on on of my pages I have 5 thumbnail images that I've turned into buttons (b1-b5). What I want is to click on an image button and display a large version on the screen then then I click on the large image it goes away. Both the thumbnail and large images are loaded dynamic. each thumbnail button sends it's consponding large image address to a function when pressed. This is the code that I have. in this example "home_img1" = images/home01.jpg (var loaded at runtime from a html text file. the buttons (and all page 1 content) are "nested" 1 level in a mc named "pg1" but this code in on the root timeline. btw this is a actionscript 2 project.


pg1.b1.onRelease = function()



    trace("load "+home_img1);


pg1.b2.onRelease = function()



    trace("load "+home_img2);


pg1.b3.onRelease = function()



    trace("load "+home_img3);


pg1.b4.onRelease = function()



    trace("load "+home_img4);


pg1.b5.onRelease = function()



    trace("load "+home_img5);


function loadPic(myPic) {

    loadMovie(myPic, lg_pic.lg_cont1);

    trace(myPic + "_loaded");


    lg_pic.lg_cont1.onRelease = function() { //everything seems to work fine to this point then nothing


      trace("remove image");





I wnat to be able to click the on the large image that came up and have it go away. What Im' I doing wrong ??





  • Currently Being Moderated
    Apr 30, 2012 4:36 AM   in reply to jdhughen

    You are assigning the onRelease to the object that exists before the image is loaded, not the one that replaces it afterwards.  Use the MovieClipLoader class to wait until the object is loaded before assigning the onRelease to it.


    If you are not familiar with using the MovieClipLoader class with a listener for load completion, look up MovieClipLoader.addListener in the help documentation and you will find an example there that shows how to set it up.

    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 9:28 AM   in reply to Ned Murphy

    You're using MovieClip.loadMovie() which loads an external asset into a MovieClip. It's a method of the MovieClip class.



    Here's how you use it:




    What you appear to want to do is MovieClip.attachMovie() which will take a MovieClip with a linkage ID from the library to attach it to a specified MovieClip.




    You specified a MovieClips path, e.g.: lg_pic.lg_cont1. Does this item already exist? Are you trying to "duplicate" something that already exists? If so then you want MovieClip.duplicateMovieClip(). Examples are provided in the links.


    Feel free to include more info on your issue, like if it exists in the library, already on the screen or external to the file.

    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 1:06 PM   in reply to jdhughen

    Your tweens look somewhat ok. Get any errors from them? I presume you're importing the Tween class and its easing counterpart so you don't get errors.


    Outside that make sure the properties of the object you're about to tween are set up to make sense. For example if some objects alpha is already 100, don't tween it to 100 or it won't look like it's doing anything.


    You'd want to:


    target_mc._alpha = 0; // make sure its alpha=0

    new Tween(target_mc, "_alpha", Regular.easeInOut, 0, 100, 0.5, true);


    Same thing with fading out. Make sure to set it to 100.


    Outside that on your "onRelease" event you're removing the image with image_mcl.unloadClip(this); (remember you're already inside target_mc, so you don't say target_mc, you say 'this'). But you try to tween it. That tells you that you need to wait until it fades out before you remove it or it will just disappear.


    That invites you to look into TweenEvent. You can set up the tween to call another method once it's complete and THEN you get rid of the image and load another image.




    In all honesty you should look up mx.utils.Delegate; because "scope" is an extremely ridiculos subject when it comes to coding in AS2. You need to know where exactly the code is firing off. Most people get into a lot of trouble because they think it's all executing from the frame script they wrotes scope, but it's not.




    _root.createEmptyMovieClip('moo_mc', 1);


    trace(moo_mc); // traces moo_mc


    moo_mc.onPress = function() { trace(moo_mc); } // traces undefined


    Inside that function it's a whole different "scope". You're not at the level of the frame script anymore. You're inside that onPress events object, or inside moo_mc. And there is no "moo_mc" inside of itself. That's why AS2 sucks balls, amongst other things.


    To make it work, I know once I press moo_mc I am inside it. So 'this' will refer to itself.


    moo_mc.onPress = function() { trace(this); } // traces moo_mc


    Stupid AS2..

    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 1:53 PM   in reply to jdhughen

    Be sure to include the import lines...


    import mx.transitions.Tween;

    import mx.transitions.easing.Regular;

    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 7:56 PM   in reply to jdhughen

    I would try placing the imports in the frame where they are used.  You should also try tracing the objects you are trying to tween to make sure that you are actually targeting them.

    Mark as:
  • Currently Being Moderated
    May 1, 2012 8:10 AM   in reply to jdhughen

    Your var myFadeOut.... and then myFadeOut.onMotionFinished isn't working. I told you about the horrific 'scope' issues of AS2. Inside the onMotionFinished, why don't you do this:


    trace(image_mg); // if it worked it would say image_mc, if not UNDEFINED


    You need to assign the listener, BTW, or it won't fire off.


    Bottom line, trace() is your friend. Sprinkle it everywhere in your code. trace() everywhere you go so you can watch your code execute. More importantly where you "think" something should exist, trace() it and if it doesn't return undefined, it exists.


    For example, trace anything on your onMotionFinished function above. See if it's even firing off.

    Mark as:
  • Currently Being Moderated
    May 2, 2012 10:42 AM   in reply to jdhughen

    It's the terrible nature of AS2. Variables should never exist outside their scope.




    function foo() {

         var a = "Hello";



    foo(); // 'a' is created in the function but once it exits it's gone

    trace(a); // undefined


    That's obvious to understand. The function runs, creates a variable inside the function but when the function exists the variable is discarded.


    Now, as a convenience the look ahead compiler will let you get away with something like this:


    function foo()


        var a = "something"; // at timeline scope in a frame script but inside a function so 'a' will be nuked at the end



       myButton.onPress = function() {

            // inside myButton function which has nothing to do with foo()s scope so it shouldn't exist

             myButton.a = a;  // assign a property to myButton (which should be 'this') and set it to "something"

                                       // just using myButton here (from inside itself) should error, it makes no sense, but flash "fixes" it for you





    trace(a); // undefined


    trace(a); // undefined

    trace(myButton.a); // "something"


    AS2 is just wacky with this stuff. Even though you go in and out of scopes the compiler figures out what you meant (sometimes) and lets you think that it was kosher to do what you did when in general programming it isn't.


    Ditch it and run to AS3 as soon as possible. You're only 6 or so years off . Not much of this wacky scope crap exists anymore.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points