【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 cdt 于 2019-1-26 16:53 编辑
原理: 1、确保拖动对象在鼠标点上,如果不确定会出现瞬间移动的感觉 2、确保触碰到非通行区域,跳回到没触碰的点 [Actionscript3] 纯文本查看 复制代码 import flash.events.MouseEvent;
import flash.events.Event;
car.buttonMode = true;
car.addEventListener(MouseEvent.MOUSE_DOWN,downH);
var diffX:Number, diffY:Number;
var canMove:Boolean = false;
function downH(e){
diffX = car.x - mouseX;
diffY = car.y - mouseY;
canMove = true;
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveH);
stage.addEventListener(MouseEvent.MOUSE_UP,upH);
addEventListener(Event.ENTER_FRAME, onEnter);
}
function upH(E){
stage.removeEventListener(MouseEvent.MOUSE_UP,upH);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveH);
removeEventListener(Event.ENTER_FRAME, onEnter);
}
//确保在非通行区域可以拖动
function moveH(E){
var xx = car.x;
var yy = car.y;
if (canMove){
car.x = mouseX + diffX;
car.y = mouseY + diffY;
}
if (HitTest.complexHitTestObject(car,bg)){
car.x = xx;
car.y = yy;
}
}
//确保车在鼠标点上
function onEnter(e:Event):void {
if (car.hitTestPoint(mouseX,mouseY)) {
if (!canMove) {
canMove = true;
diffX = car.x - mouseX;
diffY = car.y - mouseY;
}
}
else {
canMove = false;
}
}
|