Using Print2Flash Document API for ActionScript2 documents from Flash with ActionScript3

Note: Embedding of ActionScript2 documents in
other Flash movies compiled with ActionScript3 support and using Print2Flash Document API from
it is possible but not recommended. The method described below is
deprecated due to poor interoperability support by the Flash Player between
ActionScript2 and ActionScript3 movies. It is preferable to use ActionScript2
documents with movies compiled with ActionScript2 support. See
Using Print2Flash Document API from
Flash with ActionScript 2
help topic for more information.
If you still need to be able to load Print2Flash documents in other Flash movies
compiled with ActionScript3 support or in Flex applications, consider using
ActionScript3 documents instead. You can control the ActionScript version using
Document Template field in the Output Tab of Document Options window. See
Using Print2Flash Document API
for ActionScript3 documents from Flash and
Using Print2Flash Document API from Flex
for ActionScript3 documents help topics for more information.
To facilitate the task of loading of ActionScript2 documents into other Flash
movies created in Adobe Flash CS Professional with ActionScript3 support, you may make use of Print2FlashDoc class which is included in Print2Flash SDK.
First, copy the folder named Print2Flash to a directory located at class path
of your project It can be the root directory of your project.
Then, you may use the code such as the following to load a Print2Flash document:
import Print2Flash.*;
var P2FDoc:Print2FlashDoc=new Print2FlashDoc("sample.swf",10,50,500,350,this);
|
The meanings of Print2FlashDoc constructor parameters (from left to right)
are:
- url - path of Print2Flash document to load;
- x - x coordinate of the Print2Flash document movie;
- y - y coordinate of the Print2Flash document movie;
- width - width of Print2Flash document movie;
- height - height of Print2Flash document movie;
- parent- parent object of Print2Flash document movie.
Note that to load a document in Flash and access it via Print2Flash Document
API you should clear "Disable
Print2Flash Document API support" option when converting the document.
Then you may call any Document API functions using the created Print2FlashDoc
class instance reference. For example:
P2FDoc.setCurrentPage(5);
|
Invoking functions that return values is a bit tricky in ActionScript 3 as interaction
between ActionScript 3 and Print2Flash documents is asynchronous. It means that you should
break your function calling code into two parts: the first part for calling the
function and another part for obtaining the result. When
calling such functions you should pass an additional argument at first position
which must denote the function that will receive the return value. For example,
to call getCurrentPage
function you should use the code such as this:
public function ongetCurrentPage(page:Number):void {
trace(page);
}
P2FDoc.getCurrentPage(ongetCurrentPage);
|
Here ongetCurrentPage is the function that receives the result of the
getCurrentPage function
which is passed to it as a parameter. When calling
getCurrentPage function,
the first argument denotes ongetCurrentPage function as the function to
call for receiving the return value.
To handle events fired by Print2Flash documents, you should register an event
handler using standard addEventListener method. For example, to handle
onPageChanged
event you need to declare the event handler like this:
function onPageChanged(e:PageChangedEvent) {
trace(e.page.toString())
}
P2FDoc.addEventListener("onPageChanged", onPageChanged);
|
The event processing code may reference an object passed to the event handler
method as a parameter which
holds event detailed information. Each event has a special type of event
object. These types are detailed in the table below.
|
Event |
Event object type |
Event object properties |
| onPageChanged |
Print2Flash.PageChangedEvent |
page:Number - page number |
| onZoomChanged |
Print2Flash.ZoomChangedEvent |
zoom:Number - zoom level |
| onSelection |
Print2Flash.Selection |
selection:Object - text selection described
by object of SelectionRange
type |
| onToolChanged |
Print2Flash.ToolChangedEvent |
tool:String - tool represented by text as
in getCurrentTool
function |
| onVisibleAreaChanged |
Print2Flash.VisibleAreaEvent |
area:Object - visible area described by
object of VisibleArea type |
| onLoaded |
flash.events.Event |
No properties |
| onUnloadCompletedEvent |
Print2Flash.UnloadCompletedEvent |
No properties |
If you want an event handler to stop receiving event notifications, you need
to remove the handler using removeEventListener method:
P2FDoc.removeEventListener("onPageChanged", onPageChanged);
|
Print2FlashDoc component exposes an event additional to
Print2Flash Document API events named
onLoaded. It is called when the document is ready to be accessed by API
methods. You should not call any methods of Print2FlashDoc component before
onLoaded event is fired.To remove (unload) a loaded document properly, you should call unload
method of the Print2FlashDoc class. This is to ensure that all resources used by
the document are freed. If you want to load another document reusing the same
Print2FlashDoc class instance, you may not do this until onUnloadCompletedEvent
event is fired. If you try to load a document before this event is fired, you'll
get an exception.
Print2Flash SDK contains a sample of
embedding a Print2Flash document into a Flash movie which uses ActionScript 3.
|