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

[Flex & Flash] openAync

http://www.flashas.net/html/air/20091005/4593.html

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
}

[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.
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

[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

[Flash] Concept

http://active.tutsplus.com/tutorials/actionscript/as3-101the-display-list/

[Flex] PropertyChangeEvent

can be used to detect if viewport scroll position changed
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;
}
}

[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

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

[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>

2013年6月4日 星期二

[Flex] Bitmap, BitmapData, ByteArray

http://blog.yoz.sk/2009/10/bitmap-bitmapdata-bytearray/

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));
// result: byteArray


Bitmap Decode
http://gotoandlearn.com/play.php?id=120

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"