2009/04/13

一个简单的鼠标手势模拟

Demo

/**
* @author chenqian
* @contact yesterday[at]gmail.com
* @URI www.stopdesign.cn
*/


package
{
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
import flash.text.TextField;

public class MouseGesture extends Sprite
{

public static const GESTURE_UP:String="GESTURE_UP";
public static const GESTURE_RIGHT:String="GESTURE_RIGHT";
public static const GESTURE_DOWN:String="GESTURE_DOWN";
public static const GESTURE_LEFT:String="GESTURE_LEFT";
// the color of the drawing line
public var lineColor:Number;
// the size of the drawing line
public var lineSize:Number;
// where the mouse start drawing
private var _startPoint : Point;
// where the mouse stop drawing
private var _newPoint : Point;
// the drawing direction
private var _direction : String;
// the drawing line
private var _drawLine : Sprite;

public function MouseGesture(...rest){
trace("mouse gesture init")
// default mouse gestrue line color and size
lineColor=0xFF0000;
lineSize= 2;

// parse parameters
if(rest[0] != null){
lineColor = rest[0];

}
if(rest[1] != null){
lineSize=rest[1];
}

//add the shape, used for drawing the gesture
var child:Shape = new Shape();
child.graphics.beginFill(0x000000);
child.graphics.lineStyle(0, 0xFFFFFF);
child.graphics.drawRect(0, 0, 1600, 1600);
child.graphics.endFill();
addChild(child);
child.alpha=0;
_drawLine = new Sprite();
addChild( _drawLine );
addEventListener( MouseEvent.MOUSE_DOWN , mouseDownHandler );
}

// set the gesture color
public function setLineColor(c:Number):void{
lineColor=c;
}

// set the gesture size
public function setLineSize(s:Number):void{
lineSize=s;
}


// fire when user press mouse down
private function mouseDownHandler( e:MouseEvent ):void
{

var _color : Number = lineColor;
var _size : Number = lineSize;
_startPoint = new Point( mouseX , mouseY );
_drawLine.graphics.lineStyle( _size , _color , 1.0 );
_drawLine.graphics.moveTo( _startPoint.x , _startPoint.y );
addEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
addEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}


// fire when user move the mouse
private function mouseMoveHandler( e:MouseEvent ):void
{
_newPoint = new Point( mouseX , mouseY );
var _distance : Number = Point.distance( _startPoint , _newPoint );
_drawLine.graphics.lineTo( _newPoint.x , _newPoint.y );
}


// fire when user's mouse is up
private function mouseUpHander( e:MouseEvent ):void
{
detectDir();
_drawLine.graphics.clear();
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}


/* detecting the mouse move direction */
private function detectDir():void
{
var _vector:Point = _newPoint.subtract( _startPoint );
var _angle : Number = Math.atan2( _vector.y , _vector.x ) * 180 / Math.PI;
// move down
if ( _angle >= 45 && _angle < 125 ){
dispatchEvent(new Event(MouseGesture.GESTURE_DOWN,true));
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}
// move left
else if ( _angle >= 125 || _angle < -125 ){
dispatchEvent(new Event(MouseGesture.GESTURE_LEFT,true))
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );

}
// move right
else if ( _angle >= -45 && _angle < 45 ){
dispatchEvent(new Event(MouseGesture.GESTURE_RIGHT,true))
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );

}
// move up
else if ( _angle >= -125 && _angle < -45 ){
dispatchEvent(new Event(MouseGesture.GESTURE_UP,true))
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );

}
}

}

}

Labels: , ,

2008/01/08

actionscript
刚发现 china-pub 有 Essential ActionScript 3.0 原版书在卖,如果不是很急,也可以等影印版,总之都比直接在 amazon 上买便宜多了。

Labels:

2007/09/18

尝试用as3处理XML


利用饭否的 API 生成的好友头像,因为平时没怎么用,也就没有多少好友,暂时用 Realazy 同学的好友(比较多)做测试。体验到 as3 处理 XML 的方便,基本上就是内建对 xpath 的支持,不过深度管理方面让人比较抓狂,还在琢磨中。发布成 AIR 的格式。

Labels: , , ,

2007/08/26

spectrum


(点击图片打开,有声音)
actionscript 3 对声音的波谱分析加上一些滤镜效果往往会给人一些意想不到的东西。

update:音乐来自 Linkin Park。

Labels: , ,

2007/08/21

文字转换为声音


看到一个有趣的日文 API ,可以把文字转换为声音,刚好有人在试验,我把它改成了可以输入文字的形式。以前在学校的时候,某某同学总是用刚学来的叨不叨不的日语跟我说“男人很辛苦”,我试着输入“男人很辛苦”,“男人”好像是这么说的,辛苦好像用中文说的。这个东西没事可以用来查查某些日语。
代码如下(未经整理的):
package {
import
fl.controls.Button;
import
flash.display.*;
import
flash.events.*;
import
flash.net.*;
import
flash.media.Sound;
import
flash.media.SoundChannel;
import
flash.text.TextField;
import
flash.text.TextFieldType;
import
flash.events.TextEvent;
public class
Text2speech01 extends Sprite {
private
const API_URL:String = "http://api.satoru.net/text2voice/";
private var
urlReq:URLRequest = new URLRequest(API_URL);
private var
urll:URLLoader = new URLLoader();
private var
v:URLVariables = new URLVariables();
public var
textInputTxt:TextField;
function
Text2speech01() {
init();
}

private function
init():void {
var
myButton:Button = new Button();
textInputTxt = new TextField();
textInputTxt.addEventListener(TextEvent.TEXT_INPUT, textInputHandler);
textInputTxt.type = TextFieldType.INPUT;
textInputTxt.background = true;
textInputTxt.border = true;
textInputTxt.height=20;
textInputTxt.width=300;
textInputTxt.x=10;
textInputTxt.y=18;
textInputTxt.text="Long long ago, there is a girl."
v.text = "Long long ago, there is a girl.";
addChild(textInputTxt);
myButton.label = "saying";
//myButton.emphasized = true;
myButton.width = 80;
myButton.move(10, 50);
addChild(myButton);
myButton.addEventListener(MouseEvent.CLICK, buttonClick);
textInputTxt.addEventListener(MouseEvent.MOUSE_OVER,changeStyle1);
textInputTxt.addEventListener(MouseEvent.MOUSE_OUT,changeStyle2);
}

private function
changeStyle1(e:MouseEvent):void{
textInputTxt.borderColor= 0x00FF00;
}

private function
changeStyle2(e:MouseEvent):void{
textInputTxt.borderColor= 0x000000;
}

private function
textInputHandler(e:TextEvent):void {
v.text= textInputTxt.text+e.text;
//trace(">> e.text: " + e.text);
//trace(">> textInputTxt.text: " + v.text);
}
private function
buttonClick(e:MouseEvent) {
urlReq.data = v;
urlReq.method = URLRequestMethod.GET;
trace
("urlReq is :"+urlReq.data);
urll.load(urlReq);
urll.addEventListener(Event.COMPLETE, function(e:Event):void {
trace
("API COMPLETE : " + urll.data);
var
sreq:URLRequest = new URLRequest(urll.data);
var
sf:Sound = new Sound();
sf.addEventListener(Event.COMPLETE, function(e:Event):void {
trace
("Sound load COMPLETE");
});

sf.load(sreq);
var
sc:SoundChannel = sf.play();

sc.addEventListener(Event.SOUND_COMPLETE, function(e:Event):void {
trace
("Sound play COMPLETE");
});

});
}
}
}

Labels: ,

2007/08/20

优化actionscript3

John Grden 发了一篇优化 actionscript3 计算的文章,总的来说如果按编译器的工作方式写代码的话执行起来会快一点(或者说人类越难一眼看懂的执行起来会快些),比如使用位运算

1.trace (int(n) + (n >> 31));trace( Math.floor(n) );快;

2.如果要置换2个数据,可以采用:var a:Number=20;
var b:Number=30;
a^=b; b^=a; a^=b;
trace(a+'\n'+b);
c=a; a=b; b=c;快。

3.var n:Number = value *.5;var n:Number = value /2;快,而var n:Number =value >>1;var n:Number=value*.5;还快。

更多参考原文

Labels: