用AS写了个,
package { import flash.display.Sprite; import flash.events.MouseEvent; /** * www.duowan.com * @author hacker47 */ [SWF(width="1000",height="600")] public class NewsSetting extends Sprite { private var items:Vector.- ; private var icons:Vector.
; private var selectItemNums:int = 6; private var itemNums:int = 15; private var startX:Number = 20; private var startY:Number = 200; private var itemWidth:Number = 80; private var currentItemInd:int; private var isCollision:Boolean = false; public function NewsSetting() { icons = new Vector. (); items = new Vector. - (); var i:int = 0; var item:Item; for (i = 0; i <= itemNums; i++ ) { var sx:Number; var sy:Number; if (i < selectItemNums) { sx = startX + i * itemWidth; sy = startY - 130; }else { sx = startX + (i - selectItemNums) * itemWidth; sy = startY; } item = new Item(sx, sy); addChild(item); items.push(item); } initItem(10); } private function initItem(length:int):void { for (var i:int = 0; i <= length; i++ ) { var icon:ItemIcon = new ItemIcon(i.toString()); icon.x = items[i].x + 4; icon.y = items[i].y + 4; icon.itemIndex = i; addChild(icon); icons.push(icon); icon.addEventListener(MouseEvent.MOUSE_DOWN, onDown); } } private function onDown(e:MouseEvent):void { var icon:ItemIcon = e.target as ItemIcon; //把当前对象置于最顶层 icon.parent.setChildIndex(icon, icon.parent.numChildren - 1); icon.addEventListener(MouseEvent.MOUSE_MOVE, onMove); icon.addEventListener(MouseEvent.MOUSE_UP, onUp); } private function onUp(e:MouseEvent):void { var icon:ItemIcon = e.target as ItemIcon; icon.stopDrag(); icon.removeEventListener(MouseEvent.MOUSE_MOVE, onMove); icon.removeEventListener(MouseEvent.MOUSE_UP, onUp); if (!isCollision) { var item:Item item= items[icon.itemIndex]; icon.x = item.x + 4; icon.y = item.y + 4; }else { insertEelementToVector(currentItemInd, icon); orderAll(); isCollision = false; } } private function onMove(e:MouseEvent):void { var icon:ItemIcon = e.target as ItemIcon; icon.startDrag(false); checkCollision(icon); } private function checkCollision(icon:ItemIcon):void { var item:Item; for (var i:int = 0; i < itemNums; i++ ) { if (icon.itemIndex == i) break; item = items[i]; //碰撞检测 if (icon.hitTestObject(item)) { if (isCollision==false) { isCollision = true; //trace("碰撞了"+i); //insertEelementToVector(i, icon); currentItemInd = i; } return ; }else { isCollision = false; } } } private function insertEelementToVector(index:int, element:ItemIcon):void { var startInd:int = element.itemIndex; icons.splice(index, 0, element); if (index > startInd) { icons.splice(startInd , 1); }else if (index < startInd) { icons.splice(startInd + 1, 1); } } private function orderAll():void { for (var i:int = 0; i < icons.length; i++ ) { icons[i].x = items[i].x + 4; icons[i].y = items[i].y + 4; icons[i].itemIndex = i; } } } } import com.duowan.util.DrawUtil; import flash.display.Stage; import flash.geom.Point; internal class Item extends flash.display.Sprite { private var _width:Number=60; private var _height:Number=60; public function Item(x:Number, y:Number) { this.x = x; this.y = y; DrawUtil.drawRect(graphics, new Point(), _width, _height); } }
package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; /** * www.duowan.com * @author hacker47 */ public class ItemIcon extends Sprite { public var itemIndex:int; private var txt:TextField = new TextField(); public function ItemIcon(txtstr:String="") { graphics.clear(); graphics.lineStyle(1); graphics.beginFill(Math.random() * 0xffffff); graphics.drawRoundRect(0, 0, 53, 51, 5); graphics.endFill(); txt.selectable = false; txt.mouseEnabled = false; txt.width = 50; txt.height = 50; //addChild(txt); //for test var tf:TextFormat = new TextFormat(); txt.text = txtstr; tf.align = "center"; tf.color = 0xFF0000; tf.size = 30; txt.setTextFormat(tf); } } }