prevent event continuing propagation:
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
2013年8月26日 星期一
2013年6月10日 星期一
[Flex, Flash] use XML
var myXML:XML = new XML("<root><person name='Mike'/><person name='Bike'/></root>");
trace(myXML.person[0].@name);
var yourXML:XML = <root><person name='Mike'/><person name='Bike'/></root>;
trace(myXML.person[1].@name);
value in XML is String by default
trace(myXML.person[0].@name);
var yourXML:XML = <root><person name='Mike'/><person name='Bike'/></root>;
trace(myXML.person[1].@name);
value in XML is String by default
2013年6月9日 星期日
[Flex] Coding Note
Using Sprite in flex:
class needs to be inherited from "SpriteVisualElement", not "Sprite"
for each and for key:
for each : get value
var obj:Object = {x:10, y:20};
for each (var num in obj)
{
trace(num); //get 10, 20
}
for (var key:String in obj)
{
trace(obj[key]);//get 10, 20. but key is x, y
}
class needs to be inherited from "SpriteVisualElement", not "Sprite"
for each and for key:
for each : get value
var obj:Object = {x:10, y:20};
for each (var num in obj)
{
trace(num); //get 10, 20
}
for (var key:String in obj)
{
trace(obj[key]);//get 10, 20. but key is x, y
}
[Flex] use SWF in web page
ExternalInterface
The ExternalInterface class is an application programming interface that enables straightforward communication between ActionScript and the SWF container– for example, an HTML page with JavaScript or a desktop application that uses Flash Player to display a SWF file.
The ExternalInterface class is an application programming interface that enables straightforward communication between ActionScript and the SWF container– for example, an HTML page with JavaScript or a desktop application that uses Flash Player to display a SWF file.
Using the ExternalInterface class, you can call an ActionScript function in the Flash runtime, using JavaScript in the HTML page. The ActionScript function can return a value, and JavaScript receives it immediately as the return value of the call.
This functionality replaces the
fscommand() method.[Flex, Flash] Events
currentTarget: object that is processing this event (object that register this listener)
target: object that actually fires the event
target: object that actually fires the event
[Flash] Keys
http://www.webwasp.co.uk/tutorials/038/index.php
Ctrl+K: align window
Ctrl + F8: create Symbol
Ctrl + F11: change symbol type
Movie Explorer : Alt + F3
Ctrl + Shift +Enter : debug run
Ctrl+K: align window
Ctrl + F8: create Symbol
Ctrl + F11: change symbol type
Movie Explorer : Alt + F3
Ctrl + Shift +Enter : debug run
[Flex] PropertyChangeEvent
can be used to detect if viewport scroll position changed
http://stackoverflow.com/questions/4390725/flex-4-scroller/4425091#4425091
http://stackoverflow.com/questions/4390725/flex-4-scroller/4425091#4425091
2013年6月7日 星期五
[Flex] Menu
<s:HGroup>
<mx:MenuBar id="mBar" labelField="@label" itemClick="mbiTest(event)" >
<mx:dataProvider>
<s:XMLListCollection>
<fx:XMLList xmlns="">
<menu label="File">
<item label="load image"/>
<item label="browse"/>
<item label="save"/>
</menu>
</fx:XMLList>
</s:XMLListCollection>
</mx:dataProvider>
</mx:MenuBar>
<mx:MenuBar id="mBar2" labelField="@label" itemClick="mbiTest2(event)" >
<!--<mx:MenuBar id="mBar" labelField="@label" >-->
<mx:dataProvider>
<s:XMLListCollection>
<fx:XMLList xmlns="">
<menu label="BBB">
<item label="bbb0"/>
</menu>
</fx:XMLList>
</s:XMLListCollection>
</mx:dataProvider>
</mx:MenuBar>
</s:HGroup>
private function mbiTest(event:MenuEvent):void
{
switch(event.index)
{
case 0:
deal with load file
break;
case 1:
deal with browse dir
break;
case 2:
deal with save
break;
}
}
<mx:MenuBar id="mBar" labelField="@label" itemClick="mbiTest(event)" >
<mx:dataProvider>
<s:XMLListCollection>
<fx:XMLList xmlns="">
<menu label="File">
<item label="load image"/>
<item label="browse"/>
<item label="save"/>
</menu>
</fx:XMLList>
</s:XMLListCollection>
</mx:dataProvider>
</mx:MenuBar>
<mx:MenuBar id="mBar2" labelField="@label" itemClick="mbiTest2(event)" >
<!--<mx:MenuBar id="mBar" labelField="@label" >-->
<mx:dataProvider>
<s:XMLListCollection>
<fx:XMLList xmlns="">
<menu label="BBB">
<item label="bbb0"/>
</menu>
</fx:XMLList>
</s:XMLListCollection>
</mx:dataProvider>
</mx:MenuBar>
</s:HGroup>
private function mbiTest(event:MenuEvent):void
{
switch(event.index)
{
case 0:
deal with load file
break;
case 1:
deal with browse dir
break;
case 2:
deal with save
break;
}
}
[Flex & Flash] Drag
[Flex]
Group drag:
http://nightlycoding.com/index.php/2012/04/re-arrange-objects-in-a-vgroup-by-dragdrop-flex-spark/
Custom Drag:
http://stackoverflow.com/questions/1469609/flash-as3-custome-dragging-using-mouse-move-event
http://stackoverflow.com/questions/1353361/problems-replicating-drag-and-drop-with-mouse-events/1353489#1353489
http://stackoverflow.com/questions/11994352/flash-as3-event-listener-for-startdrag-stopdrag
Group drag:
http://nightlycoding.com/index.php/2012/04/re-arrange-objects-in-a-vgroup-by-dragdrop-flex-spark/
Custom Drag:
http://stackoverflow.com/questions/1469609/flash-as3-custome-dragging-using-mouse-move-event
http://stackoverflow.com/questions/1353361/problems-replicating-drag-and-drop-with-mouse-events/1353489#1353489
http://stackoverflow.com/questions/11994352/flash-as3-event-listener-for-startdrag-stopdrag
2013年6月5日 星期三
[Flex] Use Document Class
In flash CS, right side "class" make class name
In flex, New->Flash Professional Project->select the .fla file to use
if error with "addFrame" => document class should extend from MovieClip
every component shown on stage should be declared as public in the class, variable name should be the same as instance name on stage
use frame: http://blog.ring.idv.tw/comment.ser?i=336
In flex, New->Flash Professional Project->select the .fla file to use
if error with "addFrame" => document class should extend from MovieClip
every component shown on stage should be declared as public in the class, variable name should be the same as instance name on stage
use frame: http://blog.ring.idv.tw/comment.ser?i=336
[Flex] Use Custom Event in MXML
http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html
Content in Green Text Must Be the Same
package testclass
{
import flash.events.Event;
public class FileServiceEvent extends Event
{
public static const FILE_SERVICE_LOAD_COMPLETE:String = "fileServiceLoadComplete";
public function FileServiceEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
Event we want to trigger:
package testclass
{
import flash.events.Event;
public class FileServiceLoadCompleteEvent extends FileServiceEvent
{
public function FileServiceLoadCompleteEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
class that may dispatch the event we want to trigger:
package testclass
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.filesystem.File;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import mx.controls.Alert;
import testclass.FileServiceLoadCompleteEvent;
//the following 2 lines are important
[Event(name="fileServiceLoadComplete", type="testclass.FileServiceLoadCompleteEvent")]
[DefaultTriggerEvent("fileServiceLoadComplete")]
public class FileService extends EventDispatcher
{
public static const LOAD_ANY:uint = 0;
public static const LOAD_IMAGE:uint = 1;
public var aaa:uint;
public var fileServiceLoadComplete:FileServiceLoadCompleteEvent;
private var fileLoad:File;
public function FileService()
{
setupFileLoad();
}
private function setupFileLoad():void
{
fileLoad = null;
fileLoad = new File();
fileLoad.addEventListener(Event.SELECT, selectFile);
fileLoad.addEventListener(Event.COMPLETE, loadComplete);
}
public function loadFile(load_type:uint = LOAD_ANY):void
{
switch(load_type)
{
case LOAD_ANY:
fileLoad.browseForOpen("Select a file");
break;
case LOAD_IMAGE:
var filter:Array = [];
filter.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
fileLoad.browseForOpen("Select a image file", filter);
break;
default:
Alert.show("argument 'load_type' wrong");
break;
}
}
private function selectFile(event:Event):void
{
fileLoad.load();
}
private function loadComplete(event:Event):void
{
//dispatchEvent(new Event(Event.COMPLETE));
//dispatch our custom event
dispatchEvent(new FileServiceLoadCompleteEvent(FileServiceEvent.FILE_SERVICE_LOAD_COMPLETE));
}
public function getLoadedData():ByteArray
{
if((fileLoad.data == null) || (fileLoad.data.length == 1))
{
Alert.show("getLoadedData Error, data is null");
return null;
}
return fileLoad.data;
}
}
}
in MXML:
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<testclass:FileService id="fileService" fileServiceLoadComplete="fileServiceLoadComplete(event)"/>
<!--<testclass:FileService id="fileService" aaa="5"/>-->
</fx:Declarations>
//the red part is needless if we declare the red part in the event dispatcher class
<!--<fx:Metadata>
[Event(name="fileServiceLoadComplete", type="testclass.FileServiceLoadCompleteEvent")]
</fx:Metadata>-->
<fx:Script>
<![CDATA[
import testclass.FileServiceLoadCompleteEvent;
import mx.controls.Alert;
private function fileServiceLoadComplete(event:FileServiceLoadCompleteEvent):void
{
//Alert.show("load complete");
img.source = fileService.getLoadedData();
}
]]>
</fx:Script>
Content in Green Text Must Be the Same
package testclass
{
import flash.events.Event;
public class FileServiceEvent extends Event
{
public static const FILE_SERVICE_LOAD_COMPLETE:String = "fileServiceLoadComplete";
public function FileServiceEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
Event we want to trigger:
package testclass
{
import flash.events.Event;
public class FileServiceLoadCompleteEvent extends FileServiceEvent
{
public function FileServiceLoadCompleteEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
class that may dispatch the event we want to trigger:
package testclass
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.filesystem.File;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import mx.controls.Alert;
import testclass.FileServiceLoadCompleteEvent;
//the following 2 lines are important
[Event(name="fileServiceLoadComplete", type="testclass.FileServiceLoadCompleteEvent")]
[DefaultTriggerEvent("fileServiceLoadComplete")]
public class FileService extends EventDispatcher
{
public static const LOAD_ANY:uint = 0;
public static const LOAD_IMAGE:uint = 1;
public var aaa:uint;
public var fileServiceLoadComplete:FileServiceLoadCompleteEvent;
private var fileLoad:File;
public function FileService()
{
setupFileLoad();
}
private function setupFileLoad():void
{
fileLoad = null;
fileLoad = new File();
fileLoad.addEventListener(Event.SELECT, selectFile);
fileLoad.addEventListener(Event.COMPLETE, loadComplete);
}
public function loadFile(load_type:uint = LOAD_ANY):void
{
switch(load_type)
{
case LOAD_ANY:
fileLoad.browseForOpen("Select a file");
break;
case LOAD_IMAGE:
var filter:Array = [];
filter.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
fileLoad.browseForOpen("Select a image file", filter);
break;
default:
Alert.show("argument 'load_type' wrong");
break;
}
}
private function selectFile(event:Event):void
{
fileLoad.load();
}
private function loadComplete(event:Event):void
{
//dispatchEvent(new Event(Event.COMPLETE));
//dispatch our custom event
dispatchEvent(new FileServiceLoadCompleteEvent(FileServiceEvent.FILE_SERVICE_LOAD_COMPLETE));
}
public function getLoadedData():ByteArray
{
if((fileLoad.data == null) || (fileLoad.data.length == 1))
{
Alert.show("getLoadedData Error, data is null");
return null;
}
return fileLoad.data;
}
}
}
in MXML:
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<testclass:FileService id="fileService" fileServiceLoadComplete="fileServiceLoadComplete(event)"/>
<!--<testclass:FileService id="fileService" aaa="5"/>-->
</fx:Declarations>
//the red part is needless if we declare the red part in the event dispatcher class
<!--<fx:Metadata>
[Event(name="fileServiceLoadComplete", type="testclass.FileServiceLoadCompleteEvent")]
</fx:Metadata>-->
<fx:Script>
<![CDATA[
import testclass.FileServiceLoadCompleteEvent;
import mx.controls.Alert;
private function fileServiceLoadComplete(event:FileServiceLoadCompleteEvent):void
{
//Alert.show("load complete");
img.source = fileService.getLoadedData();
}
]]>
</fx:Script>
2013年6月4日 星期二
[Flex] Bitmap, BitmapData, ByteArray
http://blog.yoz.sk/2009/10/bitmap-bitmapdata-bytearray/
DisplayObject to BitmapData
DisplayObject to BitmapData
import flash.display.BitmapData;var bitmapData:BitmapData = new BitmapData(button.width, button.height, false, 0xFFFFFF);bitmapData.draw(button);
BitmapData to Bitmap:import flash.display.Bitmap;
var bitmap:Bitmap = new Bitmap(bitmapData);
// result: bitmap
BitmapData to ByteArray:
import mx.graphics.codec.JPEGEncoder;
import flash.utils.ByteArray;
var encoder:JPEGEncoder = new JPEGEncoder(90);
var byteArray:ByteArray = encoder.encode(bitmapData));
2013年6月3日 星期一
[Flash] Misc
Document Class: where the program starts
publish as html:
File->publish settings -> OTHER FORMATS (left side) -> check HTML Wrapper
pass param from html to swf:
in html:
under <Object> tage
<param name=FlashVars value="param_key=param_value"/>
or
<object type="application/x-shockwave-flash" data="yout.swf?param_key=param_value"
publish as html:
File->publish settings -> OTHER FORMATS (left side) -> check HTML Wrapper
pass param from html to swf:
in html:
under <Object> tage
<param name=FlashVars value="param_key=param_value"/>
or
<object type="application/x-shockwave-flash" data="yout.swf?param_key=param_value"
2013年6月2日 星期日
2013年5月31日 星期五
[Flash] Component Related
Convert symbol to class (and use its members)
1. right click to convert primitives to a symbol
2. check "Export for ActionScript"
3. give instance name for the symbol (to be able to be accessed from parent symbol)
Default Symbol Class : MovieClip
enable/disable mouse control on object : .mouseEnabled, .mouseChildren
Make sure no components on the component that has mouseEventListener, otherwise mouseEvent won't work
1. right click to convert primitives to a symbol
2. check "Export for ActionScript"
3. give instance name for the symbol (to be able to be accessed from parent symbol)
Default Symbol Class : MovieClip
enable/disable mouse control on object : .mouseEnabled, .mouseChildren
Make sure no components on the component that has mouseEventListener, otherwise mouseEvent won't work
2013年5月29日 星期三
[Flex] Popup Window
http://blog.flexexamples.com/2009/10/23/displaying-a-popup-spark-titlewindow-container-in-flex-4/
Window Component:
Window Component:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2009/10/23/displaying-a-popup-spark-titlewindow-container-in-flex-4/ --> <s:Application name="Spark_TitleWindow_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo"> <s:controlBarContent> <s:Button id="btn" label="Show TitleWindow" click="btn_click(event);" /> </s:controlBarContent> <fx:Script> <![CDATA[ import comps.MyTitleWindow; import mx.managers.PopUpManager; protected function btn_click(evt:MouseEvent):void { var ttlWndw:MyTitleWindow = PopUpManager.createPopUp(this, MyTitleWindow, true) as MyTitleWindow; PopUpManager.centerPopUp(ttlWndw); } ]]> </fx:Script> </s:Application>
Main
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/23/displaying-a-popup-spark-titlewindow-container-in-flex-4/ -->
<s:TitleWindow name="MyTitleWindow"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
title="Spark TitleWindow title"
width="300" height="200"
close="ttlWndw_close(event);">
<fx:Script>
<![CDATA[
import mx.core.IFlexDisplayObject;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
protected function ttlWndw_close(evt:CloseEvent):void {
PopUpManager.removePopUp(evt.currentTarget as IFlexDisplayObject);
}
]]>
</fx:Script>
</s:TitleWindow>
[Flex] Misc
Some libraries will not be available under different environments (WebApp or Desktop (AIR))
use package in MXML:
<s:Application
xmlns:namespace_name="package"
e.g.: xmlns:file="flash.filesystem.*"
then in <fx:Declaration>: <file:File id="id"/>
Coding conventions:
http://sourceforge.net/adobe/flexsdk/wiki/Coding%20Conventions/
public static const AAA_BBB:String = "value"
set lib (source path): set the parent directory of lib package
show Sprite (include bitmapData )on MXML: use UIComponent:
var ui:UIComponent = new UIComponent;
ui.addChild(sprite);
group.addElement(ui); //group: spark group
use package in MXML:
<s:Application
xmlns:namespace_name="package"
e.g.: xmlns:file="flash.filesystem.*"
then in <fx:Declaration>: <file:File id="id"/>
Coding conventions:
http://sourceforge.net/adobe/flexsdk/wiki/Coding%20Conventions/
public static const AAA_BBB:String = "value"
set lib (source path): set the parent directory of lib package
show Sprite (include bitmapData )on MXML: use UIComponent:
var ui:UIComponent = new UIComponent;
ui.addChild(sprite);
group.addElement(ui); //group: spark group
[Flex] Keys
[Flex]
http://dispatchevent.org/mims/flexbuilder-keyboard-shortcuts/
Editing:
Shift+direction keys: select text
text selected->(ctrl+alt)+up/down: copy&paste selected text
Other:
F3/ Ctrl+Mouse Click: Open Declaration
Ctrl+Q: go to last modified line
Ctrl+L : go to one specific lime
Ctrl+Shift+D: ASDoc style comment
Ctrl+Shift+T: search class
Alt + <-, -> : last cursor
[Flash]
Ctrl+Shift+V: paste at the same position
http://dispatchevent.org/mims/flexbuilder-keyboard-shortcuts/
Editing:
Shift+direction keys: select text
text selected->(ctrl+alt)+up/down: copy&paste selected text
Other:
F3/ Ctrl+Mouse Click: Open Declaration
Ctrl+Q: go to last modified line
Ctrl+L : go to one specific lime
Ctrl+Shift+D: ASDoc style comment
Ctrl+Shift+T: search class
Alt + <-, -> : last cursor
[Flash]
Ctrl+Shift+V: paste at the same position
[Flex] Slice Image
package utils
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.errors.IllegalOperationError;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.registerClassAlias;
import spark.primitives.BitmapImage;
public class ImageSlicer
{
/**
* Creates a new ImageSlicer
*
* @constructor
* @throws IllegalOperationError
*/
public function ImageSlicer() {
throw new IllegalOperationError('ImageSlicer cannot be instantiated.');
}
public static function sliceImageWithFixedSliceSize(image:BitmapData,sliceWidth:uint,sliceHeight:uint):Array
{
var iWidth:Number = image. width;
var iHeight:Number = image.height;
var testMod:Boolean;
testMod=((iWidth%sliceWidth)>0)?true:false;
var numCols:uint=iWidth/sliceWidth+testMod;
testMod=((iHeight%sliceHeight)>0)?true:false;
var numRows:uint=iHeight/sliceHeight+testMod;
var iArray:Array = new Array();
var rectangle:Rectangle=new Rectangle();
var bitmap:Bitmap;
var bData:BitmapData;
for( var rowIdx:int = 0; rowIdx < numRows; rowIdx++ ) {
iArray[rowIdx] = new Array();
for( var colIdx:int = 0; colIdx < numCols; colIdx++ ) {
bData = new BitmapData( sliceWidth, sliceHeight, true, 0x00000000 );
rectangle = new Rectangle( colIdx * sliceWidth, rowIdx * sliceHeight, sliceWidth, sliceHeight );
bData.copyPixels( image, rectangle, new Point( 0, 0 ) );
bitmap = new Bitmap( bData );
iArray[rowIdx][colIdx] = bitmap;
}
}
return iArray;
}
}
}
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.errors.IllegalOperationError;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.registerClassAlias;
import spark.primitives.BitmapImage;
public class ImageSlicer
{
/**
* Creates a new ImageSlicer
*
* @constructor
* @throws IllegalOperationError
*/
public function ImageSlicer() {
throw new IllegalOperationError('ImageSlicer cannot be instantiated.');
}
public static function sliceImageWithFixedSliceSize(image:BitmapData,sliceWidth:uint,sliceHeight:uint):Array
{
var iWidth:Number = image. width;
var iHeight:Number = image.height;
var testMod:Boolean;
testMod=((iWidth%sliceWidth)>0)?true:false;
var numCols:uint=iWidth/sliceWidth+testMod;
testMod=((iHeight%sliceHeight)>0)?true:false;
var numRows:uint=iHeight/sliceHeight+testMod;
var iArray:Array = new Array();
var rectangle:Rectangle=new Rectangle();
var bitmap:Bitmap;
var bData:BitmapData;
for( var rowIdx:int = 0; rowIdx < numRows; rowIdx++ ) {
iArray[rowIdx] = new Array();
for( var colIdx:int = 0; colIdx < numCols; colIdx++ ) {
bData = new BitmapData( sliceWidth, sliceHeight, true, 0x00000000 );
rectangle = new Rectangle( colIdx * sliceWidth, rowIdx * sliceHeight, sliceWidth, sliceHeight );
bData.copyPixels( image, rectangle, new Point( 0, 0 ) );
bitmap = new Bitmap( bData );
iArray[rowIdx][colIdx] = bitmap;
}
}
return iArray;
}
}
}
訂閱:
意見 (Atom)