Actionscript 3 当对象位于正确位置时,禁用拖动该对象
当“物品”在正确的位置时,我有一个禁用拖动的问题。我试图使用Actionscript 3 当对象位于正确位置时,禁用拖动该对象,actionscript-3,flash,Actionscript 3,Flash,当“物品”在正确的位置时,我有一个禁用拖动的问题。我试图使用e.currentTarget.removeEventListener(MouseEvent.MOUSE\u DOWN,dragObject),但似乎什么也没发生。我认为它不起作用,因为我要删除的事件不是` dragObject',而是它返回的函数…请帮助; 这是我的密码 function dragObject(indx1:int,indx2:int):Function { return function(event:MouseEve
e.currentTarget.removeEventListener(MouseEvent.MOUSE\u 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);
你有非常具体的代码。。。这里是可拖动对象的工作示例,当它到达屏幕右侧时将停止。如您所见,在我的示例中,我不使用像您这样的构造(函数生成另一个函数)。注意
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();
}
}
}
如果你能正确格式化你的代码(缩进),那会很有帮助。谢谢你…我是StackOverflowW的新手,你的函数返回的函数太可怕了;)我认为最好将变量indx1、indx2、indx、origX和origY放在一些类变量中,并使用实名函数作为事件处理程序,而不是匿名函数。在您提供的代码中,没有任何地方可以调用dragObject和releaseObect方法。我已经加上了我叫它的地方。顺便说一下,我不知道如何将我的变量indx1、indx2、indx、origX和origY放在一些类变量中