Skip to main content
Inspiring
February 21, 2022
Answered

Change layer for symbol in code - HTML5 Canvas

  • February 21, 2022
  • 1 reply
  • 2805 views

How can a symbol be changed to a layer through JavaScript?

    This topic has been closed for replies.
    Correct answer kglad

    Ah, item.name


    this:

     

    item.x-=item.lensParentLeft.x-9.5;
    		item.y-=item.lensParentLeft.y-9.5;

     

    suggests item.lensParentLeft is not aligned at 0,0 on its own timeline, and is 19px wide and high and is centered.  align at 0,0 and you won't need that fudge factor. or if that's where it needs to be aligned for other reasons use:

     

    item.x-=item.lensParentLeft.x-item.lensParentLeft.width/2;
    item.y-=item.lensParentLeft.y-item.lensParentLeft.height/2;

    1 reply

    kglad
    Community Expert
    Community Expert
    February 21, 2022

    that doesn't make sense, so it can't be done.

     

    but it's probable that whatever you're trying to accomplish can be done.  what is that?

    FlatChatAuthor
    Inspiring
    February 22, 2022

    @kglad   I thought it made sense when I wrote the question... 

     

    So, I have a MovieClip symbol in a layer on stage.  The symbol has the instance ID 'ConcaveSphereLens'. It's an optometry trial lens that fits into a trial frame.

     

    The symbol 'ConcaveSphereLens' is draggable and I want it to be above all other elements on stage when dragged. 

     

    However, when dropped, it snaps into the trail lens frame.  That part that it snaps into has supports for the lens. 

     

    In real life, the lens is slid into those supports which have grooves to hold the lens.  So the top faces of those supports appear in front of the dragged lens.  See image below.

     

    Therefore, I want to change the layer of the dragged lens, so that when it is dropped, it changes layer to one that sits behind the lens supports.  So, dragged in front of the supports and dropped behind the supports that are in their own layer.

     

    In my Animate move, as an example a lens in red which is sitting in the lens supports on the trial frame.

     

     

    The frame and lens in real life..

     

     

    kglad
    Community Expert
    Community Expert
    February 22, 2022

    understood, and that does make sense.

     

    so, assuming your code is on the main timeline:

     

    to make the lens appear to be above everything else on the main timeline, use

     

    this.addChild(this.ConcaveSphereLens);

     

    the lens will remain the topmost object on your main timeline unless and until you either reparent it (eg, using addChild) or you use similar code applied to some other object.

     

    and the easiest, but not only, way to make the lens above some things and below others is to add a movieclip (eg, with instance name lensParent) to the layer where you want the lens to appear to be located and use:

     

    this.lensParent.addChild(this.ConcaveSphereLens);

     

    when you want it to appear your lens is on the lensParent layer. 

     

    btw, lensParent can (and probably should) be an empty movieclip.