如何设置拖动X限制 [英] How to set drag X limit

查看:30
本文介绍了如何设置拖动X限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些可拖动/可放置的栏.所以它只能拖动 x 因为这是选择栏.问题是选择按钮可拖动超出舞台限制.我只想在阶段限制内拖动.我尝试了一些方法 hitTestPoint 和 newshape 但没有奏效.

I'm working on some draggable/droppable bar.So it's draggable only x because this is select bar.Problem is select button draggable out of stage limit. I want to draggable only in stage limit.I tried some methods hitTestPoint and newshape but it didn't work.

// define lock on y-axis
var LOCKY:Number = secbuton.y;
 
stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
function _mouseMove(e:MouseEvent):void
{
if(secbuton.y != LOCKY) secbuton.y = LOCKY;
}
 
// dragging
secbuton.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
function _mouseDown(e:MouseEvent):void
{
secbuton.startDrag(false, new Rectangle(35,345,420));
secbuton.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
}
 
// dropping
function _mouseUp(e:MouseEvent):void
{
secbuton.stopDrag();
secbuton.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);

            if(secbuton.hitTestObject(kose1)){
    secbuton.x = levcbk1.x +5  
    }

    if(secbuton.hitTestObject(lev2)){
    secbuton.x = levcbk2.x +5  
    }
 if(secbuton.hitTestObject(lev3)){
    secbuton.x = levcbk3.x +5
    }
                 if(secbuton.hitTestObject(lev4)){
    secbuton.x = levcbk4.x +5
    }

                         if(secbuton.hitTestObject(lev5)){
    secbuton.x = levcbk5.x +5
    }
}

更新

通常酒吧是这样工作的.

Normally bar work like this.

[![在此处输入图片描述][1]][1]

[![enter image description here][1]][1]

但是当点击边界或尝试拖动到 y 轴时会出现问题.

But problem is there when hit the borders or when try drag to y axis.

[![在此处输入图片描述][2]][2]

[![enter image description here][2]][2]

推荐答案

不要使用 MOUSE_LEAVE.使用 MOUSE_UP 但在 stage 对象上 - MOUSE_UPstage 对象调度,即使您在 Flash 播放器外释放按钮窗口(基本上任何地方)

Don't use MOUSE_LEAVE. Use MOUSE_UP but on stage object - MOUSE_UP is dispatched by stage object even it you release button outside the flash player window (basically anywhere)

所以只需更改这些行

secbuton.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
secbuton.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);

对此:

stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);

正如Organis所说.Rectangle 参数是 x , y , width 和 height.所以从我在你想要的 gif 上看到的设置来看:

And as Organis said. Rectangle parameters are x , y , width and height. So from what I see on the gifs you want set it to:

secbuton.startDrag(false, new Rectangle(35,345,420-35,0));

如果您将矩形高度设置为 0,您的对象将被锁定在 y 轴(在您的情况下为 345),因此您不需要使用 LOCKY变量和移动侦听器.

If you set rectangle heigh to 0, your object will be locked on y-axis (345 in your case) so you don't need to use the LOCKY variable and move listener.

这篇关于如何设置拖动X限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆