你能不能帮我弄清楚是什么原因造成我程序中的意外输出?我用过flash cs5和as3。这是一个拖放游戏。
找不到任何错误,但我的 MOUSE_UP 和 MOUSE_DOWN 无法正常运行。 (在我看来)
我说这是因为,当我测试代码时,当我点击对象时。鼠标有点像点击或类似的功能。我不能放弃这个物体。
这是我用于拖放的代码集
wintersquash_mc.buttonMode = true;
wintersquash_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
wintersquash_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObject(event:MouseEvent):void
{
event.target.startDrag(true);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropObject(event:MouseEvent):void
{
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget)
{
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
scorer++;
score.text = String(scorer);
}
else
{
event.target.x = objectoriginalX;
event.target.y = objectoriginalY;
}
}
希望有人能告诉我为什么它不能正常工作:(
答案 0 :(得分:0)
尝试将MOUSE_UP
添加到舞台,而不是动画片段。这应该确保无论鼠标在哪里都会捕获事件。
此外,您可以考虑在pickupObject()运行后仅添加MOUSE_UP
侦听器。像这样:
wintersquash_mc.buttonMode = true;
wintersquash_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
function pickupObject(event:MouseEvent):void
{
event.target.startDrag(true);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
stage.addEventListener(MouseEvent.MOUSE_UP, dropObject);
}
这将要求您的类具有对Stage对象的引用。此外,您必须在完成后从舞台中显式删除侦听器。不过试一试,看看它是否适合你。
答案 1 :(得分:0)
我通常会像这样拖拉:
wintersquash_mc.buttonMode = true;
wintersquash_mc.mouseChildren = false;
//this way you won't start dragging i.e. a sub-movieclip of the button
wintersquash_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
function pickupObject(event:MouseEvent):void
{
event.target.startDrag(true);
event.target.parent.addChild(event.target);
// is it necessary to add the clip to its parent?
// I would rather make a var on the root,
// and dump the event.target into that like this:
// root.nowDragged = event.target;
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
stage.addEventListener(MouseEvent.MOUSE_UP, dropObject);
}
function dropObject(event:MouseEvent):void
{
root.nowDragged.stopDrag(true);
stage.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
// this removes the listener to prevent false alarms on other mouse ups
//... (rest of your code)
}
我希望这会有所帮助。