Image Substitutions

This section provides description for extensions that control image substitutions.

setImageSubstitutions () method

public setImageSubstitutions(substInfoArr:Array) : void
public setImageSubstitutions(substInfo:Object) : void
public setImageSubstitutions(null) : void 

Scaleform version: 2.0.38

Sets image substitutions for substrings to the text field. Strings substitution works only with images embedded into a SWF; these images also should have assigned linkage in order to have an export name. To embed image into a SWF you need to:

  1. Import a bitmap image to the library.
  2. Right-click (Windows) or Control-click (Macintosh) the image in the library and select Linkage from the context menu.
  3. Select Export for ActionScript and Export in first Frame and type the desired name (for example, myImage) in the Identifier text box.
  4. Click OK to set the linkage identifier.

After the image is imported and linkage identifier is assigned, it is necessary to create a BitmapData instance. Here is the example of ActionScript code:

import flash.display.BitmapData;
var imageBmp:BitmapData = BitmapData.loadBitmap("myImage");

NOTE: Do NOT forget the import statement (import flash.display.BitmapData; or to use fully a qualified name - flash.display.BitmapData), otherwise the result will be "undefined"!

If more than one image to be used as a substitution you need to repeat these steps for each image, giving different linkage IDs.

The descriptor of the single substitution is the Object with the following members set:

subString:String Specifies the sub-string that will be replaced by image; this member is mandatory. The maximum length of this sub-string is 15 characters.

image : BitmapData Specifies the image; this is mandatory.

width : Number Specifies the width of image on the screen, in pixels. Optional.

height : Number Specifies the height of image on the screen, in pixels. Optional.

baseLineY : Number Specifies the Y-offset of base line in the image, in pixels of original image (without transformation). Optional. By default, this value is equal to image's height; thus, the bottom of the image appears on a baseline.

id : String Specifies the id of the substitution to use as a first parameter for the "updateImageSubstitution" call. Optional.

The substInfoArr should be the array of such objects, as well as the substInfo should be the instance of the object. The version setImageSubstitutions(substInfo:Object) can set only a single substitutions whereas the version setImageSubstitutions(substInfoArr:Array) sets multiple ones.

Note, every call to setImageSubstitutions adds substitutions to the internal list. To clear all of them call the setImageSubstitutions(null).

It is not necessary to keep a reference to the array with substitutions or to the single descriptor object in ActionScript code after setImageSubstitutions is called; however, keep it if it is necessary to refer it somewhere in the ActionScript code, since there is no way to get the array of substitutions back from the text field.

Parameters
substInfoArr:Array – An array of substitution descriptor objects (see above). substInfo:Object – A single substitution descriptor object (see above). null – Clear all substitutions.

See also: updateImageSubstitution()

Example:

var b1 = BitmapData.loadBitmap("smile1"); 
var b2 = BitmapData.loadBitmap("smile2"); 
var b3 = BitmapData.loadBitmap("smile3"); 
var a = new Array; 
a[0] = { subString:"=)", image:b1, baseLineY:35, width:20, height:20, id:"sm=)" }; 
a[1] = { subString:":-)", image:b2, baseLineY:20, id:"sm:-)" }; 
a[2] = { subString:":\\", image:b3, baseLineY:35, height:100 }; 
a[3] = { subString:":-\\", image:b1 }; 
t.setImageSubstitutions(a);

As soon as a text field contains a substring "=)", without quotes, this substring will be replaced by the image with "smile1" linkage identifier.

updateImageSubstitution () method

public updateImageSubstitution(id:String, image:BitmapData) : void

Scaleform version: 2.0.38

Replaces or removes the image for the text substitution previously created by the setImageSubstitutions function.

Parameters
id:String – An ID of the substitution, same as id member of the descriptor object used for the setImageSubstitutions call.

image:BitmapData – Specifies the new image; if null then the substitution will be removed completely.

See also: setImageSubstitutions()

Example:

t.updateImageSubstitution("sm=)", b3);

The following is an example of animation of embedded images. Update may be done in the onEnterFrame handler or using setInterval. Note, no text reformatting occurs when updateImageSubstitution is called; thus, the size of new image should be the same as the old ones.

var phase = 0;
var b1a = BitmapData.loadBitmap("smile1a");
var b2a = BitmapData.loadBitmap("smile2a");

onEnterFrame = function() 
{
   if (phase % 10 == 0) 
   {
  if (phase % 20 == 0) 
  {
 _root.t.updateImageSubstitution("sm=)", b1);
 _root.t.updateImageSubstitution("sm:-)", b2);
  }
  else 
  {
 _root.t.updateImageSubstitution("sm=)", b1a);
 _root.t.updateImageSubstitution("sm:-)",b2a);
  }
   }
   ++phase;
}