For some reason, when I try to receive a shared property from a Worker (in AS3), the result is always null. That is, I send a value to a Worker using setSharedProperty(), when I retrieve it using getSharedProperty(), it always returns undefined/null.
Here's a simple test I set up:
package
{
import flash.display.Sprite;
import flash.system.Worker;
import flash.system.WorkerDomain;
public class Main extends Sprite
{
private var _worker:Worker;
public function Main():void
{
if (Worker.current.isPrimordial)
{
initMain();
}
else
{
initWorker();
}
}
private function initMain():void
{
_worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
_worker.setSharedProperty("message", "test");
_worker.start();
}
private function initWorker():void
{
_worker = Worker.current;
var message:String = _worker.getSharedProperty("message");
trace(message);
}
}
}
When I trace message, the result is null. Although my main goal is to make an AIR app, I get the same result whether I'm compiling for AIR (3.7) or Flash Player (11.6, for some reason 11.7 doesn't recognise flash.system.Worker as a valid class).
I'm compiling using the Flex SDK, through FlashDevelop. Does anybody know what's wrong, maybe I've missed something in my code?
FlashDevelop now seems to have complete support for debugging workers which really wasn't the case on older versions (you could neither break, or trace inside workers). AIR SDK workers support has also progressed (I remember things working in release would break in debug version)
I just recompiled your sample with AIR SDK 14 release (14.0.0.110)
air14_sdk_win/bin/mxmlc -swf-version=25 -debug=true Main.as
and debugged it with Shockwave Flash Debugger 14,0,0,125 and FlashDevelop 4.6.1.30 and got the expected result:
[Starting debug session with FDB]
Created Worker 2
test
Beware that any element not up-to-date in your debugging chain (sdk/player/debugger) could result in problems for debugging workers
Related
IDE: FLASH CS6 Professional
SDK: AIR 18 for Android
I'm trying to use Workers for my app.
The problem is, it doesn't work that it suppose to be.
public class Main extends Sprite {
private var worker:Worker;
public function Main() {
var ba:ByteArray = this.loaderInfo.bytes;
if(Worker.current.isPrimordial){
worker = WorkerDomain.current.createWorker(ba);
worker.start();
trace("Created Main");
}else{
trace("Created Worker");
}
}
}
It should output
Created Main
Created Worker
but i'm only getting
[SWF] Test.swf - 2831 bytes after decompression
Created Main
[UnloadSWF] Test.swf
Test Movie terminated.
EDIT:
Ok i tried to publish the app and ran it. It works like it should be. But why doesn't it work when i try to ran it with Adobe Debug Launcher ?
This is what it looks like on Android Debug Launcher (ADL)
ADL
This is what it looks like when Published.
Published
EDIT 2:
Tried it with Flash Professional CC 2015. Upgraded to AIR SDK 19.0.0.213.
Still the same result. What a pain Adobe.
It should work, but Flash Pro CS6 doesn't show the worker's trace statements. Check flashlog.txt or send messages from the worker to the main SWF over a MessageChannel.
I made a lib to support use of Workers, if you want use you can check it in GitHub, it is open source project.. hope help ASWorker Link
The implementation is like this
package
{
import com.tavernari.asworker.ASWorker;
import com.tavernari.asworker.notification.NotificationCenter;
import com.tavernari.asworker.notification.NotificationCenterEvent;
import flash.display.Sprite;
public class ASWorkerDemo extends Sprite
{
private var asWorker:ASWorker;
//BOTH AREA
public function ASWorkerDemo()
{
//important know, all class start here will be replicated in all works.
asWorker = new ASWorker(this.stage, uiWorkerStartedHandler, backWorkerStartedHandler);
}
//BOTH AREA END
//UI AREA START
private function uiWorkerStartedHandler():void{
//implement all class or calls for your UI
NotificationCenter.addEventListener("FROM_BACK_EVENT_MESSAGE", onFromBackEventMessageHandler );
}
private function onFromBackEventMessageHandler(e:NotificationCenterEvent):void
{
trace(e.data);
if(e.data == "completed job"){
NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("NEXT_MESSAGE") );
}
}
//UI AREA END
//BACK AREA START
private function backWorkerStartedHandler():void{
//implement all class or calls for your BACK operations
NotificationCenter.addEventListener("NEXT_MESSAGE", uiCallForNextMessageHandler );
}
private function uiCallForNextMessageHandler():void
{
for(var i:int = 0; i < 15; ++i){
NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("FROM_BACK_EVENT_MESSAGE", false, i) );
}
NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("FROM_BACK_EVENT_MESSAGE", false, "completed job") );
}
// BACK AREA END
}
}
Good luck with Workers
Air 20.0.0.204 ADL is also not enjoying anything worker related (from my limited experimentation), however the desktop Flash Player seems to work just fine. This makes debugging convoluted and problematic when you are using Air specific libraries in your project and prefer not to wildcard variable types.
At the moment, workers are only supported on desktop platform. This Adobe developer's guide page specifically mentions "for desktop platforms".
Meanwhile, there is an AS3-Worker-Compat library by Jeff Ward that enables you to write code with Workers that will still work correctly in an environment that doesn't support Workers (of course, in this case your code will run in single-threaded mode).
I think many people are waiting for Adobe to implement workers for mobile, hopefully it will come in the future.
Tried this,
package {
import flash.display.MovieClip;
import flash.events.*;
public class test extends MovieClip {
public function test() {
addEventListener(Event.ADDED_TO_STAGE, registerBtn);
}
private function registerBtn(e:Event):void {
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
}
private function myButtonClick(e:MouseEvent):void {
trace("CLICKED");
}
}
}
Image
And the same code on frame 1, And there's a MovieClip Button on stage having Instance name "Homebtn".
Imports
import flash.events.*;
Importing all classes from a package that originates in flash has zero impact on compile size because they're already present in the Flash Player runtime environment. It's pure monotony that you're required to explicitly declare these imports, but good practice when dealing with third party packages.
Stage Relationship
Document code (i.e., code in the Flash IDE timelines) have a direct relationship to MainTimeline, whereas class files do not. If you want to add button1.addEventListener(MouseEvent.CLICK, myButtonClick); to your class, you're not going to be able to do so unless you:
A: Pass a pointer to the button/stage/root to the class when instantiating your test class:
var myObj:test = new test(root)
B: Wait to add the event listener until after you've given the test object a parent relationship to the stage from which to traverse to the button:
addChild(test);
inside your class...
public function test() {
// constructor code
addEventListener(Event.ADDED_TO_STAGE, registerBtn)
}
private function registerBtn():void {
this.parent.button1.addEventListener(MouseEvent.CLICK, myButtonClick);
}
Turn on Debugging
To find the cause of your bugs, you need to debug your code. If you're using Flash IDE CS6, then you can enable this by going to your publish settings and enabling "Permit Debugging". This will take your ambiguous error...
null object reference at myDocument/doSomething()
...to a much clearer...
null object reference at myDocument/doSomething() package\myClass.as:20
...which now denotes which line in your code to look for your issue.
Use the Debug Console
Use the debugging compile mode to bring up the Debug Console. This will provide you with an immediate look at the line of code in question, as well as the Call Stack, and the state of all available Variables. No programmer should be without it.
Run by going to the menu "Debug > Debug Movie > Debug", or use the keyboard combo CONTROL+SHIFT+ENTER.
Now that you're armed with the know-how to do this on your own, I'll cover what you'd encounter, and how you'd fix it (since you're new).
First, it's flash.events with an "s". So we'll change that.
Next, compiling it we get the following errors:
So we see on line 7 of our test.as class: you've placed the timeline code into the class.
var myObj:test = new test(root);
addChild(test);
You don't want to instantiate you class from within itself as it'll never get instantiated. Think of your code as a railroad. The train starts with your timeline code, and only runs on the rails set before it. Your class is floating off to the side, ready with all its interesting turns and zigzags, but you have to add it to the rails for it to be actually run. That's instantiation; we're copying that path onto the current track, and the train runs on it.
So, we get rid of lines 6 & 7, and we're left with Access of possibly undefined property Homebtn. Calling this.parent is actually a getter function, and it returns a DisplayObjectContainer. Because AS3 is a strongly datatyped language, the compiler will know that there is no such property "Homebtn" on DisplayObjectContainers (silly compiler). But of course, you know it's there (or at least it will be by the time this code runs). A simple way of getting around that is by making it evaluate the reference at run-time.
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
By encapsulating the button name as a string and within brackets, we've done that.
Now we recompile again, and get the following:
This is because all event listeners receive one argument: an event object. You may not use it, but not having a variable to hold it is a no-no.
private function registerBtn(e:Event):void {
As a final point. All class functions need to be denoted as to what namespace they exist in. myButtonClick needs one, so we'll add it as private since no external (ie., non-class based) functions need access to it.
Here's your revised code:
test.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class test extends MovieClip {
public function test() {
addEventListener(Event.ADDED_TO_STAGE, registerBtn);
}
private function registerBtn(e:Event):void {
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
}
private function myButtonClick(e:MouseEvent):void {
trace("CLICKED");
}
}
}
test.fla (timeline code on frame 1)
import test;
var Homebtn:MovieClip = new MovieClip();
Homebtn.graphics.beginFill(0xFF0000, 1);
Homebtn.graphics.drawRect(0, 0, 150, 25);
Homebtn.graphics.endFill();
addChild(Homebtn);
var testObj:test = new test();
addChild(testObj);
I have an Air application. It consist of an HTML component. I create a flex application and launch this application in HTML component of Air Application. I am able to capture the trace output of Air application but I can not capture trace output of flex application. As this flex application is launched in Air application's HTML component. I use vizy that output the log prints. How can I capture the trace output of flex web application. Thanks
One possible solution, use mx.logging.Log for debug traces and custom LogTarget to capture them.
It would go something like this. Declare the necessary objects:
private var logTarget:MyLogTarget= new MyLogTarget();
private var myLog:ILogger;
Set up logging where you initialize the application:
myLog=Log.getLogger("MyApp");
Log.addTarget(logTarget);
Log stuff:
myLog.info("Something something.");
myLog.warn("This is weird!");
myLog.error("This shouldn't happen!");
The meat of the solution is the custom log target, MyLogTarget.as:
package
{
import mx.logging.LogEvent;
import mx.logging.targets.LineFormattedTarget;
public class MyLogTarget extends LineFormattedTarget
{
public var log:Vector.<String>=new Vector.<String>;
public function MyLogTarget()
{
super();
}
override public function logEvent(event:LogEvent):void
{
trace(event.message);
log.push(event.message+"\n");
}
}
}
This particular implementation just stores all traces in a vector of Strings, but you can modify it to save the log to disk, send it to a service, trace it on screen or whatever works for you.
I wanted to enable all compiler warnings in Flex to resolve them in my code. But there is one warning which I can't figure out how to solve it. Here is some example code:
package lib
{
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
public class player
{
private function tmp(event:NetStatusEvent):void
{
}
public function player():void
{
super();
var connection:NetConnection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, tmp);
}
}
}
On compiling with -warn-scoping-change-in-this I'm getting the following warning:
/var/www/test/src/lib/player.as(16): col: 59 Warning: Migration issue: Method tmp will behave differently in ActionScript 3.0 due to the change in scoping for the this keyword. See the entry for warning 1083 for additional information.
connection.addEventListener(NetStatusEvent.NET_STATUS, tmp);
Putting tmp as function inside player() will work but this is not what I want. I have even tried to use this.tmp as callback but there is no difference. Does somebody know how to solve this compiler warning?
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/compilerWarnings.html
This is a code migration warning. This warning is generated when a method of an object is used as a value, usually as a callback function. In ActionScript 2.0, functions are executed in the context they are called from. In ActionScript 3.0, functions are always executed in the context where they were defined. Thus, variable and method names are resolved to the class that the callback is part of, rather than relative to the context it is called from, as in the following example:
class a
{
var x;
function a() { x = 1; }
function b() { trace(x); }
}
var A:a = new a();
var f:Function = a.b; // warning triggered here
var x = 22;
f(); // prints 1 in ActionScript 3.0, 22 in ActionScript 2.0
That warning is only placed there to let you know that the behavior of your code might have changed in case you are migrating your code from AS2 to AS3 (which the compiler has no way of knowing beforehand). You should only enable the compiler option -warn-scoping-change-in-this when you are migrating your code from AS2 to AS3.
So, as I said in the comments, you shouldn't worry about that warning, since obviously seing your code that is not your case and you don't need that compiler option enabled.
I'm trying to play a remote AAC file in ActionScript 3 in Flash CS3 and am currently using this code:
var url:String = "http://a1.phobos.apple.com/us/r1000/020/Music/d4/50/94/mzm.kjjofihr.aac.p.m4a";
var connect_nc:NetConnection = new NetConnection();
connect_nc.connect(null);
var stream_ns:NetStream = new NetStream(connect_nc);
stream_ns.play(url);
(This is based on: http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player_03.html)
No errors are thrown, but no sound is played. I get the same behavior with a local AAC file and with a local MP4 video.
If I use a URL or file path that isn't a streamable file, I get a NetStream.Play.StreamNotFound error, which I'm guessing means that the stream is found in the case of a valid URL. If I use a local FLV, its audio is played just fine.
If I add the following listener and trace(evt.info.code) in netStatusHandler, I only see any codes (e.g. NetStream.Play.Start) traced with the FLV. No codes are traced with the AAC or MP4.
stream_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
The same is true of adding this listener (i.e. the onMetaData argument is only traced with the FLV, not with the other file types), with metaDataListener defined as an object with an onMetaData method that traces its argument.
stream_ns.client = metaDataListener;
Any ideas of what might be going wrong here, or how to diagnose it?
Thanks!
As stated here http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player_03.html what you are doing is correct.
var connect_nc:NetConnection = new NetConnection();
connect_nc.connect(null);
var stream_ns:NetStream = new NetStream(connect_nc);
stream_ns.play("RE-Sample.m4a");
However, the Actionscript Language reference about nestream found here:
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/NetStream.html#play%28%29
states that:
play () method
...
...
When you use this method without Flash Media Server, there are security considerations. A file in the local-trusted or local-with-networking sandbox can load and play a video file from the remote sandbox, but cannot access the remote file's data without explicit permission in the form of a cross-domain policy file. Also, you can prevent a SWF file running in Flash Player from using this method by setting the allowNetworking parameter of the the object and embed tags in the HTML page that contains the SWF content.
...
...
Parameters
... arguments — The location of the video file to play, as a URLRequest object or a string. In Flash Player and in AIR content outside of the application security sandbox, you can play local video files that are stored in the same directory as the SWF file or in a subdirectory; however, you can't navigate to a higher-level directory.
So it's probably a security sandbox issue.
Everything in ActionScript 3.0 is event based (with few random exceptions where callbacks are used).
You need to listen for the NetStatusEvent with info.code "NetConnection.Connect.Success" in order to be allowed to call the NetStream.play() function.
Here's something that works (I just wrote it now, and tested it for you):
package
{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
public class MainDocument extends Sprite
{
private var _connection:NetConnection=new NetConnection();
private var _netStream:NetStream=null;
private var _strM4AURL:String="http://a1.phobos.apple.com/us/r1000/020/Music/d4/50/94/mzm.kjjofihr.aac.p.m4a";
//constructor
public function MainDocument():void
{
this._connect();
}
private function _connect():void
{
this._connection.close();
this._connection=new NetConnection();
this._connection.addEventListener(NetStatusEvent.NET_STATUS, this._netStatusHandler);
this._connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this._asyncErrorHandler);
this._connection.connect(null);
}
private function _netStatusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success":
this._requestAudio();
break;
}
}
private function _requestAudio():void
{
if(this._netStream!==null)
this._netStream.close();
this._netStream=new NetStream(this._connection);
this._netStream.addEventListener(NetStatusEvent.NET_STATUS, this._netStatusHandler);
this._netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this._asyncErrorHandler);
this._netStream.checkPolicyFile=false;
this._netStream.play(this._strM4AURL);
}
private function _asyncErrorHandler(event:AsyncErrorEvent):void
{
trace(event);
}
}
}
Consult the ActionScript 3.0 Language Reference for more information.
There's a good chance what Oliver is saying is true as your not getting any feedback from any event listeners associated with the NetStream, and you are getting a StreamNotFound response.
StreamNotFound when not connecting to a FMS means that you either have the path wrong or its not seeing it, due to a security issue.