Actionscript 3 Actionscript 3-如何使新实例可拖动?
我试图建立一个拖放游戏,用户可以建立一些使用我提供的图像。我将有一个菜单中的图像,用户可以点击并拖动到建筑面积。用户将能够添加任意数量的图像实例 我能让它的一部分工作。到目前为止,我可以单击图像并拖动它,然后创建任意多的实例。但是,一旦放置图像,我就无法单击并拖动它。 当我进行跟踪以查看名称时,会显示所有新实例都命名为hillChild1。我试着把它们命名为hillChild1、hillChild2等等,但这似乎也不起作用。。。不过,我不确定这是否是一个问题 这是我的密码:Actionscript 3 Actionscript 3-如何使新实例可拖动?,actionscript-3,flash-cs6,Actionscript 3,Flash Cs6,我试图建立一个拖放游戏,用户可以建立一些使用我提供的图像。我将有一个菜单中的图像,用户可以点击并拖动到建筑面积。用户将能够添加任意数量的图像实例 我能让它的一部分工作。到目前为止,我可以单击图像并拖动它,然后创建任意多的实例。但是,一旦放置图像,我就无法单击并拖动它。 当我进行跟踪以查看名称时,会显示所有新实例都命名为hillChild1。我试着把它们命名为hillChild1、hillChild2等等,但这似乎也不起作用。。。不过,我不确定这是否是一个问题 这是我的密码: thesubmenu
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
var myImage:Sprite = Sprite(new Hill_mc());
var i:Number=0; i++;
function onDown(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.name = "hillChild"+i;
addChild(myImage);
myImage.x = mouseX;
myImage.y = mouseY;
myImage.startDrag();
myImage.buttonMode = true;
}
function onUp(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.stopDrag();
myImage.name = "hillChild";
}
stage.addEventListener(MouseEvent.CLICK, traceName);
function traceName(event:MouseEvent):void { trace(event.target.name); }
myImage.getChild(myImage).addEventListener("mouseDown", mouseDownHandler);
stage.addEventListener("mouseUp", mouseUpHandler);
function mouseDownHandler (e:MouseEvent):void{
myImage.startDrag();
}
function mouseUpHandler (e:MouseEvent):void{
myImage.stopDrag();
}
我不熟悉StackOverflow和Actionscript 3,如果它不明显的话。您的问题很可能是在鼠标向上时创建了一个新实例(当您需要的是鼠标向下时已经创建的实例的引用时)。此外,您从不向新对象添加单击侦听器。仅在鼠标按下后将鼠标向上侦听器添加到后台(然后在鼠标向上中删除侦听器)
仅供参考,没有必要扮演精灵<代码>var myImage:Sprite=new Hill_mc()代码>很好用。哦,天哪!你是个救生员!我已经花了将近三天的时间尝试我能找到的一切。。。
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, createCopy);
var i:int=0;
var tmpImage:Sprite; //to store which image is being dragged currently
function createCopy(e:MouseEvent):void {
tmpImage = new Hill_mc();
tmpImage.name = "hillChild"+(i++); //increment every copy
addChild(tmpImage);
tmpImage.x = mouseX;
tmpImage.y = mouseY;
tmpImage.startDrag();
tmpImage.buttonMode = true;
tmpImage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); //add the mouse down to this new object
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //since the mouse is currently down, we need to listen for mouse up to tell the current copy to stop dragging
}
//this will be called when click a copy
function onDown(e:MouseEvent):void {
tmpImage = Sprite(e.currentTarget); //get a reference to the one that was clicked, so we know which object to stop dragging on the global mouse up.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //listen for the mouse up
tmpImage.startDrag();
}
function onUp(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP,onUp); //now that the mouse is released, stop listening for mouse up
tmpImage.stopDrag(); //stop dragging the one that was clicked
}