Seven Yu @ 04/09/2008 (6:13 pm)

用最简单的判断确定9个区域

Tags: , , ::

有时候我们会遇到需要判断鼠标所在场景区域的情况, 类似的典型应用是地图导航时鼠标的形状, 即根据鼠标所在区域显示不同的鼠标图标 (见演示).

但如果直接判断可能会涉及到嵌套判断, 代码量大, 无疑那样是很不优雅的. 上面例子中使用了非常简单的判断就确定了9个区域. (见示意图)

4 3 5
1 0 2
7 6 8

上表中显示了各方向对应的值, 左中右进行了一组判断, 确定1, 0, 2 三个中的一个值, 上中下也进行一组判断, 确定了3, 0, 6 三个中的一个值, 其他四个方向的值为以上两个值(相邻两个方向)相加得出(如果两个值都不为0).

请看具体实现代码或下载源文件:

PLAIN TEXT >> ACTIONSCRIPT:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  3.  backgroundColor="#eeeeee" initialize="initApp();">
  4. <mx:Script>
  5.     <![CDATA[
  6.     /**
  7.      * Author: Seven Yu
  8.      * Email: dofyyu at gmail dot com
  9.      * URI: http://phpz.org/?p=48
  10.      * */
  11.     import mx.controls.Image;
  12.    
  13.     // 定义鼠标图标
  14.     [Embed(source="icons/center.png")]
  15.     private var mCenter:Class;
  16.     [Embed(source="icons/left.png")]
  17.     private var mLeft:Class;
  18.     [Embed(source="icons/right.png")]
  19.     private var mRight:Class;
  20.     [Embed(source="icons/up.png")]
  21.     private var mUp:Class;
  22.     [Embed(source="icons/down.png")]
  23.     private var mDown:Class;
  24.     [Embed(source="icons/left_up.png")]
  25.     private var mLeftUp:Class;
  26.     [Embed(source="icons/left_down.png")]
  27.     private var mLeftDown:Class;
  28.     [Embed(source="icons/right_up.png")]
  29.     private var mRightUp:Class;
  30.     [Embed(source="icons/right_down.png")]
  31.     private var mRightDown:Class;
  32.     // 背景图片
  33.     [Embed(source="images/map.png")]
  34.     private var iMap:Class;
  35.     // 图标标识索引
  36.     private var iconIndex:int;
  37.     private var oldIndex:int;
  38.     // 移动步长
  39.     private var xStep:int = 0;
  40.     private var yStep:int = 0;
  41.     // 背景图对象
  42.     private var _bgImage:Image = new Image();
  43.     // 移动速度 和 鼠标偏移量
  44.     private const SPEED:Number = 5;
  45.     private const OFFSET:Number = 8; // 图片大小的一半
  46.     // 角度数组 (没有应用)
  47.     private var angleArray:Array = [180, 0, -90, -135, -45, 90, 135, 45];
  48.     // 图标数组
  49.     private var iconArray:Array = [mCenter, mLeft, mRight, mUp, mLeftUp,
  50.                                 mRightUp, mDown, mLeftDown, mRightDown];
  51.     // 偏移倍数数组
  52.     private var offsetArray:Array = [{x:-1, y:-1}, {x:0, y:-1}, {x:-2, y:-1}, {x:-1, y:0},
  53.                             {x:0, y:0}, {x:-2, y:0}, {x:-1, y:-2}, {x:0, y:-2}, {x:-2, y:-2}];
  54.     /**
  55.      * 初始化
  56.      * */
  57.     private function initApp():void
  58.     {   
  59.         // 关闭滚动条
  60.         horizontalScrollPolicy = "off";
  61.         verticalScrollPolicy = "off";
  62.         // 绑定事件
  63.         addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
  64.         addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  65.         // 添加背景图
  66.         _bgImage.source = iMap;
  67.         addChild(_bgImage);
  68.     }
  69.     /**
  70.      * 鼠标移动处理函数
  71.      * */
  72.     private function moveHandler(event:MouseEvent):void
  73.     {
  74.         var leftLine:Number  = width / 3;     // 左参考线
  75.         var rightLine:Number = leftLine * 2// 右参考线
  76.         var upLine:Number    = height / 3;    // 上参考线
  77.         var downLine:Number  = upLine * 2;    // 下参考线
  78.        
  79.         var mouseXFlag:int// 左右参考值
  80.         var mouseYFlag:int// 上下参考值
  81.         // iconIndex = mouseXFlag + mouseYFlag;
  82.         // mouseXFlag 和 mouseYFlag 的参考值及 iconIndex 的计算方法见示意图
  83.        
  84.         // 左右参考值
  85.         if (mouseX <leftLine)
  86.         {
  87.             xStep = 1;
  88.             mouseXFlag = 1;
  89.         }
  90.         else if (mouseX> rightLine)
  91.         {
  92.             xStep = -1;
  93.             mouseXFlag = 2;
  94.         }
  95.         else
  96.         {
  97.             xStep = 0;
  98.             mouseXFlag = 0;
  99.         }
  100.         // 上下参考值
  101.         if (mouseY <upLine)
  102.         {
  103.             yStep = 1;
  104.             mouseYFlag = 3;
  105.         }
  106.         else if (mouseY> downLine)
  107.         {
  108.             yStep = -1;
  109.             mouseYFlag = 6;
  110.         }
  111.         else
  112.         {
  113.             yStep = 0;
  114.             mouseYFlag = 0;
  115.         }
  116.         // 计算图标索引
  117.         iconIndex = mouseXFlag + mouseYFlag;
  118.         // 应用索引
  119.         if (oldIndex != iconIndex)
  120.         {
  121.             oldIndex = iconIndex;
  122.             cursorManager.removeAllCursors();
  123.             cursorManager.setCursor(iconArray[iconIndex], 2,
  124.                 offsetArray[iconIndex].x * OFFSET, offsetArray[iconIndex].y * OFFSET);
  125.         }
  126.     }
  127.     /**
  128.      * 移动背景处理函数
  129.      * */
  130.     private function enterFrameHandler(event:Event):void
  131.     {
  132.         if (_bgImage.x>= width - _bgImage.contentWidth - xStep * SPEED
  133.                 && _bgImage.x <= 0 - xStep * SPEED)
  134.             _bgImage.x += xStep * SPEED;
  135.         else
  136.             xStep = 0;
  137.        
  138.         if (_bgImage.y>= height - _bgImage.contentHeight - yStep * SPEED
  139.                 && _bgImage.y <= 0 - yStep * SPEED)
  140.             _bgImage.y += yStep * SPEED;
  141.         else
  142.             yStep = 0;
  143.     }
  144.     ]]>
  145. </mx:Script>
  146. </mx:Application>

Seven Yu @ 04/08/2008 (3:09 pm)

几款不错的 Image Viewer.

Tags: , , ::

这几款 Image Viewer 都出自同一公司, 表现形式都很不错, 值得学习. 其中有一个 3D 表现形式的很酷 :D

Seven Yu @ 04/07/2008 (10:56 pm)

绘制两点间较平滑曲线

Tags: , , , ::

本例通过两条贝塞尔曲线来实现两点间的较平滑曲线.
演示:

图示:

参考主要实现脚本, 或下载 fla 源文件:

PLAIN TEXT >> ACTIONSCRIPT:
  1. public function drawLine():void
  2. {
  3. // 第一点坐标
  4. var x1:Number = _obj1.x;
  5. var y1:Number = _obj1.y;
  6. // 第二点坐标
  7. var x2:Number = _obj2.x;
  8. var y2:Number = _obj2.y;
  9.  
  10. // 曲线方向
  11. var dir:Boolean = Math.abs(x2 - x1)> Math.abs(y2 - y1);
  12.  
  13. // 中点坐标
  14. var xc:Number = (x2 - x1) / 2 + x1;
  15. var yc:Number = (y2 - y1) / 2 + y1;
  16.  
  17. // 第一条曲线控制点坐标
  18. var bx1:Number = dir ? (xc - x1) / 2 + x1 : x1;
  19. var by1:Number = dir ? y1 : (yc - y1) / 2 + y1;
  20.  
  21. // 第二条曲线控制点坐标
  22. var bx2:Number = dir ? (x2 - xc) / 2 + xc : x2;
  23. var by2:Number = dir ? y2 : (yc - y2) / 2 + y2;
  24.  
  25. // 绘制曲线
  26. graphics.clear();
  27. graphics.lineStyle(1, 0x008000);
  28. graphics.moveTo(x1, y1);
  29. graphics.curveTo(bx1, by1, xc, yc);
  30. graphics.curveTo(bx2, by2, x2, y2);
  31. }