当“item”位于正确的位置时,我遇到了禁用拖动的问题。我尝试使用e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, dragObject)
,但似乎没有任何反应。我认为它不起作用,因为我要删除的事件不是`dragObject,而是它返回的函数...请帮忙;
这是我的代码
function dragObject(indx1:int,indx2:int):Function {
return function(event:MouseEvent){
var item:MovieClip=MovieClip(event.currentTarget);
item.startDrag();
var topPos:uint=item.parent.numChildren-1;
var itemSound:Sound = new Sound();
itemSound.load(new URLRequest("sounds/"+dirArray[indx1]+indx2+".mp3"));
if(!activeSound)
{
itemSound.play();
activeSound=true;
}
activeSound=false;
item.parent.setChildIndex(item, topPos);
}
function releaseObject(indx:int,origX:int,origY:int):Function{
return function(e:MouseEvent):void{
var item:MovieClip=MovieClip(e.currentTarget);
item.stopDrag();
trace(indx);
if(indx==1)
{
if (box5_mc.hitTestPoint(item.x,item.y)) {
if(insideBox5==1){
item.x=73;//2nd locations
item.y=298;
myBell.play();
}
else if(insideBox5==2){
item.x=90;//3rd locations
item.y=267;
myBell.play();}
else{
item.x=32; //1st locations
item.y=268
myBell.play();
}
insideBox5++;
e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,dragObject);
correctItems++;
}
else {
item.x=origX;
item.y=origY;
myBoing.play();
} }
我已经更新了这篇文章并包含在我调用它的地方,它实际上是我的主要功能。这是调用函数dragObject和releaseObject
的部分var itemImage:Loader = new Loader();
//loads the file on location...
itemImage.load(new URLRequest("images/"+dirArray[indexc[count-1]]+index[count2]+".png"));//load random image from random images folders
var functionOnDrag:Function = dragObject(indexc[count-1],index[count2]);
index.splice(0,1);
var functionOnRelease:Function = releaseObject(indexc[count-1],tempx-42,tempy);
trace(index);//trace index
trace(count);//trace count
count++;
pb[i].addChild(itemImage);//adds the picture on the picBox
pb[i].addEventListener(MouseEvent.MOUSE_DOWN,functionOnDrag);
pb[i].addEventListener(MouseEvent.MOUSE_UP,functionOnRelease);
答案 0 :(得分:0)
你有非常具体的代码......这里是可拖动对象的工作示例,如果它到达屏幕的右侧,它将被停止。正如您所看到的,在我的示例中,我不使用像您这样的构造(函数生成另一个函数)。请注意onDown
,onMouseUp
和onMoveCursor
方法:
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
public class StackOverflow extends Sprite {
private var _dragObject:Sprite;
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function setup():void {
var randomObject:Sprite = createObject();
addChild(randomObject);
//Place it in center of scene
randomObject.x = stage.stageWidth >> 1;
randomObject.y = stage.stageHeight >> 1;
//Register mouse down for drag
randomObject.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
}
private function onDown(e:MouseEvent):void {
//By moving mouse will check current position
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveCursor);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
_dragObject = e.currentTarget as Sprite;
_dragObject.startDrag();
}
private function onMouseUp(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveCursor);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
if (_dragObject != null) {
_dragObject.stopDrag();
_dragObject = null;
}
}
private function onMoveCursor(e:MouseEvent):void {
//Assert that right side is end of the screen
if (_dragObject.width + _dragObject.x >= stage.stageWidth) {
trace("Gotcha!")
onMouseUp(null);
}
}
private function createObject():Sprite {
var result:Sprite = new Sprite();
result.graphics.beginFill(Math.random() * 0xFFFFFF);
result.graphics.drawRect(0, 0, 20, 20);
result.buttonMode = true;
result.tabEnabled = false;
return result;
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setup();
}
}
}