1、鼠标事件:mouseWheel
当用户在物体上使用鼠标滚轮时触发。
========================================================================
2、得到鼠标的位置:两种方法
使用DisplayObject实例的mouseX 和mouseY 属性;
或通过鼠标事件中MouseEvent 实例localX 和localY 属性。
区别:
一、mouseX 和mouseY 属性指示鼠标相对于DisplayObject对象左上角的坐标
例:如果有个矩形在x坐标为20 和y坐标为50 的地方,而鼠标的位置为x坐标25和y坐标60。
那么矩形的mouseX 属性返回5,mouseY属性返回10。
二、localX 和localY 是相对于激活事件的对象位置。当矩形返回mouseX 为10时发出mouseMove 事件,事件的localX 属性也是10
三、localToGlobal( )方法从本地坐标中获得全局坐标
如:
var localPoint:Point = new Point( event.localX, event.localY );
// 得到全局坐标
var globalPoint:Point = event.target.localToGlobal( localPoint );
========================================================================
3、拖动对象阴影
e.target.filters = [new DropShadowFilter()];
e.target.filters = null; //去掉阴影
========================================================================
4、拖动对象DraggableSprite类(在ascb.display 包中)
DraggableSprite最大的不同是实现了自定义鼠标跟踪代码,多个DraggableSprite 实例可同时拖动,而且updateAfterEvent( ) 也解决了渲染延时问题。
但是在DraggableSprite里dropTarget 属性不能用了,代替它的是getObjectsUnderPoint( ) 方法,返回鼠标位置下面的对象。
getObjectsUnderPoint( ) 方法返回一个对象数组,在数组的length-1位置上的对象就是最底层的对象,0位置是最上层的对象。
关键代码:
private var _red:DraggableSprite;
event.target.drag( );
event.target.drop( );
var dropTargets:Array = getObjectsUnderPoint( new Point( mouseX, mouseY ) );
if ( dropTargets[ dropTargets.length - 2 ] == _red) {
...
}
========================================================================
5、ascb.drawing.pen类,绘图类
Pen 类还定义了一些API 能更简单的画出弧线,椭圆,多边形,星形等等
使用方法如:var pen:Pen = new Pen(sampleSprite.graphics);
一、画虚线:
var pen:Pen = new Pen(sampleSprite.graphics); pen.drawLine(0, 0, 10, 0); pen.drawLine(15, 0, 25, 0); pen.drawLine(30, 0, 40, 0); pen.drawLine(45, 0, 55, 0);
******************************************
二、画扇形:
使用Pen.drawArc( )方法,参数说明:
x
扇形中心的x坐标(圆的中心)
y
扇形中心的y坐标
radius
扇形半径
arc
扇形度数,指定为角度
startingAngle
扇形开始角度,默认为0
radialLines
布尔值,指示是否画出扇形两端点到中心的直线,默认为false。
下面的例子画出了半径为50,角度为80,开始角度为20的扇形:
var pen:Pen = new Pen(graphics);
pen.drawArc(100, 100, 50, 80, 20, true);
********************************************
三、画椭圆
使用Pen.drawEllipse( )方法,参数说明:
x
椭圆中心的x坐标
y
椭圆中心的y坐标
xRadius
椭圆x方向的半径
yRadius
椭圆y方向的半径
下面的代码定义了Pen对象并画出一个椭圆:
var pen:Pen = new Pen(sampleSprite.graphics);
pen.drawEllipse(100, 100, 100, 50);
********************************************************
四、画三角形
使用Pen.drawTriangle( )方法,给出两条边的长度和夹角就能确定一个三角形,参数说明:
坐标
ab
a(夹角点)到b的长度
ac
a(夹角点)到c的长度
angle
ab与ac的夹角
rotation
三角形的旋转角度。如果是0或undefined,那么ac平行于x轴。
例子:
var pen:Pen = new Pen(sampleSprite.graphics);
pen.drawTriangle(100, 100, 100, 200, 40);
下面的代码填充三角形:
var pen:Pen = new Pen(sampleSprite.graphics);
pen.beginFill(0xFF0000);
pen.drawTriangle(100, 100, 100, 200, 40);
pen.endFill( );
****************************************************************
五、画多边形
使用Pen.drawRegularPolygon( )方法,参数说明:
x
多边形中心的x坐标
y
多边形中心的y坐标
sides
多边形边数
length
边长度
rotation
旋转角度
例:
var pen:Pen = new Pen(sampleSprite.graphics);
// 画出长度为50的5边形
pen.drawRegularPolygon(100, 100, 5, 50);
*****************************************************************
六、画星形
使用Pen.drawStar( )方法,参数说明:
x
星形中心的x坐标
y
星形中心的y坐标
points
星形的顶点数
innerRadius
内半径
outerRadius
外半径
rotation
旋转角度,默认为0
例:
var pen:Pen = new Pen(sampleSprite.graphics);
pen.drawStar(100, 100, 5, 50, 100);
=======================================================================
6、用位图填充图形
使用Graphics.beginBitmapFill( )方法,参数说明:
bitmap
填充用的BitmapData对象
matrix
默认下不需要应用转换,也可指定flash.geom.Matrix对象进行位图的缩放,旋转,倾斜,透明等
变换。
repeat
布尔值,指定是否平铺位图,默认为true。
smooth
布尔值,指示对位图进行光滑处理,默认为false
下面的例子通过URL载入位图,拷贝到BitmapData对象上,使用BitmapData对象填充圆:
package { import flash.display.Sprite; import flash.geom.Matrix; import flash.display.Loader; import flash.net.URLRequest; import flash.display.BitmapData; import flash.events.Event; public class Drawing extends Sprite { private var _loader:Loader; public function Drawing( ) { _loader = new Loader( ); _loader.load(new URLRequest("http://www.rightactionscript.com/samplefiles/image2.jpg")); _loader.contentLoaderInfo.addEventListener(Event.COMPELTE, onImageLoad); } private function onImageLoad(event:Event):void { var bitmap:BitmapData = new BitmapData(_loader.width, _loader.height); bitmap.draw(_loader, new Matrix( )); var matrix:Matrix = new Matrix( ); matrix.scale(.1, .1); var sampleSprite:Sprite = new Sprite( ); sampleSprite.graphics.lineStyle( ); sampleSprite.graphics.beginBitmapFill(bitmap, matrix); sampleSprite.graphics.drawCircle(100, 100, 100); sampleSprite.graphics.endFill( ); addChild(sampleSprite); } } }