应用思考-教育技术论坛
标题:
一个简单的拖动图形和文字的匹配例子
[打印本页]
作者:
想换个名字可以吗
时间:
2014-3-14 20:41
标题:
一个简单的拖动图形和文字的匹配例子
用fd写的例子
Main.as
package
{
import flash.accessibility.AccessibilityProperties;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
/**
* ...
* @author langxianmeng
*/
public class Main extends Sprite
{
public var shapes:Array = new Array();
public var currSpr:ShapeSpr ;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var txtspr:TxtSpr = new TxtSpr('正方形');
this.addChild(txtspr);
var txtspr1:TxtSpr = new TxtSpr('长方形',2);
this.addChild(txtspr1);
shapes.push(txtspr);
shapes.push(txtspr1);
txtspr1.x = txtspr.x + 200;
var squr:ShapeSpr = new ShapeSpr(1);
this.addChild(squr);
squr.x = squr.y = 100;
var squr1:ShapeSpr = new ShapeSpr(2);
this.addChild(squr1);
squr1.x = squr1.y = 100;
squr1.x = squr.x + squr.width + 20;
var squr2:ShapeSpr = new ShapeSpr(3);
this.addChild(squr2);
squr2.x = squr2.y = 100;
squr2.x = squr1.x + squr1.width + 20;
this.addEventListener(MouseEvent.MOUSE_DOWN, this_mouseDown);
}
private function this_mouseDown(e:MouseEvent):void
{
trace("main mousedown");
this.currSpr = e.target as ShapeSpr;
this.addEventListener(Event.ENTER_FRAME, this_enterFrame);
trace(e.target.name);
}
private function this_enterFrame(e:Event):void
{
for (var i:int = 0; i < shapes.length; i++) {
if (this.currSpr.hitTestObject(shapes[i])) {
if (currSpr.flag == TxtSpr(shapes[i]).flag) {
//this.removeEventListener(Event.ENTER_FRAME, this_enterFrame);
currSpr.stopDrag();
//匹配后禁止拖动
//currSpr.mouseEnabled = false;
currSpr.x = TxtSpr(shapes[i]).x + ( TxtSpr(shapes[i]).width - currSpr.width) / 2;
currSpr.y = TxtSpr(shapes[i]).y + TxtSpr(shapes[i]).height + 2;
//停止拖动并自动排版
//这里保存了对二者的引用,所以很容易进行排版
//currSpr是当前拖动的图形
//shapes[i]是文字
//注意要流畅
}
}
}
}
}
}
复制代码
TxtSpr.as
package
{
import flash.display.Sprite;
import flash.text.TextField;
/**
* ...
* @author langxianmeng
*/
public class TxtSpr extends Sprite
{
public var flag:int = 1;
public function TxtSpr(str:String,flag:int=1)
{
this.flag = flag;
var txt:TextField = new TextField();
txt.height = 30;
txt.width = 60;
txt.mouseEnabled = false;
txt.text = str;
this.addChild(txt);
trace(txt.width);
this.graphics.beginFill(0xaaaaaa);
this.graphics.drawRect(0, 0, txt.width, txt.height);
this.graphics.endFill();
}
}
}
复制代码
ShapeSpr.as
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
/**
* ...
* @author langxianmeng
*/
public class ShapeSpr extends Sprite
{
public var flag:int;
//是否匹配的标识
public var isMatched:Boolean;
public function ShapeSpr(flag:int)
{
this.buttonMode = true;
this.flag = flag;
//根据flag标识绘制不同的图形
switch(flag) {
case 1:
this.drawSqur(); break;
case 2:
this.drawRect(); break;
case 3:
this.drawTri(); break;
default:;
}
this.addEventListener(MouseEvent.MOUSE_DOWN, this_mouseDown);
this.addEventListener(MouseEvent.MOUSE_UP, this_mouseUp);
}
//形状接口有待对外开放
//这样可以绘制同种图像的不同例子
//比如不同边长的长方形等
private function drawSqur():void {
this.graphics.beginFill(Math.random()*0xffffff);
this.graphics.drawRect(0, 0, 40, 40);
this.graphics.endFill();
}
private function drawRect():void {
this.graphics.beginFill(Math.random()*0xffffff);
this.graphics.drawRect(0, 0, 80, 40);
this.graphics.endFill();
}
private function drawTri():void {
this.graphics.beginFill(Math.random()*0xffffff);
this.graphics.lineTo(100, 0);
this.graphics.lineTo(0, 100);
this.graphics.lineTo(0, 0);
this.graphics.endFill();
}
public function this_mouseUp(e:MouseEvent):void
{
trace("up");
this.stopDrag();
}
public function this_mouseDown(e:MouseEvent):void
{
trace("shapespr mouse down");
e.target.parent.setChildIndex(e.target,e.target.parent.numChildren-1)
//this.setChildIndex(this,this.parent.numChildren );
this.startDrag();
}
}
}
复制代码
欢迎光临 应用思考-教育技术论坛 (http://etthink.com/)
Powered by Discuz! X3.4