Creating instances with Action Script

  • koivi
  • Novice
  • Novice
  • koivi
  • Posts: 23
  • Loc: Minnesota

Post 3+ Months Ago

How do I create an instance of a symbol from the library and change it's properties?

My first thought was that I'd have to create and name an instance off-stage, then use AS to copy, rename and change properties. Is this what needs to be done, or can I create a new instance directly from the symbol's name? In either case, I don't know how I'd create the new instance that I want to modify.

Any help is greatly appreciated!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Right click the symbol in the library & select "linkage"

This will popup a dialog titled "Linkage Properties"

There will be 3 checkboxes that are enabled,

check the one that says "Export for actionscript".

this will enable the "Identifier" textBox,

Enter the ID name you want to use to call this symbol with actionscript & click "Ok"

Now when you want to use this symbol with actionscript you would add it like this,

Code: [ Select ]
attachMovie("linkageID", "instanceName",0);

Where "linkageID" is the name you just gave it i the linkage properties dialog, "instanceName" is what you will use to modify it with actionscript, & 0 is the depth to place it.

You can modify it's properties like so,
Code: [ Select ]
attachMovie("linkageID", "instanceName",0);
instanceName._alpha = 50;
  1. attachMovie("linkageID", "instanceName",0);
  2. instanceName._alpha = 50;


You can attach it to other movieClips as well,
Code: [ Select ]
myMC.attachMovie("linkageID", "instanceName",0);
myMC.instanceName._alpha = 50;
  1. myMC.attachMovie("linkageID", "instanceName",0);
  2. myMC.instanceName._alpha = 50;

Now it will be atached to depth 0 of myMC instead of depth 0 of _level0.
  • koivi
  • Novice
  • Novice
  • koivi
  • Posts: 23
  • Loc: Minnesota

Post 3+ Months Ago

AWESOME! Thanks for all the info. Now I think I can finally get a decent code base to finish the project up the way I want to do it rather than trying to hack things and make sloppy code that is hard to maintain. :)
  • koivi
  • Novice
  • Novice
  • koivi
  • Posts: 23
  • Loc: Minnesota

Post 3+ Months Ago

One more question... How can I create instance names? I've tried:
Code: [ Select ]
instance_name = "point"+i;
attachMovie("map_point",instance_name,0);
instance_name._x=new_x;
instance_name._y=new_y;
  1. instance_name = "point"+i;
  2. attachMovie("map_point",instance_name,0);
  3. instance_name._x=new_x;
  4. instance_name._y=new_y;

Which doesn't seem to work, unless I only have one point being placed....
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Try replacing that zero with i if that's a loop.
It looks like you keep placing new movieclips at the same depth 0.

This is another way,
Code: [ Select ]
attachMovie("map_point", "point"+i, i);/*note the variable being used for the depth*/
this["point"+i]._x=new_x;
this["point"+i]._y=new_y;
  1. attachMovie("map_point", "point"+i, i);/*note the variable being used for the depth*/
  2. this["point"+i]._x=new_x;
  3. this["point"+i]._y=new_y;


Basically this[] means whatever the equation inside [] equates to is going to be a movieClip, object, variable, ect.. name inside the object that AS is currently processing.

there is also,
_root["string"+variable]
_parent["string"+variable]
which are the same deal but the path it starts looking for what is being referenced is different.
  • koivi
  • Novice
  • Novice
  • koivi
  • Posts: 23
  • Loc: Minnesota

Post 3+ Months Ago

This seems to be a bit more complicated than I thought it would be - especially after a week's vacation...

I now have all code necessary code in place to read some XML data and then place a dot on the map using a symbol from the library:

Code: [ Select ]
var myXml = new XML();
var base_url="http://www.example.com/";
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
 if (success) {
  this = this.childNodes[0];
  // be sure we're inside the dataset node
  for (i=0; i<this.childNodes.length; i++) {
   // OK, all the points should be at this level
   if (this.childNodes[i].nodeName == "point") {
    // point data is here, get coordinates and scale accordingly
    point_x = _root.baseMap._xscale/100 * this.childNodes[i].attributes.coord_x;
    point_y = _root.baseMap._yscale/100 * this.childNodes[i].attributes.coord_y;

    // get the other point data
    point_name = this.childNodes[i].attributes.name;
    point_description = this.childNodes[i].attributes.description;
    point_contents = this.childNodes[i].firstChild.nodeValue;
    point_id = this.childNodes[i].attributes.id;
    point_url=base_url+"?point_id="+point_id;
    inst="point"+i;
    
    // create the map point instance & set properties
    attachMovie("map_point",inst,i);
    _root[inst]._x = point_x;
    _root[inst]._y = point_y;
   }
  }
 } else {
  trace("Error, data not loaded.");
 }
};
myXML.load(base_url + "point_data.php");
stop();
  1. var myXml = new XML();
  2. var base_url="http://www.example.com/";
  3. myXML.ignoreWhite = true;
  4. myXML.onLoad = function(success) {
  5.  if (success) {
  6.   this = this.childNodes[0];
  7.   // be sure we're inside the dataset node
  8.   for (i=0; i<this.childNodes.length; i++) {
  9.    // OK, all the points should be at this level
  10.    if (this.childNodes[i].nodeName == "point") {
  11.     // point data is here, get coordinates and scale accordingly
  12.     point_x = _root.baseMap._xscale/100 * this.childNodes[i].attributes.coord_x;
  13.     point_y = _root.baseMap._yscale/100 * this.childNodes[i].attributes.coord_y;
  14.     // get the other point data
  15.     point_name = this.childNodes[i].attributes.name;
  16.     point_description = this.childNodes[i].attributes.description;
  17.     point_contents = this.childNodes[i].firstChild.nodeValue;
  18.     point_id = this.childNodes[i].attributes.id;
  19.     point_url=base_url+"?point_id="+point_id;
  20.     inst="point"+i;
  21.     
  22.     // create the map point instance & set properties
  23.     attachMovie("map_point",inst,i);
  24.     _root[inst]._x = point_x;
  25.     _root[inst]._y = point_y;
  26.    }
  27.   }
  28.  } else {
  29.   trace("Error, data not loaded.");
  30.  }
  31. };
  32. myXML.load(base_url + "point_data.php");
  33. stop();


I then wanted to open a URL up when a button was clicked, so I tried this:
Code: [ Select ]
_root[inst].onRelease = function(){
 getUrl(point_url,"_blank");
}
  1. _root[inst].onRelease = function(){
  2.  getUrl(point_url,"_blank");
  3. }


It looked like it was going to work until I had added a second point of data. Now whenever I click on a point (either of them), I go to the URL of the first one. Another problem that I forsee is I want to be able to place text labels next to the points on mouse over. I initially thought that I could simply create an "over" frame for the symbol with a dynamic text field and simply change the text. However, I do not see how I could get that to work, especially since the points will be close together and the label would need to be much larger than the actual "dot" that is placed on the map.

The movie so far is posted here (I threw it on a server at work for now):
http://waf.rangenet.com/test/test.swf

It's just a base map with the XML data being read - nothing pretty, and no functionality that works yet.

Can someone help me out or at least direct me to reference material for what I need.

TIA!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Code: [ Select ]
getUrl(point_url,"_blank");

Since point_url hasn't been made as a variable that is local to that point it is using the same variable for every point. (the last url found in the xml file).(actually all of your variables are this way, the _x & _y work because they are used before they get changed again)

By attaching the point first, then making theese variables local to that point you'll skip alot of headaches since you'll be able to reference them with "this.varName". Making them local to the points will allow you to have the XML object delete itself after all other actions are carried out as well.

Code: [ Select ]
        var inst = "point"+i;
        _root.attachMovie("map_point",inst,i);
    _root[inst].x = _root.baseMap._xscale/100 * this.childNodes[i].attributes.coord_x;
    _root[inst].y = _root.baseMap._xscale/100 * this.childNodes[i].attributes.coord_y;

    // get the other point data
    _root[inst].name = this.childNodes[i].attributes.name;
    _root[inst].description = this.childNodes[i].attributes.description;
    _root[inst].contents = this.childNodes[i].firstChild.nodeValue;
    _root[inst].id = this.childNodes[i].attributes.id;
    _root[inst].__url=base_url+"?point_id="+_root[inst].id;
    
    // set properties from local variables
    
    _root[inst]._x = _root[inst].x;
    _root[inst]._y = _root[inst].y;
    _root[inst].onRelease = function(){
            getURL(this.__url,"_blank");
    }
  1.         var inst = "point"+i;
  2.         _root.attachMovie("map_point",inst,i);
  3.     _root[inst].x = _root.baseMap._xscale/100 * this.childNodes[i].attributes.coord_x;
  4.     _root[inst].y = _root.baseMap._xscale/100 * this.childNodes[i].attributes.coord_y;
  5.     // get the other point data
  6.     _root[inst].name = this.childNodes[i].attributes.name;
  7.     _root[inst].description = this.childNodes[i].attributes.description;
  8.     _root[inst].contents = this.childNodes[i].firstChild.nodeValue;
  9.     _root[inst].id = this.childNodes[i].attributes.id;
  10.     _root[inst].__url=base_url+"?point_id="+_root[inst].id;
  11.     
  12.     // set properties from local variables
  13.     
  14.     _root[inst]._x = _root[inst].x;
  15.     _root[inst]._y = _root[inst].y;
  16.     _root[inst].onRelease = function(){
  17.             getURL(this.__url,"_blank");
  18.     }


Code: [ Select ]
_root[inst].onRelease = function(){
     getURL(this.__url,"_blank");
    }
  1. _root[inst].onRelease = function(){
  2.      getURL(this.__url,"_blank");
  3.     }

using this inside of the onRelease causes the variable local to that point to be used since = function() { has brought us into the scope of that movieclip.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 19 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.