Adobe Flash

[转]FlashBuilder中使用fl组件

意思就是:用Flash CS创建一个flash,将需要的fl组件拖进舞台,然后导出swc文件

在FlashBuilder中引入该swc,这时就能在代码中直接new这个fl组件了

How to Use Flash CS’s Components in Flex Builder

To generate the .swc file in Flash CS3, we follow these steps:

1) Make a new Flash CS3 ActionScript 3.0 .fla file.
2) Drag the desired component(s) to the Library. In this example, we’ll drag the TextArea component to the Library.
3) Choose File > Export > Export Movie.
4) For File name, enter v3components.swf. (We don’t even want the generated .swf, but there’s no other way to get the .swc to compile.)
5) Select a folder in which to save the .swf file.
6) Click Save.
7) In the Export Flash Player dialog, check Export SWC.
8) Click OK.

The preceding steps generate two files, cs3 components.swf and v3components.swc, both of which are placed in the folder selected in Step 5.

Now let’s use cs3 components.swc in a Flex Builder project. Follow these steps:

1) In Flex Builder, select File > New > ActionScript Project.
2) For Project name, enter “V3Test.as”.
3) Click Next.
4) For Main source folder, enter “src”.
5) For Main application file, enter “V3Test.as”.
6) On the Library path tab, click Add SWC.
7) Browse to the v3components.swc file from the preceding procedure.
8) Click Finish.
9) Update the code in cs Test.as so it looks like this:

package {
  import flash.display.Sprite;
  import fl.controls.TextArea;
  public class V3Test extends Sprite {
    public function V3Test() {
      var t:TextArea = new TextArea();
      t.text = "You're not cookin'";
      addChild(t);
    }
  }

}
10) Run the project.

AS3在本地选择图片并预览(仅限FlashPlayer10+)

(仅限FlashPlayer10+)

<?xml version="1.0" encoding="utf-8"?>
<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="application1_creationCompleteHandler(event)">

	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			private var file:FileReference;
			private var loader:Loader;
			protected function button1_clickHandler(event:MouseEvent):void
			{
				file = new FileReference();
				file.addEventListener(Event.SELECT,file_selectHandler);
				file.browse ([new FileFilter("Images(*.jpg;*.jpeg;*.png;*.gif)","*.jpg;*.jpeg;*.png;*.gif")]);
			}

			protected function file_selectHandler(event:Event):void{
				file.removeEventListener(Event.SELECT,file_selectHandler);
				file.addEventListener(Event.COMPLETE,file_completeHandler);
				file.load();
			}

			protected function file_completeHandler(event:Event):void{
				file.removeEventListener(Event.COMPLETE,file_completeHandler);
				loader.loadBytes(file.data);
			}
			protected function loaderCompleteHandler(event:Event):void{
				var $bmd:BitmapData = new BitmapData(loader.width,loader.height,true,0x00FFFFFF);
				$bmd.draw(loader.content);
				this.preview.source = $bmd;
				this.preview.width = 40;
				this.preview.height = 40;
			}

			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
				this.swf.addChild(loader);
			}

		]]>
	</fx:Script>
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<s:Button click="button1_clickHandler(event)" label="Select Image"/>
	<s:HGroup>
		<s:BitmapImage id="preview"/>
		<mx:SWFLoader id="swf"/>
	</s:HGroup>
</s:Application>

Flex 4.1以及Flash Builder 4.0.1更新

针对Flash Player 10.1的开发工具更新,快更新吧!

Adobe has publicly pushed the Flex 4.1/Flash Builder 4.0.1 update, which includes:

The new Layout Mirroring feature for repurposing Flex UIs for deployment in right-to-left locales
Native support for Flash Player 10.1 and AIR 2 in the Flex SDK
Native support in Flash Builder 4.0.1 for building apps targeting SDK 4.1, AIR 2 or FP 10.1
Many critical bugfixes and enhancement requests for both Flex SDK and Flash Builder

Adobe has also officially debuted the next release of the Flex SDK, code-named Hero. We share with the world our amazing story of a single unified Flex framework that can be used to build applications for the web, desktop and mobile devices. With today’s debut, we released many new documents and feature specifications, including:

Inaugural launch of the new Hero opensource Flex page
Posting of several new Hero desktop and Hero mobile feature specifications
Updated the labs.adobe.com Flex Mobile content to announce our intent to add mobile development capabilities directly into the core Flex framework, including
o Updating the Flex and Mobile Whitepaper
o Updating the Flex and Mobile FAQ

Please help us spread the word regarding these announcements. You can find more details in the following two blog postings:
Flex SDK 4 and Flash Builder 4 Updates Now Available http://blogs.adobe.com/flex/archives/2010/06/flex_sdk_4_and_flash_builder_4.html
Introducing….Hero!

http://blogs.adobe.com/flex/archives/2010/06/introducinghero.html

Adobe Flash Player 10.1 and Adobe AIR 2 正式发布

Rachel Luxemburg wrote:

Hi All!

Adobe Flash Player 10.1 and Adobe AIR 2 are now available for Windows, Mac and Linux operating systems. To download the runtimes, go to http://get.adobe.com/flashplayer/ for Flash Player and
http://get.adobe.com/air/ for AIR 2.

More info is posted on the AIR team blog, http://blogs.adobe.com/air/ and the Flash Player Team blog http://blogs.adobe.com/flashplayer/.

We’re really excited that we’re finally “over the finish line” with these releases, there’s a lot of new features & some real performance improvements.

Please help spread the word, and enjoy!

Rachel

Flash在IE全屏时 使用Deeplinking修改网址会退出全屏的bug解决方案

BUG描述:

IE浏览器下,Flash全屏时 一旦使用deeplinking修改网址参数,就会导致Flash退出全屏

解决方案:

在全屏时不再修改deeplinking, 而当退出全屏时,使用最后的deeplinking

可以侦听stage类的事件FullScreenEvent. FULL_SCREEN

在任何被添加到了舞台的DisplayObject对象内部,都可以这样写:

this.stage.addEventListener(FullScreenEvent.FULL_SCREEN, stage_fullScreenHandler);

//侦听全屏变化的事件

private function stage_fullScreenHandler(event:FullScreenEvent):void{

//event.fullScreen为false的时候,是退出全屏,true是进入全屏

if(!event.fullScreen){

//修改deeplinking

}

FlashFlex发布可访问本地的SWF

无论是Flex还是Flash编译后的SWF都只可方位网络或者只可访问本地数据,不可以同时访问二者,
Flex默认是只可访问网络,要把Flex编译后的SWF设为只可访问本地数据模式,要在Flex项目的编译参数中增加:-use-network=false
在Flash IDE中,设置方式:在菜单工具栏中选择File(文件)->Publish Settings(发布设置),在弹出窗口中选择Flash标签,在Local playback中选择需要的访问模式

Flex中的Base64加解密

Flex sdk3就内置了Base64的加/解密工具类
分别是
mx.utils.Base64Encoder
mx.utils.Base64Decoder

Base64Encoder用法如下:
var $orgin:String = this.textOrigi.text;//获取原始字符串
var $base64:Base64Encoder = new Base64Encoder();
$base64.insertNewLines = false;//该值等于true时,输出的结果会自动换行,默认为true,
$base64.encodeUTFBytes($orgin);//这里注意,如果想加密中文就不要使用$base64.encode();
var $result:String = $base64.toString();//输出结果
Base64Decoder用法如下:
				var $origi:String = this.textEncodeResult.text;//获取原始字符串
				var $base64:Base64Decoder = new Base64Decoder();
				$base64.decode($origi);
				var $result:String = $base64.toByteArray().toString();//输出结果,decode类只能输出ByteArray类型的数据,因此要转换成string
完成代码:
<?xml version="1.0" encoding="utf-8"?>
<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">

	<fx:Script>
		<![CDATA[
			import mx.utils.Base64Decoder;
			import mx.utils.Base64Encoder;
			protected function btnEncode_clickHandler(event:MouseEvent):void
			{
				var $orgin:String = this.textOrigi.text;//获取原始字符串
				var $base64:Base64Encoder = new Base64Encoder();
				$base64.insertNewLines = false;//该值等于true时,输出的结果会自动换行,默认为true,
				$base64.encodeUTFBytes($orgin);//这里注意,如果想加密中文就不要使用$base64.encode();
				var $result:String = $base64.toString();//输出结果
				this.textEncodeResult.text = $result
			}

			protected function btnDecode_clickHandler(event:MouseEvent):void
			{
				var $origi:String = this.textEncodeResult.text;//获取原始字符串
				var $base64:Base64Decoder = new Base64Decoder();
				$base64.decode($origi);
				var $result:String = $base64.toByteArray().toString();//输出结果,decode类只能输出ByteArray类型的数据,因此要转换成string
				this.textDecodeResult.text = $result
			}

		]]>
	</fx:Script>

	<mx:Form width="100%">
		<mx:FormItem label="原始字符串:" width="100%">
			<s:TextArea editable="true" id="textOrigi" width="100%" text="在这里输入原始字符串"/>
		</mx:FormItem>
		<mx:FormItem>
			<s:Button id="btnEncode" label="Encode Base64" enabled="{this.textOrigi.text!=''}" click="btnEncode_clickHandler(event)"/>
		</mx:FormItem>
		<mx:FormItem label="Encode 结果:" width="100%">
			<s:TextArea editable="false" id="textEncodeResult" width="100%"/>
		</mx:FormItem>
		<mx:FormItem>
			<s:Button id="btnDecode" enabled="{this.textEncodeResult.text!=''}" label="Decode Base64" click="btnDecode_clickHandler(event)"/>
		</mx:FormItem>
		<mx:FormItem label="Decode 结果:" width="100%">
			<s:TextArea editable="false" id="textDecodeResult" width="100%" text="点击Decode Base64按钮后,这里的结果应该和原始字符串相同"/>
		</mx:FormItem>
	</mx:Form>
</s:Application>

Air/Flex动态加载module及其依赖的RSL

有关Flex和Air中如何使用RSL网上教程很多,google一下即可,这里不多做叙述了

而这次遇到的问题是这样的:

发布时只发布一个主程序(Web或者Air方式),而不发布所需要的module及其依赖的swc,只在运行时根据需要加载module及其依赖的swc

1.       建立一个实验用的module工程:

先准备一个module工程,普通的Flex Project(web)即可,主程序名无所谓,因为这个工程只是为了编译module对象用的

在Properties->Flex Build Path中引入所要依赖的swc,这个例子中使用了TweenLite.swc

注意AIR project的默认Link TypeMerge into code,改成RSL。如图:

新建一个包modules并新建一个module程序:MyModule1.mxml

到Properties->Flex Modules中修改该module的optimize属性为none,如下图

点击Edit按钮,弹出下图

选择Do not optimize(…)

在MyModule1.xml中使用TweenLite做一个简单的动画:

TweenLite.to(<strong>this</strong>.goed,4,{x:500});

编译工程,

此时MyModule1.mxml被编译生成为MyModule1.swf

TweenLite.swc会被编译成TweenLite.swf

复制到要被加载的目录下,我的是

http://127.0.0.1/testflex/TweenLite.swf

http://127.0.0.1/testflex/modules/MyModule1.swf

2.       建立主工程(Flex Web):

新建一个Flex Project(web),不要使用引入TweenLite.swc

新建一个用来控制加载TweenLite的类:

tasks/TaskLoadRSL.as

在这个类中,使用URLLoader将TweenLite.swc从http://127.0.0.1/testflex/TweenLite.swf上加载下来,主要代码如下:

var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;//必须有这一句,RSL必须和主程序在同一个域中
loader.load(new URLRequest(this.web),context);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,localLoadCompleteHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,localLoadIoErrorHandler);

加载完TweenLite后不需要任何操作,直接开始加载module即可

使用IModuleInfo或ModuleLoader加载module都可以,详见源代码

3.       建立主工程 (AIR):

Air中比Flex要麻烦,因为发布时只发布主程序,所以运行时需要先检测本地是否有swc和

module

如果有就直接加载运行(同Web)

如果没有就从网站上下载到本地,然后再加载

主要代码如下:

从网络地址上加载:

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loadFromWebCompleteHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, loadFromWebErrorHandler);
loader.load(new URLRequest(web))

加载完,保存到本地:

var loader:URLLoader = URLLoader($e.target);
var data:ByteArray = ByteArray(loader.data);
var file:File = new File(File.applicationDirectory.resolvePath(this.local).nativePath);
trace('save file to' + file.nativePath);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(data);
fileStream.close();
this.loadFromLocalFile();

本地加载过程同Flex Web

更多内容参加源代码:DynamicTestLoadModuleAndRSL

httpservice request传参数的几种方式

mxml代码中 httpservice 组件,连接一php计算器

<s:Button click="this.hs.send();"/>
<fx:Declarations>
	<s:HTTPService id="hs" url="http://127.0.0.1/cal.php" method="GET">
		<s:request xmlns="">
			<calculator>plus</calculator>
			<param1>10</param1>
			<param2>23</param2>
		</s:request>
		<s:fault>
			<![CDATA[
				trace("")
			]]>
		</s:fault>
		<s:result>
			<![CDATA[
				trace(event.result.result.equals)
			]]>
		</s:result>
	</s:HTTPService>
</fx:Declarations>

httpService组件,在as块中传入参数

var param:Object = {calculator:"minus",param1:"23",param2:"13"}
httpServ.send(param)

<mx:HTTPService id="httpServ">
	<mx:resultFormat>text</mx:resultFormat>
	<mx:url>http://127.0.0.1/cal.php</mx:url>
	<mx:fault>Alert.show(event.toString(), event.type);</mx:fault>
</mx:HTTPService>

as代码中,使用httpservice类

public var httpservice:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
public var param:Object={calculator:"minus",param1:"23",param2:"13"};
public function send_data():void{
	httpservice.url ="http://127.0.0.1/cal.php";
	httpservice.method = "POST";
	httpservice.addEventListener(ResultEvent.RESULT, resultHandler);
	httpservice.addEventListener(FaultEvent.FAULT, this.HttpErrorHandle);
	httpservice.send(param);
		}

as代码中使用urlloader

public function Temp()
		{
			var url:String = "http://127.0.0.1/cal.php"
			var urlVariables:URLVariables = new URLVariables();
			urlVariables.decode("calculator=plus&param1=10&param2=22");
			var request:URLRequest = new URLRequest(url);
			request.data = urlVariables;
			request.method = URLRequestMethod.POST

			var loader:URLLoader = new URLLoader()
			loader.dataFormat = URLLoaderDataFormat.TEXT
			loader.addEventListener(Event.COMPLETE, onComplete)
			loader.load(request);

		}
		public function onlistener(event:Event):void{
			var xml:XML = new XML(event.target.data)

			trace(xml..equals)
		}

AS3中使用Twitter API

自制app使用Twitter API

1.1   准备工作

A.注册Twitter用户

B. 下载库文件http://dev.twitter.com/pages/libraries

本实例使用ActionScript 库文件 TwitterScript http://code.google.com/p/twitterscript/

C. Twitter官方文档 http://dev.twitter.com/doc TwitterScript并无说明文档,Twitter官方的文档和TwitterScript的代码不一致,但不矛盾。

1.2 需求说明

制作AIR程序(图1),实现如下功能

a.  显示当前用户头像,用户名

b.  显示当前用户following   followers

c.  显示当前用户friends Tweet  public Tweet

d.  提交新Tweet,并体现到friends Tweet中

图1 程序界面

1.3编写代码

Twitter数据调用多数需要身份验证,有的是不需要的。

Twitter的身份验证简单,只需将用户名和密码编码后嵌入到实际调用数据的request的requestHeaders中即可(暂不考虑OAuth),不同于facebook须登录后得到session-key才能调用数据。

TwitterScript 中的方法:

类Twitter.as中function setAuthenticationCredentials(“username”,”password”)

此方法只是将用户的名和密码编码,function twitterRequest()负责将编码嵌入到requestHeaders,该嵌入过程被整合到各个具体调用数据的方法中因此编写程序代码时不用考虑此过程

A.制作登录页面,通过TextInput将用户名密码传给程序。(图2)

图2

提示:两个state ,登录界面和程序界面两个界面大小可以不同:代码如

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
					   xmlns:s="library://ns.adobe.com/flex/spark"
					   xmlns:mx="library://ns.adobe.com/flex/mx" width="800" height="500"
					   currentState="sign_out" width.sign_out="320" height.sign_out="230"
					   width.sign_in="628" height.sign_in="500">

Sign in按钮click=“sign_in( )”

B. 定义sign_in方法,调用数据

var twitter:Twitter = new Twitter()

twitte.setAuthenticationCredentials(usrId.text, passwd.text)

对用户名 密码编码, userId 和 passwd是登录页面两个输入框的ID

twitter.loadInfo(userId.text)

twitter.addEventListener(TwitterEvent.ON_SHOW_INFO, onloadInfo)

调用user信息

twitter.loadFriends(usrId.text)

twitter.addEventListener(TwitterEvent.ON_FRIENDS_RESULT, onloadfriends)

调用user following

twitter.loadFriendsTimeline(userID.text)

twitter.addEventListener(TwitterEvent.ON_FRIENDS_TIMELINE_RESULT, onloadUserTimeLine)

调用friends Tweet

twitter.addEventListener(TwitterEvent.ON_ERROR, onerror)

监听error

C. 处理返回结果。制作程序界面(如图3)

图3

a.显示用户名 头像

loadInfo方法返回的data类型为TwitterUser,包含name ,square url等,处理方法略

b.显示following用户名

loadFriends方法返回TwitterUser的Array

var tmp:Array = event.data as Array

for (var i:int; i<tmp.length; i++){

var user:TwitterUser = tmp[i];

followname.addItem({flwname:user.name})

followname:ArrayCollection 绑定为显示following 的List的数据源,List标签中建itemrenderer 代码如

<fx:Script>
		<![CDATA[

			[Bindable]
			public var displayname:String;
			override public function set data(value:Object):void{

				displayname = new String(value.flwname)

			}
		]]>
	</fx:Script>
	<s:VGroup>
			<s:Label text="{displayname}"/>
	</s:VGroup>

提示:也可显示头像、创建时间、时区等等

c.显示user ‘s friends Tweet   public Tweet

loadFriendsTimeline方法返回Tweet的Array包含text 和发布者信息等

var tmp:Array = event.data as Array;

for(var i:int;i<event.data.length;i++){ tweetArray.addItem(tmp[i])}

tweetArray:ArrayCollection绑定为显示tweet的DataGrid的数据源,DataGrid代码

<mx:DataGrid x="10" y="87" width="607" height="327" dataProvider="{tweetArray}">
			<mx:columns>
				<mx:DataGridColumn headerText="Tweet" dataField="text"/>
				<mx:DataGridColumn headerText="Tweeter" dataField="user.name" width="100"/>
			</mx:columns>
		</mx:DataGrid>

d.  提交新Tweet。添加输入框和提交按钮,按钮事件sendTweet()

public function sendTweet():void{

var Tweet:String = tweet.text

将输入内容赋值给变量Tweet

twitter.setStatus(Tweet);

提交Tweet

twitter.addEventListener(TwitterEvent.ON_SET_STATUS, onsetstatus)

监听是否成功

tweet.text = “”} 将输入框中文本清除

提交成功后onsetstatus方法刷新一下DataGrid的数据 定义刷新方法

function freshTweet()本例将loadFriendsTimeline方法提出另定义为LoadFriendsTimeLine方法,function freshTweet()执行LoadFriendsTimeLine方法。

e.  添加刷新按钮  添加button 定义skinClass=“com.skin.freshButton”按钮图存同一路径 freshButton 代码

<s:states>
		<s:State name="disabled" />
		<s:State name="down" />
		<s:State name="over" />
		<s:State name="up" />
	</s:states>
	<s:BitmapImage source="@Embed('arrow_refresh.png')"/>

按钮 buttonmode=“true”click=“freshTweet()”

f.   添加Alert.Show 。之前监听Error的响应方法 编写代码

var alert:Alert = Alert.show(“something was wrong,click button for try again”)

登录页面 检验是否输入用户名密码

if(userId.text==”"||passwd.text==”"){ Alert.show(“please enter your username and password!”)}

2 Twitter官网工具

API Status    查看各API状态

http://dev.twitter.com/status

API Console     Web Console

http://dev.twitter.com/start