PDA

View Full Version : how to use external AS3 in flex ??


trendykarn
11-11-2009, 08:03 AM
hello,
I want to use an external actionscript file with flex button.
Actually there is a function defined in actionscript file named nextImage().
Now what i want to do is, I want to use this function for button in flex on click.

How to do this plzz help me....

trendykarn
11-11-2009, 08:51 AM
function loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
image = [];
caption = [];

total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
caption[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
}
firstImage();
} else {
content = "file not loaded!";
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("images.xml");
/////////////////////////////////////
listen = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
prevImage();
} else if (Key.getCode() == Key.RIGHT) {
nextImage();
}
};
Key.addListener(listen);
previous_btn.onRelease = function() {
prevImage();
};
next_btn.onRelease = function() {
nextImage();
};
/////////////////////////////////////
p = 0;
this.onEnterFrame = function() {
filesize = image1.getBytesTotal();
loaded = image1.getBytesLoaded();
if (loaded != filesize) {

if (image1._alpha<100) {
image1._alpha += 10;
}
}
};
function nextImage() {
if (p<(total-1)) {
p++;
if (loaded == filesize) {
image1._alpha = 0;
image1.loadMovie(image[p], 1);
textArea.text = caption[p];
image1_num();
}
}
}
function prevImage() {
if (p>0) {
p--;
image1._alpha = 0;
image1.loadMovie(image[p], 1);
textArea.text = caption[p];
image1_num();
}
}
function firstImage() {
if (loaded == filesize) {
image1._alpha = 0;
image1.loadMovie(image[0], 1);
textArea.text = caption[0];
image1_num();
}
}
function picture_num() {
current_pos = p+1;
pos_txt.text = current_pos+" / "+total;
}
}

i just want this code to be converted for flex.
Plzz help me....
thank you

suchislife801
11-12-2009, 06:10 AM
Man that sounds confusing.

a. What are you trying to do? (The purpouse of your project)
b. This is Action Script and can be used in flex.

trendykarn
11-12-2009, 07:11 AM
sorry if u are in confusion with my code..

as there is a function defined nextimage();
and i have a button in flex project . i just want to evoke this function when pressed that button.

thankyou

Barna Biro
11-12-2009, 07:02 PM
Hi trendykarn,

The code you have posted is ActionScript 2.0 which won't work with current Flex versions. If I'm not mistaking, only Flex Server 1.0 and 1.5 used ActionScript 2.0 but you really don't want to use those versions ( they are really old ).

I'm afraid that there is not easy or straight solution for your problem.

suchislife801
11-13-2009, 05:41 AM
XML FILE NAME: pictures.xml

XML FILE STRUCTURE:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<root>

<record>
<picture>H:\\picture1.jpg</picture>
<caption>This is the caption for picture 1.</caption>
</record>

<record>
<picture>H:\\picture2.jpg</picture>
<caption>This is the caption for picture 2.</caption>
</record>

<record>
<picture>H:\\picture3.jpg</picture>
<caption>This is the caption for picture 3.</caption>
</record>

<record>
<picture>H:\\picture4.jpg</picture>
<caption>This is the caption for picture 4.</caption>
</record>

<record>
<picture>H:\\picture5.jpg</picture>
<caption>This is the caption for picture 5.</caption>
</record>

</root>

pix.mxml file code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
<mx:Script>
<![CDATA[

private var arrPix:Array;

private var firstPix:int;
private var currPix:int;
private var lastPix:int;

private function init():void
{

arrPix = new Array();

var doc:String = "H:\\pictures.xml?nocache=" + Math.random();
var request:URLRequest = new URLRequest(doc);
var loader:URLLoader = new URLLoader();

loader.load(request);
loader.addEventListener(Event.COMPLETE, docLoad_Complete);

}

private function docLoad_Complete(event: Event): void
{

// Once document has finished loading, add it to string variable doc.
var xmlDoc:XMLDocument = new XMLDocument();
xmlDoc.ignoreWhite = true;
xmlDoc.parseXML(String(event.currentTarget.data));

var root:XMLNode = xmlDoc.firstChild;
parseRoot(root);

}

private function parseRoot(root:XMLNode): void
{
var xmlRoot:Array = root.childNodes;

for each(var item:XMLNode in xmlRoot) {

switch(item.nodeName.toString())
{
case 'record':
parseRecord(item);
break;
}

}

firstPix = 0;
lastPix = arrPix.length -1;

pixFrame.load(arrPix[firstPix].picture);
lblCaption.text = arrPix[firstPix].caption;

}

private function parseRecord(subroot:XMLNode): void
{
var xmlRecord:Array = subroot.childNodes;

var obj:Object = new Object();

obj.picture = XMLNode(xmlRecord[0]).childNodes.toString();
obj.caption = XMLNode(xmlRecord[1]).childNodes.toString();

arrPix.push(obj);

}

private function firstPic():void
{
// First Picture on the array.
pixFrame.load(arrPix[0].picture);
lblCaption.text = arrPix[0].caption;
currPix = 0;
}

private function lastPic():void
{
// Last Picture on the array.
pixFrame.load(arrPix[lastPix].picture);
lblCaption.text = arrPix[lastPix].caption;
currPix = lastPix;
}

private function prevPic():void
{

switch (currPix)
{
case firstPix: break;

default:
currPix--;
pixFrame.load(arrPix[currPix].picture);
lblCaption.text = arrPix[currPix].caption;
break;
}


}

private function nextPic():void
{

switch (currPix)
{
case lastPix: break;

default:
currPix++;
pixFrame.load(arrPix[currPix].picture);
lblCaption.text = arrPix[currPix].caption;
break;
}

}

]]>
</mx:Script>
<mx:Canvas width="350" height="275" backgroundColor="#F5F5F5">
<mx:Button x="93" y="243" label="Pevious" width="75" id="btnPrevious" click="prevPic()"/>
<mx:Button x="182" y="243" label="Next" width="75" id="btnNext" click="nextPic()"/>
<mx:Image x="10" y="10" width="330" height="204" id="pixFrame"/>
<mx:Button x="10" y="243" label="First" width="75" id="btnFirst" click="firstPic()"/>
<mx:Button x="265" y="243" label="Last" width="75" id="btnLast" click="lastPic()"/>
<mx:Label x="10" y="219" text="Caption" width="330" id="lblCaption"/>
</mx:Canvas>

</mx:Application>