顯示包含「action script」標籤的文章。顯示所有文章
顯示包含「action script」標籤的文章。顯示所有文章

Flex 4 ~ regular expression example

這範例是用簡單的正規表示法驗證身分證XD


var pattern:RegExp = /^([a-z]{1})([0-9]{9})/;
var str:String = humanID.text;
if(!pattern.test(str)){
Alert.show('身分證不符合格式');
return;
}


reference
http://livedocs.adobe.com/flex/3/html/help.html?content=12_Using_Regular_Expressions_11.html

Flex 4 ~ validate email

var emailVal:EmailValidator = new EmailValidator();
var valResult:* = emailVal.validate(email.text);
if(valResult.type == "invalid"){
Alert.show('invalid email');
return;
}


<s:TextInput id="email"  />


reference
http://www.java2s.com/Code/Flex/Data-Model/UseEmailValidatortovalidateemailcelleditor.htm

Flex 4 ~ open new link

var urlRequest:URLRequest = new URLRequest('http://bittyferrari.blogspot.com/');
navigateToURL(urlRequest, '_blank');



reference
http://blog.flexexamples.com/2007/08/29/launching-new-browser-windows-from-flex/

Flex 4 ~ set preloader center

問題是說設定preloader置中卻得到null的stageWidth

因為場景還未建立完成~

這邊有solution

先hide preloader

public override function set preloader(p:Sprite):void
{
_preloader = p;
p.addEventListener( ProgressEvent.PROGRESS , onSWFDownloadProgress );
p.addEventListener( FlexEvent.INIT_COMPLETE , onFlexInitComplete);
p.visible = false;
}


然後讓她每跑個進度讓他置中

private function onSWFDownloadProgress( event:ProgressEvent ):void
{
centerPreloader();
//updateProgressBar(event.bytesLoaded/event.bytesTotal);
}

在有抓到stageWidth數值的時候置中並設定visible

private function centerPreloader():void
{
if ((stage.stageWidth > 0) && (_preloader)) {
x = (stage.stageWidth / 2) - (clip.width / 2);
y = (stage.stageHeight / 2) - (clip.height / 2);
_preloader.visible = true;
}
}


source
http://tjoadesign.nl/blog/?p=83

Flex 4 ~ set preloader swf

這是張某弄的= =+


default application MXML

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()"
preloader="comp.GgyyPreloader" backgroundColor="#f7babd" >


file
src/comp/GgyyPreloader.as


content
--------------------------------------------------------------------------------------------------

package comp
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.ByteArray;

import mx.controls.Label;
import mx.controls.SWFLoader;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.preloaders.DownloadProgressBar;

public class GgyyPreloader extends DownloadProgressBar
{

[Embed(source="swf/loading3.swf", mimeType="application/octet-stream")]
public var WelcomeScreenGraphic:Class;

private const bgColor:uint = 0xf7babd;

private var _preloader:Sprite;

public function GgyyPreloader():void
{
super();

this.backgroundColor = 0xf7babd;
this.alpha = 1;

var loader:Loader= new Loader();
loader.loadBytes( new WelcomeScreenGraphic() as ByteArray );
this.addChild(loader);

}

public override function set preloader(preloader:Sprite):void
{
preloader.visible = false;
_preloader = preloader;

preloader.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress);
preloader.addEventListener(Event.COMPLETE, onSWFDownloadComplete);
preloader.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress);
preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
}

private function centerPreloader():void
{
if ((stage.stageWidth > 0) && (_preloader)) {
x = (stage.stageWidth / 2) - (1024 / 2);
y = (stage.stageHeight / 2) - (700 / 2);
_preloader.visible = true;
}
}

private function onSWFDownloadProgress(event:ProgressEvent):void
{
centerPreloader();

}

private function onSWFDownloadComplete(event:Event):void
{

}

private function onFlexInitProgress(event:FlexEvent):void
{

}

private function onFlexInitComplete(event:FlexEvent):void
{

dispatchEvent( new Event(Event.COMPLETE));
}
}
}

Flex 4 ~ get top level Application

在Flex 3的時候如果要取得原本Application的物件

Application.application;

設定id叫OfferEndLabel的元件隱藏

Application.application.OfferEndLabel.visible = false;



可是在Flex 4 的寫法是這樣~

FlexGlobals.topLevelApplication;


import mx.core.FlexGlobals;
//call changeForm
FlexGlobals.topLevelApplication.changeForm('register');


reference
http://www.unitedmindset.com/jonbcampos/2009/06/06/migrating-from-flex-3-to-flash-builder-4-mxcoreapplication/

Flex 4 ~ animate scale effect

MXML
--------------------------------------------------------------------------------------------------------------------------

<fx:Declarations>
<s:Scale id="scaleEffect" target="{PreviewImage}" duration="200" />
</fx:Declarations>

<mx:Image id="PreviewImage" x="0" y="0"  />



Action Script
--------------------------------------------------------------------------------------------------------------------------
private function zoomInImage():void{

scaleEffect.scaleXTo = PreviewImage.scaleX + 0.15;
scaleEffect.scaleYTo = PreviewImage.scaleY + 0.15;
scaleEffect.end();
scaleEffect.play();
}



reference
http://help.adobe.com/en_US/flex/using/WS6461B3EC-1AED-486d-A4B8-8EBD1993CE22.html

Flex 4 ~ save Group, Canvas content as image and download

MXML
--------------------------------------------------------------------------------------------------
<s:Group id="imageCav" x="0" y="30" width="300" height="200" clipAndEnableScrolling="true" >
<mx:Image id="PreviewImage" x="0" y="0" />
</s:Group>

<s:Button label="save Image" click="saveImage()" x="50" y="300">




Action Script
--------------------------------------------------------------------------------------------------

private function saveImage():void{
var bd:BitmapData = new BitmapData(imageCav.width,imageCav.height);
bd.draw(imageCav);
var ba:ByteArray = (new PNGEncoder()).encode(bd);
(new FileReference()).save(ba, "cool.png");
}




reference
http://www.jamesward.com/2009/04/16/flex-paint-2/

http://www.jamesward.com/demos/FlexPaint2/srcview/index.html

Flex 4 ~ upload image file and set image width, height

MXML
--------------------------------------------------------------------------------------------------------
<mx:Button label="Browse" click="Image_Browse();" />
<mx:Image id="PreviewImage" x="0" y="0" />
--------------------------------------------------------------------------------------------------------




Action Script
--------------------------------------------------------------------------------------------------------
private var ImageTypes:FileFilter = new FileFilter("Image (*.png, *.jpg, *.gif)", "*.png; *.jpg; *.gif");
private var UploadFilter:Array = new Array(ImageTypes);
private var FileRef:FileReference = new FileReference();

private function Image_Browse() : void {
FileRef.addEventListener(Event.SELECT, Image_Select);
FileRef.addEventListener(Event.COMPLETE, PreviewImageHandler);
FileRef.browse(UploadFilter);
}

private function Image_Select(e:Event) : void {
FileRef.load();
}

private function PreviewImageHandler(e:Event) : void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.loadBytes(FileRef.data);
PreviewImage.source = FileRef.data;
}

//set image width, height
public function loader_complete (event:Event) : void {
var sourceBMP:Bitmap = event.currentTarget.loader.content as Bitmap;
PreviewImage.width = sourceBMP.width;
PreviewImage.height = sourceBMP.height;
}


--------------------------------------------------------------------------------------------------------

reference
http://stackoverflow.com/questions/3439532/flex-4-filereference-selected-image-file-dimmensions-width-and-height

Flex ~ sort DataGrid date column

這問題是當要排序日期的時候發生用字串排序的問題

所以要用sortCompareFunction去把字串轉Date物件再來做比較~


MXML
<mx:DataGridColumn width="120" headerText="Submit Time" wordWrap="true" sortable="true" textAlign="left" dataField="SubmitTime" sortCompareFunction="sortDate">
</mx:DataGridColumn>


Action Script
(SubmitTime is one of dataProvider's field)

private function sortDate(obj1:Object, obj2:Object):int {
var dateA:Date = new Date(Date.parse(obj1.SubmitTime));
var dateB:Date = new Date(Date.parse(obj2.SubmitTime));
return ObjectUtil.dateCompare(dateA, dateB);
}



reference
http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/

Flex ~ submit form by return key ~enter key

Action Script
filterForm.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void{
if(e.charCode == 13){
//do something
}
});


MXML
<mx:Form id="filterForm">
</mx:Form>

reference
http://cookbooks.adobe.com/post_Submit_form_with_enter_key-12752.html

Flex ~DateField get date value format YYYY-MM-DD

MXML
<mx:DateField id="toDate" formatString="YYYY-MM-DD" />


Action Script
trace(toDate.text);


output
2010-12-28

Flex ~ FileReference can't download, FileReference download problem

如果是把FileReference宣告在function裡就會無法下載

要宣告在全域變數..........

---------------------------------------------------------------------------------
private var file:FileReference = new FileReference();
private function DoDownload(e:MouseEvent):void{

var url:String = _downloadAddress + "?R=";

var ran:Number = Math.random();
var req:URLRequest = new URLRequest(url + ran);

file.addEventListener(ProgressEvent.PROGRESS, StartDownload);
file.addEventListener(Event.COMPLETE, FileCompleteEvent);
file.download(req);

}
---------------------------------------------------------------------------------

如果寫在裡面 ~~~~~就會跟我一樣~~~~~ 一下載就整個卡住不會動~~~~

private function DoDownload(e:MouseEvent):void{

private var file:FileReference = new FileReference();

blablablablablablablablablablablablablablablablablabla
}


這問題鳥爆了..........害我弄超久............


reference:
http://www.learnosity.com/techblog/index.cfm/2007/7/16/Filereference-download-problem--solved

Flex ~ send http request ~ HTTPService

傳url後面要帶亂數GET參數

不然他不會重新request~~~~會取cache~XD

public function sentBehavior(i:int):void
{
var myHttp:HTTPService;
myHttp = new HTTPService();
myHttp.url = 'http://localhost:1234/test3.php?o=' + Math.random().toString();
myHttp.addEventListener(ResultEvent.RESULT, qqq);
myHttp.send();
}

public function qqq(event:ResultEvent):void
{
trace(event.result);
}

http://blog.riaproject.com/flex/82.html

Flex ~ get List control allowMultipleSelection="true" values

Action Script
var xx:XML = <root><node label="GGYY" TypeID="3"></node><node label="YYGG" TypeID="4"></node></root>
copyToSiteList.dataProvider = xx.node;

for(var i:int=0; i < copyToSiteList.selectedItems.length; i++)
{
trace(copyToSiteList.selectedItems[i].@TypeID);
}


MXML
<mx:List allowMultipleSelection="true" id="copyToSiteList" labelField="@label" >
</mx:List>

Flex ~ ComboBox length, set selectedIndex example

this is a example if combobox item's SkuID == p.mainSkuID

then set index


Action Script
for(var i:int =0; i < MainSkuID.dataProvider.length; i++){
if(MainSkuID.dataProvider[i].@SkuID == p.mainSkuID)
{
MainSkuID.selectedIndex = i;
}
}

MXML
<mx:ComboBox labelField="Label" id="MainSkuID" >
</mx:ComboBox>


http://flex.sys-con.com/node/312098

Flex ~ assing item for List control by XML

Action Script
var xx:XML = <root><node label="GGYY"></node><node label="GGYY22222"></node><node label="Parts Sto123re"></node></root>

copyToSiteList.dataProvider = xx.node;


MXMXL
<mx:List allowMultipleSelection="true" id="copyToSiteList" labelField="@label" >
</mx:List>

http://livedocs.adobe.com/flex/3/html/help.html?content=dpcontrols_2.html

Flex ~ xml

XML
<root Label="aaaaaaaaa" Icon="Root">
<Option >
<Unit UnitID="2651" Label="xxxx">
<Sku SkuID="3490" Label="aaaaaaa" />
</Unit>
</Option>
</root>

Action Script
MainSkuID.labelField = '@Label';
MainSkuID.dataProvider = xml.Option.Unit.(@UnitID==2651).Sku;

MXML
<mx:ComboBox x="115" y="61" labelField="Label" textAlign="left" id="MainSkuID" >
</mx:ComboBox>

http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_03.html

FLEX ~ change Label font color

//uint
labelQQ.setStyle('color', 0x00FFFF);

//string
labelQQ.setStyle('color', '#FF00FF');


<mx:Label x="350" y="330" id="labelQQ" alpha="1" color="#FFFFFF" fontWeight="bold" />

Flex ~ set cursor to HandCursor, pointer cursor

<mx:Canvas height="250" width="75" id="cavLeft" useHandCursor="true" buttonMode="true" >

http://www.riafox.com/2008/09/usehandcursor-in-flex-component/