Multithreading in action script using Worker Class - actionscript-3

My main Thread is getting suspended, I'm new to action script.
what can be the reason for this error.
Main Thread (Suspended: VerifyError: Error #1014: Class flash.system::Worker could not be found.)

This error is regarding the worker process not getting initialized. Try to update the flash player to latest version and flash builder if you are using it.
Please check this link for more
https://forums.adobe.com/thread/1037293

If you had problem to use or implement Workers I made a lib to help this, and if you want you can contribute.. Just check in GitHub 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

Related

How to free memory after use of FileRerence?

See [Solution]
FileReference.load(); does not have a function to unload, just as there is new Loader ().unload();.
Must be a "BUG" from Flash or FileReference needs improvement, type in a new version add a function like this: FileReference.unload();
Or am I mistaken and exists a SOLUTION?
I tried to set "NULL" to a variable of type FileReference, but clearly this does not work for the Flash works with GC (garbage collector), but this is not the focus of the question.
The problem is that a lot of memory when loading multiple files with new FileReferenceList is necessary, but I can not free memory after the process.
How to free memory after use of FileRerence?
See my code:
Main.as
package {
import com.mainpackage.LoaderTestCase;
import flash.net.FileReferenceList;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.events.Event;
import flash.display.MovieClip;
public class Main extends MovieClip {
private var listFiles:Array;
private var allTypes:Array;
private var fileRef:FileReferenceList;
private var test:int;
public function Main()
{
test = 0;
listFiles = [];
allTypes = [];
fileRef = new FileReferenceList();
fileRef.addEventListener(Event.SELECT, select);
fileRef.browse(allTypes);
}
private function select(e:Event):void
{
listFiles = fileRef.fileList;
for(var i:uint=0, j:uint=listFiles.length; i<j; i++)
{
insert(i);
}
}
private function insert(c:int):void
{
var fire:LoaderTestCase = new LoaderTestCase(listFiles[c]);
fire.destroy(function():void
{
//Delete LoaderTestCase after timeout ???
fire = null;
test++;
if(test>=listFiles.length) {//Remove FileReference
fileRef.removeEventListener(Event.SELECT, select);
fileRef = null;
for(var i:uint=0, j:uint=listFiles.length; i<j; i++) {
listFiles[i] = null;
}
listFiles = null;
trace("Clear memory");
}
});
}
}
}
LoaderTestCase.as
package com.mainpackage
{
import flash.net.FileReference;
import flash.events.Event;
import flash.display.Loader;
public class LoaderTestCase
{
private var file:FileReference;
private var loader:Loader;
private var callback:Function;
public function LoaderTestCase(e:FileReference)
{
file = e;
trace("OPEN: " + file.name);
file.addEventListener(Event.COMPLETE, loadFile);
file.load();
e = null;
}
public function loadFile(e:Event):void
{
file.removeEventListener(Event.COMPLETE, loadFile);
trace("LOAD: " + file.name);
file = null;
e = null;
callback();
}
public function destroy(a:Function):void
{
callback = a;
}
}
}
Remove the event listeners before you null the listening object.
You could use weak references to let the listeners be removed when the object is Garbage Collected.
object.addEventListener( ......, ......., false, 0, true );
for example, in your LoadFile function:
...
LoadFile(file);
}
});
...
should be:
...
LoadFile(file);
}
}, false, 0, true );
...
Or you will have to remove them manually.
To do that you will need to move the event handlers into new named functions.
Also you will need an array for storing the references to listeners and listening objects, to be able to remove the listeners AFTER the listeners are not needed any more and BEFORE nulling the listening object.
PLEASE NOTE:
When you are testing it and watching the current memory usage, make sure to force the Garbage Collector when you feel the memory usage should have dropped by now, but it didn't.
GC kicks in when it wants and very not necessarily after something has been nulled on unloaded.
To be clear, I am only talking about forcing GC during the development/testing.
Even if you null every reference to an object, it won't be deleted immediately from the memory. You have to remove the event listeners aswell. Also, never use "unnamed" functions... it is harder to remove a listener when the event calls an unnamed function. So create a new function, and call that one. For example:
test.contentLoaderInfo.addEventListener(Event.COMPLETE, contentLoaderInfoComplete);
...
function contentLoaderInfoComplete(e:Event){
test.contentLoaderInfo.removeEventListener(Event.COMPLETE, contentLoaderInfoComplete);
test.unload();
test = null;
}
This will clean the memory.
I reached my goal, if I so did FileReferenceList.fileList[5] = null; (when the "sixth file" is not being used more) Flash memory immediately frees this specific FileReference.
In the others words:
This not work:
private var file:FileReference;
...
file = FileReferenceList.fileList[5];
...
file = null;
But this worked:
FileReferenceList.fileList[5] = null;
Worked on all Desktop/Plugins/PepperFlash.
See worked code:
package {
import flash.net.FileReferenceList;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class Main extends Sprite
{
private var listFiles:Array;
private var allTypes:Array;
private var fileRef:FileReferenceList;
private var tmpFile:FileReference;
private var i:uint=0;
private var j:uint=0;
private var timer:uint;
private var imageTypes:FileFilter;
private var enable:Boolean;
public function Main()
{
imageTypes = new FileFilter(
"Images (*.JPG;*.JPEG;*.JPE;)", "*.jpg; *.jpeg; *.jpe;"
);
listFiles = [];
allTypes = [imageTypes];
eventBrowse(true);
}
private function eventBrowse(a:Boolean):void
{
enable = a;
if(a===true) {
stage.addEventListener(MouseEvent.CLICK, browse);
fileRef = new FileReferenceList();
fileRef.addEventListener(Event.SELECT, select);
} else {
fileRef.removeEventListener(Event.SELECT, select);
fileRef = null;
stage.removeEventListener(MouseEvent.CLICK, browse);
}
}
private function browse(e:MouseEvent):void
{
if(enable===true) {
fileRef.browse(allTypes);
}
}
private function select(e:Event):void
{
listFiles = fileRef.fileList;
eventBrowse(false);
i=0;
j=listFiles.length;
if(j>0) {
loadNextFile();
}
}
private function loadNextFile():void
{
if(!(i<j)) {
listFiles = null;
trace("Free memory???");
trace("--------------");
trace("listFiles:"+ listFiles);
trace("allTypes:" + allTypes);
trace("fileRef:" + fileRef);
trace("tmpFile:" + tmpFile);
trace("i:" + i);
trace("j:" + j);
trace("timer:" + timer);
trace("--------------");
eventBrowse(true);
return;
}
tmpFile = listFiles[i];
trace("Initiate load:" + tmpFile.name);
tmpFile.addEventListener(Event.COMPLETE, loadedFile);
tmpFile.load();
}
private function loadedFile(f:Event):void
{
trace(listFiles);
trace("Finished load:" + tmpFile.name);
tmpFile.removeEventListener(Event.COMPLETE, loadedFile);
tmpFile = null;
listFiles[i] = null;
i++;
loadNextFile();
}
}
}
The issue is a combination of all the things noted above.
You do need to remove the event listener(s) manually. While its possible to use weak references its better if you make a habit of keeping track of the listeners you register and always unregister them properly. This way you can better avoid memory leaks(not quite a memory leak but has a similar effect) you didn't expect or weren't thinking about.
You are creating event listeners in a loop and re-using the same function to handle all of them. If you do this you must somehow get a reference to the original loader and remove the event listener from it. I have no idea how you tried to incorporate Zhafur's answer but if you re-used file reference for each new file that will be the reason its still not working. Perhaps you can update you example above with the code you currently have so we can critique further.
You should never force the gc(garbage collector), if you need to do this you have issues elsewhere you should solve instead as san.chez mentioned. Forcing the GC is a good way to see that you are creating too many objects too fast, if you see your memory usage go way down after forcing the GC you probably did this and should rewrite your code to be more efficient in its use of new.
Judging by the amount of memory you have consumed your either creating a ton of small files or a few extremely large ones, perhaps you can tell us more about that as well.

AS3 code with an interface has suddenly started giving "Call to a possibly undefined method"

GameWorld.as, Line 96 1180: Call to a possibly undefined method initialize.
I am adding a controller to my GameWorld that implements IController:
addController(new BackgroundController(this));
public function addController(controller:IController):void
{
controller.initialize();
controllers.push(controller);
}
public interface IController
{
function initialize():void; //setup the controller
function getType():String; //define the controller by a type string
function update():void; //perform update actions
function destroy():void; //cleanup the controller
}
initialize is a method from IController but is now undefined suddenly
I am getting no syntax errors and cant seem to revert my code to a working state.
What could be causing this?
Here is the BackgroundController:
package controller
{
import Entity;
import flash.display.Bitmap;
import flash.display.Sprite;
public class BackgroundController implements IController
{
private var world:GameWorld;
private var images:Vector.<Bitmap>;
private var bgImage:Sprite;
public function BackgroundController(world:GameWorld)
{
this.world = world;
}
public function initialize():void
{
bgImage = new Sprite();
images = new Vector.<Bitmap>();
var ypos:int = 0;
for (var i:int = 0; i < 3; i++ )
{
var tempBmp:Bitmap = new Bitmap(new grasstile(0, 0));
images.push(tempBmp);
bgImage.addChild(tempBmp);
tempBmp.y = ypos;
ypos += 500;
}
GameWorld.lowerLayer.addChild(bgImage);
}
public function update():void
{
//update the background tiles
for (var i:int = 0; i < 3; i++ )
{
images[i].y -= world.gameSpeed;
if (images[i].y < -500 )
{
images[i].y += 1500;
}
}
}
public function getType():String
{
return "Background";
}
public function destroy():void
{
}
}
}
Some global checks
Are you using runtime shared assets, multiple files? Make sure you've build them all.
Make sure there are no other runtime/build errors
In FDT (which editor do you use?) there is a feature called 'reset the MXML compiler and force full build'. That clears the cache and forces to do a complete new build instead of an incremental build.
In Flashdevelop you have to use tools > flash tools > rebuild class path
In the Flash IDE you could clear the ASO files(CS5-) / clear publish cache (CS6).
Restart/kill the editors + related processes to make sure there are no weird cache conflicts and all syntax checking is up to date.
Code checks
// make sure it has implemented the IController
trace("controller is IController: " + (controller is IController) );
and..
// detect what kind of class it really is. Goto that class, check the interface.
trace("controller is : " + getQualifiedClassName(controller) );
Also make sure there are no other IController interfaces, or check all the import statements, so your sure everywhere the right interface is used.
Found myself in the same situation.
Discovered that the problem was due to a name-clashing issue between the package and the defined variable.
So, in your case, just changing
public function addController(controller:IController):void
{
controller.initialize();
controllers.push(controller);
}
to
public function addController(controllerImpl:IController):void
{
controllerImpl.initialize();
controllers.push(controllerImpl);
}
should have solved it.
An interface just defines rules for a class, in this case stating that any class implementing IController must contain definitions for those four methods. Do you actually have an initialize method defined in your controller class?

Read/write as3 zip using fzip

I'm creating a custom file type in AIR, which is a zip file under a different extension. I've been trying a few libs out, and settled on Fzip. This is to house my apps project files.
The tests seem to run OK, apart from occasionally getting 'Unknown record signature' Error.
I'm wondering if I'm missing something, and perhaps someone can shed some light. First time I've attempted something like this.
It seems to occur randomly, I have a basic app which allows you to add new files at runtime. Contents display in a list and upon select you view the text content.
From time to time when adding a new file, saving, then reopening I get this unknown record error. The main functions which could be the cause
private function openComplete( event:Event ):void {
_zipFile.loadBytes( _file.data );
dispatch( new ZipServiceEvent( ZipServiceEvent.CONTENTS_CHANGE ) );
}
public function saveFile( event:Event=null ):void {
if( _file.isDirectory ) {
browseForSave();
return void;
}
if ( _file.extension != _ext )
_file = new File( _file.nativePath + _ext );
var stream:FileStream = new FileStream();
stream.open( _file, FileMode.WRITE );
_zipFile.serialize( stream );
stream.close();
}
public function getFile( name:String ):FZipFile {
return _zipFile.getFileByName( name );
}
public function addFile( name:String, contents:ByteArray ):void {
_zipFile.addFile( name, contents );
}
private function saveFileHandler( event:Event ):void {
var contents:ByteArray = new ByteArray();
contents.writeMultiByte( view.filecontents.text, 'utf-8' );
model.addFile( view.filename.text, contents );
}
I would need to test you class to look for errors… I wont have time to check it but in the meanwhile I´ll post an Util class I created for this purpose. It´s not very extensive, it was just for a small project but it may help you…
package com.models
{
import com.events.AppEvent;
import deng.fzip.FZip;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.utils.ByteArray;
public class ZIPEncoder extends EventDispatcher
{
private var _zip:FZip;
private var _compressedBytes:ByteArray = new ByteArray();
public function ZIPEncoder(target:IEventDispatcher=null)
{
super(target);
}
public function newZip(name:String = ""):void
{
if(_zip) _zip = null;
_zip = new FZip();
_zip.addEventListener(Event.COMPLETE, onZipComplete);
}
public function newEntry(name:String, bytes:ByteArray):void
{
if(_zip == null)
{
throw(new Error("No zipOutput initialized. Call newZip() to initialize a new ZipOutput object before creating entry instances"));
return;
}
_zip.addFile(name, bytes);
}
public function compress():void
{
_zip.serialize(_compressedBytes, false);
dispatchEvent(new AppEvent(AppEvent.ZIP_ENCODED, _compressedBytes));
}
private function onZipComplete(event:Event):void
{
dispatchEvent(new AppEvent(AppEvent.ZIP_ENCODED, _compressedBytes));
}
//public function get zip():ZipOutput { return _zip; }
}
}
hope it helps…

How to improve this AS3 code structure to be more effective?

I have made an AS3 code to be a function. But I think my code is too lengthy. Could you help to improve it? Thank you!
I created test.fla first and added 5 grey block(external pictures from PSD) to stage. My function is to display different pictures when hovering mouse on corresponding grey block.
I converted those 5 grey blocks to Movie Clip and set instance name as sp1, sp2, sp3, sp4 and sp5. Then I created a document class, test.as and set 5 EventListener.
sp1.addEventListener(MouseEvent.MOUSE_OVER,clickmouse1);
sp2.addEventListener(MouseEvent.MOUSE_OVER,clickmouse2);
sp3.addEventListener(MouseEvent.MOUSE_OVER,clickmouse3);
sp4.addEventListener(MouseEvent.MOUSE_OVER,clickmouse4);
sp5.addEventListener(MouseEvent.MOUSE_OVER,clickmouse5);
So my first question is can I have any method to combine those 5 EventListener to be one? Because in my mind, so many EventListener will cost much more resource of PC.
My second question is I set 5 target pictures as 5 class.
In test.as I created code below:
public class EuroCup extends Sprite{
var arr:Array=new Array();
var Res1:Result609=new Result609();
var Res2:Result610=new Result610();
var Res3:Result611=new Result611();
var Res4:Result612=new Result612();
var Res5:Result613=new Result613();
var i:int=0;
public function EuroCup() {
arr[1]=Res1;
arr[2]=Res2;
arr[3]=Res3;
arr[4]=Res4;
arr[5]=Res5;
}
}
I think that is too lengthy. Is there any way to simplify it?
Here is the test.fla and test.as:Download
Whatever, thank u guys!
Restructuring:
public class EuroCup extends Sprite {
private var arr:Array;
public function EuroCup() {
arr = [ new Result609(), new Result610(),
new Result611(), new Result612(), new Result613()
];
}
}
Then use results as arr[0], arr[1] and so on. Also, if you have several sprites to listen clicks on, with similar listeners, you can connect all such sprites to single listeners and use event.target to distinguish them, where event is MouseEvent. Or place them into container and create one listener to that container - again, event.target will tell what sprite is clicked.
And yet two things - every time you see new Array(), replace it with [] - its faster and shorter. And place all code into constructor, not class body - it will be compiled to be executed faster.
You can/should use a Dictionary for associations between the grey rects and the images to display.
package {
public class EuroCup {
private var _children:Array, _current:Sprite, _map:Dictionary;
public function EuroCup() {
super();
initialize();
}
protected function initialize():void {
_children = [];
_map = new Dictonary();
// i don't know the image's symbol name.
// _map[_children[_children.length] = new Result609()] = new SYMBOL_NAME();
for each(var child:Sprite in _children) {
child.addEventListener(MouseEvent.CLICK, click_handler);
}
}
private function click_handler(event:MouseEvent):void
{
if (_current) {
_current.visible = false; // or use fading, etc
}
_current = _map[event.currentTarget] as Sprite;
if (_current) {
_current.visible = true; // or use fading, etc
}
}
}
}
One option for simplifying the code would be to associate the sp and Res instances with each other by identity, using a Dictionary. That allows you to avoid the work of tracking array indices, which is half of the reason you have separate event handler methods. Once the instances are associated by identity, then you can use the currentTarget property of a dispatched event to determine which element in the Dictionary you want to show on the stage.
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Dictionary;
public class test extends Sprite
{
var dict:Dictionary = new Dictionary();
var visibleResult:MovieClip;
public function test()
{
dict[sp1]=new Result609();
dict[sp2]=new Result610();
dict[sp3]=new Result611();
dict[sp4]=new Result612();
dict[sp5]=new Result613();
sp1.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp2.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp3.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp4.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp5.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
}
private function clickmouse(evt:MouseEvent):void
{
if(visibleResult)
{
removeChild(visibleResult);
}
var Res:MovieClip = dict[evt.currentTarget] as MovieClip;
addChild(Res);
Res.x=300;
Res.y=400;
visibleResult=Res;
}
}
}
If you expect to have more than 5 sp instances in the application, then you could use a loop to assign the event listeners. But for less than 10 instances, you probably don't gain much from a loop.
I would go for a more simple version; add only one event listener and use Event.target to determine on which item is clicked, using a switch-statement.
This is helpful if the buttons should do different things.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Test extends Sprite
{
public var sp1:Sprite;
public var sp2:Sprite;
public var sp3:Sprite;
public function Test()
{
this.addEventListener(MouseEvent.MOUSE_OVER, handleClick);
}
private function handleClick(event:MouseEvent):void
{
trace("Clicked on: " + event.target)
switch (event.target)
{
case this.sp1:
{
// do something here
break;
}
case this.sp2:
{
// do something here
break;
}
case this.sp3:
{
// do something here
break;
}
default
{
trace("No handler defined for: " + event.target)
}
}
}
}
}
However, you can also make smart use of it's type. Let's say all you buttons extend a custom class called CustomButton, and they all need to do the same (like call a function), but with a parameter based on it's id.
This is helpful if the buttons should basically do the same thing.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Test extends Sprite
{
public function Test()
{
this.addEventListener(MouseEvent.MOUSE_OVER, handleClick);
}
private function handleClick(event:MouseEvent):void
{
if (event.target is CustomButton)
{
var button:CustomButton = event.target as CustomButton; // you're now sure it's a CustomButton
this.showById(button.id); // let's say CustomButton has a public var 'id'
}
}
private function showById(id:int):void
{
// do something
}
}
}
Hope that helps.
Tip: Always start your class+filename with a capital. Variables start with capitals. This is very common in the actionscript world.

Trying to delete child instance in array. Lack CS training. Totally stuck

I posted yesterday about how to communicate to one class from another that I wanted to delete an instance of it, and I got the dispatcher working today. However, I think I've painted myself into a corner. Even though the dispatcher is working, I A:feel like it's running through too many functions on the way to actually deleting the object, and B: still can't manage to get it to actually delete. I don't have any formal CS training, so it's one of those situations where my mind is going in circles and I can't "see" what I'm doing wrong. I figure if I post my classes here, at the very least people can have a chuckle at my amateur code, and if I'm lucky, some kind soul will point out what I'm doing wrong. So here goes:
Background.as:
//Background class. Singleton? Sets up/maintains the application.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Background extends flash.display.MovieClip {
private var slate:MovieClip;
private var slateBounds:Rectangle = new Rectangle(100,-260,0,280);
private var _toolbox:MovieClip;
private var _elementArray:Array = new Array();
public function Background() {
//attach movieclips to stage
slate = new mc_slate();
slate.x = 100;
slate.y = 20;
addChild(slate);
_toolbox = new Toolbox();
_toolbox.x = 750;
_toolbox.y = 20;
addChild(_toolbox);
//set draggables
//slate.addEventListener(MouseEvent.MOUSE_DOWN, dragSlate);
//slate.addEventListener(MouseEvent.MOUSE_UP, releaseSlate);
slate.addEventListener(MouseEvent.MOUSE_UP, dropNewElement);
}
private function dragSlate(event:MouseEvent) {
slate.startDrag(false, slateBounds);
}
private function releaseSlate(event:MouseEvent) {
slate.stopDrag();
}
private function dropNewElement(event:MouseEvent) {
var _elementType:String = _toolbox.currentTool;
var _x:Number = event.target.x;
var _y:Number = event.target.y;
var _newElement:MovieClip;
var _latestIndex:Number;
//case switch to choose element based on _elementType
//add new element to stage
_newElement = new PageElement(_elementType, event.localX, event.localY);
_latestIndex = _elementArray.push(_newElement);
_newElement.addEventListener("closeWindow", deleteElement);
slate.addChild(_newElement);
}
private function deleteElement(event:Event) {
trace("trying to remove element.");
slate.event.target.removeChild(_elementArray[0]);
}
}
}
Toolbox.as:
//Toolbox class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Toolbox extends flash.display.MovieClip {
private var _toolboxback:MovieClip;
private var _tool01:MovieClip;
private var _tool02:MovieClip;
private var _tool03:MovieClip;
private var _tool04:MovieClip;
private var _tool05:MovieClip;
private var _currentTool:String = 'none';
public function Toolbox() {
_toolboxback = new ToolboxBack();
_toolboxback.x = 0;
_toolboxback.y = 0;
_toolboxback.alpha = .5;
addChild(_toolboxback);
_tool01 = new TextTool();
_tool01.x = 10;
_tool01.y = 10;
addChild(_tool01);
//_tool01.addEventListener(MouseEvent.MOUSE_DOWN, dragTool);
_tool01.addEventListener(MouseEvent.MOUSE_UP, switchTool);
_tool02 = new ImageTool();
_tool02.x = 10;
_tool02.y = 54;
addChild(_tool02);
_tool02.addEventListener(MouseEvent.MOUSE_UP, switchTool);
}
private function dragTool(event:MouseEvent) {
event.target.startDrag(false);
}
private function releaseTool(event:MouseEvent) {
event.target.stopDrag();
}
private function switchTool(event:MouseEvent) {
_currentTool = event.target.toolname;
//trace(_currentTool);
}
public function get currentTool():String{
return _currentTool;
}
}
}
Tool.as (any class with "Tool" at the end of it simply extends this class and adds a name)
//Tool class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Tool extends flash.display.MovieClip {
private var _toolname:String;
public function Tool(toolname) {
_toolname = toolname;
}
public function get toolname():String{
return _toolname;
}
}
}
PageElement.as:
//Page element class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
public class PageElement extends flash.display.MovieClip {
private var _elementname:String;
private var _elementback:MovieClip;
private var _elementmenu:MovieClip;
private var _title:TextField;
private var _formatter:TextFormat = new TextFormat();
public function PageElement(elementname, x, y) {
_elementname = elementname;
_elementback = new ElementBack();
_elementback.x = x;
_elementback.y = y;
_elementback.alpha = .5;
_elementback.addEventListener(MouseEvent.MOUSE_DOWN, dontBubble);
_elementback.addEventListener(MouseEvent.MOUSE_UP, dontBubble);
_elementmenu = new ElementMenu();
_elementmenu.x = x + _elementback.width - 5;
_elementmenu.y = y - 5;
_elementmenu.addEventListener(MouseEvent.MOUSE_OVER, showElementMenu);
_elementmenu.addEventListener(MouseEvent.MOUSE_OUT, retractElementMenu);
_elementmenu.addEventListener(MouseEvent.MOUSE_DOWN, dragElement);
_elementmenu.addEventListener(MouseEvent.MOUSE_UP, releaseElement);
_formatter.font = "Helvetica";
_formatter.size = 10;
_title = new TextField();
_title.text = elementname;
_title.x = x;
_title.y = y;
_title.textColor = 0xffffff;
_title.setTextFormat(_formatter);
addChild(_title);
addChild(_elementback);
addChild(_elementmenu);
}
public function get elementname():String{
return _elementname;
}
public function set elementTitle(newTitle) {
}
public function showElementMenu(event:MouseEvent) {
_elementmenu.expandMenu();
}
public function retractElementMenu(event:MouseEvent) {
_elementmenu.retractMenu();
}
public function hideElementMenu() {
_elementmenu.alpha = 0;
}
private function dragElement(event:MouseEvent) {
event.target.parent.parent.startDrag(false);
event.stopPropagation();
}
private function releaseElement(event:MouseEvent) {
event.target.parent.parent.stopDrag();
event.stopPropagation();
}
private function dontBubble(event:MouseEvent) {
event.stopPropagation();
}
}
}
DeleteBack.as:
//Element menu back class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class DeleteBack extends flash.display.MovieClip {
public function DeleteBack() {
}
public function closeElement(event:MouseEvent) {
dispatchEvent(new Event("closeWindow", true));
trace("event dispatched.");
}
}
}
ElementMenu.as:
//Element menu class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import caurina.transitions.Tweener;
public class ElementMenu extends flash.display.MovieClip {
private var _elementmenuback:MovieClip;
private var _deletebutton:MovieClip;
public function ElementMenu() {
_elementmenuback = new ElementMenuBack();
_elementmenuback.x = 0;
_elementmenuback.y = 0;
_elementmenuback.width = 100;
_elementmenuback.height = 5;
_elementmenuback.alpha = .5;
addChild(_elementmenuback);
_deletebutton = new DeleteBack();
_deletebutton.x = -5;
_deletebutton.y = 10;
_deletebutton.width = 10;
_deletebutton.height = 10;
_deletebutton.alpha = .2;
_deletebutton.visible = false;
addChild(_deletebutton);
_deletebutton.addEventListener(MouseEvent.MOUSE_DOWN, closeElement);
}
public function expandMenu() {
Tweener.addTween(_elementmenuback, {height:30, time:.2, transition:"easeOutBack"});
_deletebutton.visible = true;
}
public function retractMenu() {
Tweener.addTween(_elementmenuback, {height:5, time:.1, transition:"easeInBack"});
_deletebutton.visible = false;
}
public function closeElement(event:MouseEvent) {
//check that the user really wants to close the element before sending the destroy signal
//perform any closing animations
//this.parent.destroy();
_deletebutton.closeElement(event);
}
}
}
That's it for the meaningful classes. Anything else is either an empty class that's only in there to help make the library object accessible to ActionScript, or a trivial extension of something else.
The code puts a new element onto the stage, gives it a cool little dropdown menu that makes it draggable and has a delete button on it, and should link that button to a function that closes the element.
I've got everything but the closing.
General code criticism also very welcome. Like I said, I have no training, I've been figuring this stuff out for myself, and feedback of any kind from people who know what they're doing is valuable.
Thanks!
SS
PS. In response to Daniel's comment, here are the steps the code takes:
The Background class puts everything on the stage and creates the toolbox.
The toolbox creates the tools, which are like Photoshop's tools. You click on them to select an element you want to add to the stage, then you click inside the "slate" to drop a new instance of that object on top of it. The background creates the instance and saves it in an array of all instances created at runtime.
The new element makes its own dropdown menu, which is the draggable portion of the element and holds the delete button. This menu places an eventListener on the delete button.
When the delete button is clicked, the eventListener placed on it by its parent class calls an event dispatcher inside the delete button class itself.
This dispatched event is caught by the background class (I figured the best class to remove the element is the same class that made it, right?) and triggers the actual code to remove the element.
This code, "deleteElement," is where I'm stuck. I have all the instances in an array, but the event has gone through so many intermediary classes, the MouseEvent, and thus, I suspect, the MouseEvent target, has fallen by the wayside. So the only way to know which element to delete is to find its array index. I have no idea how this would work. Any ideas?
let's do this a bit at a time...
slate.event.target.removeChild(_elementArray[0]); in background.as
why are you using slate.event?
you are passing an event object to the function, but looks like you're using a different event's target, which I don't know where it's coming from or why it's not giving you an error.
it should just be event.target, which should give you the PageElement(formerly known as _newElement)
what I don't know also is why you are removing a child from it which is _elementArra[0] - which really is another PageElement and likely itself if you only have one.
so it looks to me that there are a bunch of things that should have thrown errors. What are you using to compile your code? What about debugger? are you using any?
If you look at your previous question, I added some code there about how to get the parent. So I adjusted it a bit
function deleteElement($e:MouseEvent):void{
var parentMC:MovieClip = $e.target.parent;
parentMC.removechild($e.target);
}
however the problem is that you're not passing a MouseEvent but a blank event
dispatchEvent(new Event("closeWindow", true)); in DeleteBack.as
so this will not pass anything under target, and you can't get it. (target is read only, so new Event(etc) will always have a null target. So essentially that's a bit of a lost cause.
you could set an onject in your singleton and pass which mc is to be deleted, and then the deleteElement would just grab that object. The other option is to look into the signals class which will let you do some better/more efficient event handling.
finally (sort of, there's more but for now) I'd say look into using CASAlib, in particular, use CasaMovieClip instead of MovieClip for extending, as it will delete your movie clips better. If you have a lot of event listeners and you don't clear them properly, they'll end up staying in memory even after you delete them.
of course looking into other frameworks like RobotLegs is a good idea too, it gets you into better practices.
GL
Edit ...
frameworks/micro-architectures:
http://www.robotlegs.org/
http://swizframework.org/
http://puremvc.org/
and many more
I think the important thing is to not get stuck on a framework (though I mention the word often). And the best framework is the framework that is best for you, and to me that means offering a good communication backbone for the app and staying out of the way.
My setup for writing code is this:
FlashDevelop with the Flex Compiler. FlashDevelop is for PC only so if you're on Mac you might want to consider other options like flex. FlashDevelop and the Flex compiler(the compiler only) are both free so you can't go wrong, and once you start using it you won't want to go back to coding in Flash - guaranteed!!
debugging:
Trace is the simplest form of debugging, and it can be quite difficult to understand the problem.
You can use the flash debugger by pressing Ctrl-Shift-Enter to compile and run. You will need to set the break points ahead though.
FlashDevelop has a debugger that works just like the Flash and Flex debuggers and I use it quite often.
But my favorite debug tool has to be de monster debugger
it takes a bit to more to implement, and you need to add some code, but it found issues for me that I couldn't get to using the default debugger only. Definitely worth a look.