本文介绍了AS3如何的startDrag仅在x轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想只在x轴拖动一个红色方块。我已经制定了一个简单的脚本,理论上应该工作,但它的行为不正常。这是一个有点难以explain..the方保持起始于错误的位置和舞台上的地位似乎正在改变所以有时你不能一路拖到广场上的权利...

  red.buttonMode = TRUE;
red.addEventListener(的MouseEvent.MOUSE_DOWN,dragHandler);

功能dragHandler(E:的MouseEvent):无效{
    VAR ypos:数= e.currentTarget.y;
    VAR XPOS:数= e.currentTarget.x;

    e.currentTarget.startDrag(假,新的Rectangle(-xpos,ypos,stage.stageWidth,0));
}


red.addEventListener(侦听MouseEvent.MOUSE_UP,dropHandler);

功能dropHandler(E:的MouseEvent){
    //跟踪(红起来);
    e.currentTarget.stopDrag();
}
 

解决方案

您可以尝试不同的方法,采用的MouseEvent.MOUSE_MOVE ,因为使用矩形动态边界将是棘手的。

  //定义在y轴锁
VAR LOCKY:数= target.y;

//的MouseEvent.MOUSE_MOVE
stage.addEventListener(的MouseEvent.MOUSE_MOVE,_mouseMove);
功能_mouseMove(E:MouseEvent)方法:无效
{
    如果(target.y = LOCKY!)target.y = LOCKY;
}

//拖动
target.addEventListener(的MouseEvent.MOUSE_DOWN,_mouseDown);
功能_mouseDown(E:MouseEvent)方法:无效
{
    target.startDrag();
    target.addEventListener(侦听MouseEvent.MOUSE_UP,_mouseUp);
}

//下探
功能_mouseUp(E:MouseEvent)方法:无效
{
    target.stopDrag();
    target.removeEventListener(侦听MouseEvent.MOUSE_UP,_mouseUp);
}
 

I have a red square that I want to drag only on the x-axis. I've worked out a simple script, which theoretically should work, but it's not behaving properly. It's a bit hard to explain..the square keeps starting at the wrong position and the stage position seems to be changing so sometimes you can't drag the square all the way to the right...

red.buttonMode = true;
red.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);

function dragHandler(e:MouseEvent):void {
    var ypos:Number = e.currentTarget.y;
    var xpos:Number = e.currentTarget.x;

    e.currentTarget.startDrag(false,new Rectangle(-xpos,ypos,stage.stageWidth,0));
}


red.addEventListener(MouseEvent.MOUSE_UP, dropHandler);

function dropHandler(e:MouseEvent) {
    //trace("red up");
    e.currentTarget.stopDrag();
}
解决方案

You could try a different approach that incorporates MouseEvent.MOUSE_MOVE, as using a rectangle for a dynamic boundary would be tricky.

// define lock on y-axis
var LOCKY:Number = target.y;

// MouseEvent.MOUSE_MOVE
stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
function _mouseMove(e:MouseEvent):void
{
    if(target.y != LOCKY) target.y = LOCKY;
}

// dragging
target.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
function _mouseDown(e:MouseEvent):void
{
    target.startDrag();
    target.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
}

// dropping
function _mouseUp(e:MouseEvent):void
{
    target.stopDrag();
    target.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);
}

这篇关于AS3如何的startDrag仅在x轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:35