as3scriptinglib (dynamic code loading) is compiling the code but is not executed - actionscript-3

I have problems implementing https://code.google.com/p/as3scriptinglib/ in my project.
The code is compiled, but it simply is not executed.
public function Main():void {
var loader:CompilerLoader = new CompilerLoader();
loader.addEventListener(CompilerEvent.INIT, compilerInit);
loader.load("ESCompilerSWF.swf");
trace("INIT");
}
private function compilerInit(event:CompilerEvent):void {
var compiler:ICompiler = event.compiler;
trace("PREPARE",compiler.initialized);
var str:String = 'trace("HELLO WORLD");';
try {
var script:IScript = compiler.compileAndLoad(str, new ScriptContext(this));
script.addEventListener(ScriptErrorEvent.SCRIPT_ERROR, trace);
trace("Script created");
} catch (e:Error) {
trace("ERROR", e.message);
}
trace("READY");
}
And it outputs:
INIT
PREPARE true
Script created
READY
So, as you can see the code is not executed by as3scriptinglib.
Help.

I have solved the problem. The class that execute the script was Garbage Collected, so I change:
var script:IScript = compiler.compileAndLoad(str, new ScriptContext(this));
to a variable outside the function, like:
script = compiler.compileAndLoad(str, new ScriptContext(this));
And it works.
:)

Related

How to run a method when onPublish is triggered in Adobe Media Server?

I have the following code inside Adobe Media Server's main.asc (latest version, 5.0.10 I think):
application.onPublish = function (clientObj, streamObj) {
for (var i = 0; i < application.clients.length; i++){
application.clients[i].call("streamConnected");
}
}
And this code inside my ActionScript (3.0) file, connected to my flash file:
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = { onBWDone: function():void{}, streamConnected: function():void{} };
nc.connect(videoURL);
...
public function streamConnected(...rest):void {
trace("Stream Connected");
}
I'm not too sure what my code means - most of it has been sourced from various sections of the Internet, so any help would be greatly appreciated.
Using your current code, the only function that will be executed is the empty one which is defined inside the nc.client object because the streamConnected() function is not attached to the nc.client's streamConnected property.
So, to get the "Stream Connected" message, you can change that anonymous function like this for example :
nc.client = {
onBWDone: function():void{},
streamConnected: function(...rest):void {
trace("Stream Connected");
}
};
or simply you can use your existing streamConnected() function :
nc.client = {
onBWDone: function():void{},
streamConnected: streamConnected
};
Hope that can help.

How do I use this URLRequest script?

I started learning ActionScript 3 a week ago and have stumbled across a huge learning curve. I found this script on the internet:
var _loader:URLLoader;
var _request:URLRequest;
function loadData():void {
_loader = new URLLoader();
_request = new URLRequest("http://www.travoid.com/game/Purchase.php?gid=1");
_request.method = URLRequestMethod.POST;
_loader.addEventListener(Event.COMPLETE, onLoadData);
_loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFailedToLoad);
_loader.load(_request);
}
function onLoadData(e:Event):void {
trace("onLoadData",e.target.data);
}
function onDataFailedToLoad(e:IOErrorEvent):void {
trace("onDataFailedToLoad:",e.text);
}
This all seems to work and is generating no errors or output, however my issue comes about when I use this next part of code (which I made)
function vpBuy(e:MouseEvent):void{
loadData();
if (e.target.data == "false") {
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
I get this error:
ReferenceError: Error #1069: Property data not found on
flash.display.SimpleButton and there is no default value. at
travoid_fla::MainTimeline/vpBuy() onLoadData
The part that is probably throwing this is:
if (e.target.data == "false") {
I was hoping e.target.data was what stored the value on the web page (which displays as false) but apparently not. With the code I found on the internet, what stores the information on the web page?
Thanks,
Ethan Webster.
The URLLoader load method is asynchronous, you have to wait the server response before triyng to get the result.
The functions onLoadData and onDataFailedToLoad are there to do that. When the response is well received the function onLoadData is called and you can get the data in e.target.data or _loader.data
The error in your function vpBuy is you try to access the data property on the object that triggered the MouseEvent (maybe a Button) and that object don't have such variable.
Try the following:
/** button clicked load the datas from the server **/
function vpBuy(e:MouseEvent):void
{
// load the datas from the server
loadData();
}
/** the datas are well loaded i can access them **/
function onLoadData(e:Event):void
{
trace("onLoadData",e.target.data);
if( e.target.data == "false" )
{
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
Hope this could help you :)

Flash AS3 Call a WSDL in a for loop

I'm using the Flex SOAP web service, connecting to our WSDL and everything is dandy. However, I'm new to web services and the web guy is on holiday, so I'm a bit confused. The first thing I'm doing is running a check connection:
private function configXMLHandler(event:LoaderEvent):void {
fws.wsdl = checkWSDL;
fws.loadWSDL();
fws.addEventListener(LoadEvent.LOAD, wsdlLoaded);
}
private function wsdlLoaded(event:LoadEvent):void {
checkAbstract = fws.getOperation("retrieveAssetIdbyLabel");
checkAbstract.arguments = ["poll-asset-do-not-remove"];
var token:AsyncToken = checkAbstract.send();
token.addResponder(new Responder(checkAbstractResult, checkAbstractError));
}
private function checkAbstractError(event:FaultEvent):void {
trace('Error in the WSDL');
}
private function checkAbstractResult(event:ResultEvent):void {
if (event.result.returnCode == 0) {
trace('Web service check ok');
initContentLoader();
} else {
trace('Error in the WSDL');
)
}
}
This works fine, I get the result I expect, and so I move on. I then need to iterate through an XML list and call the same web service function for each asset in that XML, my thinking was to use a loop:
private function initContent(event:LoaderEvent):void {
assetList = event.target.content.asset;
for (var i:int = 0; i < assetList.length(); i++) {
assetAbstract = fws.getOperation("retrieveAssetIdbyLabel");
assetAbstract.arguments = [assetList[i + assetCount].assetLabel]; //get the current index in the xmllist + the assetCount, grab the corresponding assetLabel from the XML and pass that to the web service
trace(assetAbstract.arguments);
var assetToken:AsyncToken = assetAbstract.send();
assetToken.addResponder(new Responder(getAssetResult, getAssetError));
}
}
private function getAssetResult(event:ResultEvent):void {
var treasuresAsset:TreasuresAsset = new TreasuresAsset(event.result.returnCode, assetList[assetCount].asset.assetLabel, assetList[assetCount].asset.assetImage, assetList[assetCount].asset.assetDescription);
addChild(treasuresAsset);
assetCount++; //increase the asset count
}
private function getAssetError(event:FaultEvent):void {
trace(event.fault);
trace('An error occured when we tried to get an asset id in the loop');
}
I now get an error:
Error opening URL 'http://www.nhm.ac.uk/web-services/VisitorService/'
SOAPFault (Server): org.apache.axis2.databinding.ADBException: Unexpected subelement RetrieveAssetIdbyLabel
My immediate thought was that I need to create a new instance of the web service for each asset in the xml, and repeat my first code over and over. Can I use the web service only once, do you need to recreate the entire procedure?
Thanks.
OK, so I figured this out, and it was a simple XML namespace issue.
I replaced:
assetAbstract.arguments = [assetList[i + assetCount].assetLabel];
With:
var sender:String = assetList[i + assetCount].assetLabel;
assetAbstract.arguments = [sender];
And all is working.

Actionscript 3 Sound ioError Problem

i am having trouble with sound. i am loading it dynamically, url comes from flashvars.
App is working actually but stil gives error, unhandled ioError. but i already handled it.
`
var sound:Sound = new Sound()
try{
sound.load(new URLRequest(req));
} catch(e:IOError){
trace("catch ioerror");
}
sound.addEventListener(IOErrorEvent.IO_ERROR, function(evt:IOErrorEvent):void { trace("error:",evt) } );
sound.addEventListener(Event.COMPLETE, function(e:Event):void{
channel = sound.play(0,int.MAX_VALUE);
});`
Just to rule it out, try commenting out the line channel = sound.play(0,int.MAX_VALUE); perhaps it is this that is throwing the error and not the load, and you have no catch around this.
Try a neater approach in terms of code, might just have issues with your layout and such:
var request:URLRequest = new URLRequest(req);
sound.load(request);
sound.addEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.addEventListener(Event.COMPLETE, _complete);
function _ioError(e:IOErrorEvent):void
{
trace("File was not found");
_removeListeners();
}
function _complete(e:Event):void
{
channel = sound.play(0,int.MAX_VALUE);
_removeListeners();
}
function _removeListeners():void
{
sound.removeEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.removeEventListener(Event.COMPLETE, _complete);
}

(Flash CS4/AS3) Error #1007: Instantiation attempted on a non-constructor

Having a bit of a problem creating an instance of an object. Bear in mind that this is timeline based and NOT an external class…
var foo:Object {
var a:String;
var b:String;
}
var new_foo:Object;
function makeFoo():void
{
new_foo = new foo();
}
function doStuff(e:MouseEvent):void
{
makeFoo();
}
Everything runs fine until the 'new_foo = new foo();' bit, at which point I get the #1007 error.
Any ideas?
the problem is your object. missing some sintax, here is how to declare a object with two empty strings:
var foo:Object = {
a:"",
b:""
}