0

Forgive me, I'm not a proper JS programmer and still getting my head around a lot of concepts.

Suppose one had a group of similar, 2-frame/2-state rollover movie clips nested inside a containing clip, which has the instance name "Map". Each clip uses a 4 digit ID number preceded by an "s" as an instance name – e.g., "s6566".

Suppose one then wanted to capture those respective instance names to define a variable, such that one small script could allow each of these movie clips to display their ID on rollover/active state (in this case "6566"), across multiple files.

Ultimately I have thousands of these little clips spread across several dozen documents, and it seems it should be fairly simple to grab each symbol's instance name/ID, strip off the "s" from the beginning (there because instance names can't begin with a numeral), and apply said ID as dynamic text to it's respective symbol's rollover/active frame.

Is there a method of achieving this goal? I wish I had some example code to include here, but I'm not quite sure how to begin, other than to lay out the problem thusly. Haven't yet been able to find any info on capturing instance names, and I'm not sure whether it's possible. Thanks.

Halfacre
  • 565
  • 8
  • 26

1 Answers1

0

Children of MovieClips are stored as references using their instance name. You can see the format in the exported library JS file. Note that Animate will convert some instance names to remove unsupported characters or duplicates.

Here is some untested pseudo-code to get you started.

// You can iterate a MovieClip and get the names
for (var name in someMovieClip) {
    // Ignore anything not starting with an s
    if (name.substr(0,1) != "s") { continue; }

    // remove the s
    var newName = name.substr(1);

    // The child can be accessed using bracket-access with its name
    var child = someMovieClip[name];

    // The child should have text instances if it is set up how you described
    // Set the text to the newName
    child.textInstance.text = newName
}

Don't forget to update the stage after you make changes. If you already have Ticker set up to do that, it should update immediately.

I hope that helps. If you have follow-up questions, let me know.

Lanny
  • 11,244
  • 1
  • 22
  • 30
  • Thanks once again, @Lanny! I was hoping to see you here. So I replaced `someMovieClip` with my containing clip's instance name, and gave this a whirl with a `console.log(newName)`, but `newName` is coming back as `undefined`. What am I missing? Regarding `var name`... am I declaring `name` as a new variable there? I realize I'm really showing my a** here, haha. That being said, what is Ticker? *cringe* – Halfacre Jun 10 '19 at 18:45
  • What do you get if you `console.log(n)`? It _should_ be "s1456" or whatever your instance names are. The for-loop just iterates all the properties of the MovieClip, and instances are stored on each MovieClip as variables. It will output the string name. The `createjs.Ticker` is used by EaselJS to update the stage on a regular basis, and Animate should initiate this in the HTML template. Hope that helps! – Lanny Jun 10 '19 at 21:45
  • `console.log(n)` returned `Can't find variable: n` – Halfacre Jun 10 '19 at 22:06
  • ha, totally my mistake. Should be `name` and not `n`. `name.substr()`, etc. – Lanny Jun 10 '19 at 22:07
  • No worries, really appreciate the help. So I got a symbol with instance name "Map" on the main timeline, with the following code on an actions layer on the main timeline: https://codepen.io/halfacre/pen/wLBvEp `console.log(name);` returns as `undefined`. Here's a screen cap inside the "Map" instance (symbol is named "mapTop") showing one of the stops highlighted, with an example of it's ID preceded by an "s" as its instance name (just to make sure the structure really is as I laid out previously). Each stop is on its own layer (if that matters). https://postimg.cc/ftPh3j42 – Halfacre Jun 11 '19 at 17:24
  • What does `Map` resolve to when you console.log it? Any chance you can post or send your library javascript and html? Its hard to debug without running it :) – Lanny Jun 11 '19 at 20:14
  • Understandable! I updated the pen with the HTML and JS generated from Animate. I'm not sure why I half-expected it to work there, but it doesn't. There's a lot of old CS5 code commented out, and the JS has 5 digits worth of lines. Still in way over my head, haha. – Halfacre Jun 11 '19 at 20:33
  • Sorry, forgot to answer your first question. `console.log(Map)` returns the following: `function Map() { [native code] }` – Halfacre Jun 11 '19 at 21:23
  • 1
    You have got a few `Map` usages vs `this.Map`. Note that there is a global [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object in JavaScript, which is why you see the `[native code]` log. Swap those out, and you might start seeing actual values you expect! – Lanny Jun 12 '19 at 00:57