as3.0代码怎样判断情况后再移除某个监听事件
红.addEventListener(MouseEvent.MOUSE_DOWN,onDown_红)红.addEventListener(MouseEvent.MOUSE...
红.addEventListener(MouseEvent.MOUSE_DOWN, onDown_红)
红.addEventListener(MouseEvent.MOUSE_UP, onUp_红)
function onDown_红(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.startDrag(false);
}
function onUp_红(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.stopDrag();
}
黄.addEventListener(MouseEvent.MOUSE_DOWN, onDown_黄)
黄.addEventListener(MouseEvent.MOUSE_UP, onUp_黄)
function onDown_黄(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.startDrag(false);
}
function onUp_黄(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.stopDrag();
}
比如上面的拖动代码,我有七个物体是可以拖动的,现在要识别那种颜色已经拖动过了,在跳到下一帧的时候我要移除拖动过颜色的事件,其他的不移除。如我只拖了红黄,跳帧时就只移除红黄的事件,这个要怎么识别记录我拖动过的颜色? 展开
红.addEventListener(MouseEvent.MOUSE_UP, onUp_红)
function onDown_红(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.startDrag(false);
}
function onUp_红(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.stopDrag();
}
黄.addEventListener(MouseEvent.MOUSE_DOWN, onDown_黄)
黄.addEventListener(MouseEvent.MOUSE_UP, onUp_黄)
function onDown_黄(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.startDrag(false);
}
function onUp_黄(event:MouseEvent):void{
var $mc:MovieClip = event.currentTarget as MovieClip;
$mc.stopDrag();
}
比如上面的拖动代码,我有七个物体是可以拖动的,现在要识别那种颜色已经拖动过了,在跳到下一帧的时候我要移除拖动过颜色的事件,其他的不移除。如我只拖了红黄,跳帧时就只移除红黄的事件,这个要怎么识别记录我拖动过的颜色? 展开
展开全部
你可以添加MouseMove事件, 如果有触发此事件, 就表示有拖动,然后再MouseUp事件监听中移除相应的Mouse事件就行了。。。
大致的代码如下:
var currentMC:MovieClip;
var isMoved:Boolean = false;
红.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
黄.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
function onDown(event:MouseEvent):void{
currentMC = event.currentTarget as MovieClip;
currentMC.startDrag(false);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
}
function onUp(event:MouseEvent):void{
currentMC.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
if(isMoved){
currentMC.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
isMoved = false;
}
}
function onMove(event:MouseEvent):void{
isMoved = true;
}
另外, 最好不要用中文命名, 容易出错。。。。
更多追问追答
追问
我是要跳到下一帧的时候再移除,而且是同时移除,也就是说其实我还有一个用来跳帧的按钮的,只有点了那个按钮才进行移除事件
追答
那你就需要另外定义一个数组, 存储已经拖拽的对象
例如
var hasMovedArray:Array = [];
修改上面代码
if(isMoved){
}
中的语句, 替换为
hasMovedArray.push(currentMC);
isMoved = false;
然后再跳转帧的里面, 加上
for(var i:int = 0; i < hasMovedArray.length; i++){
hasMovedArray[i].removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
}
hasMovedArray = [];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询