Changing text dynamically with drag / drop in Flash CS6 (AS3)

巧了我就是萌 提交于 2019-12-25 16:54:43

问题


I have some incredibly simple code that works fine in letting me drag a "slider" button horizontally. However, I also want the text that appears above the object to change depending upon what the x-coordinate is of the object I'm dragging.

Here's the simple code:

var rectangle:Rectangle = new Rectangle(31,944,179,0);
Button.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

function fl_ClickToDrag(event:MouseEvent):void
   {
    Button.startDrag(false, rectangle);
   }

Button.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
   {
    Button.stopDrag();
    gotoAndPlay(20);
}

What I'm wanting to do is have the system determine where the "Button" is in terms of its x-coordinate, and if the x-coordinate is higher than, say, 50, for the text above the "Button" to say "50+", and if the x-coordinate is higher than 100 for the text to change to "100+". I'm also not sure if the x-coordinate should be relative to the rectangle or relative to the entire screen.

Any and all help is appreciated.


回答1:


You can use a boolean var to indicate if your button is dragged and if, then update your text field like this :

var is_dragged:Boolean = false;
var rectangle:Rectangle = new Rectangle(0, 100, stage.stageWidth - button.width, 0);

stage.addEventListener(Event.ENTER_FRAME, _onEnterFrame);
function _onEnterFrame(e:Event):void {
    if(is_dragged){
        text_field.text = String(Math.round(button.x / 50) * 50) + '+';
    }
}

button.addEventListener(MouseEvent.MOUSE_DOWN, button_onPress);
function button_onPress(e:MouseEvent):void {    
    button.startDrag(false, rectangle);
    is_dragged = true;
}

button.addEventListener(MouseEvent.MOUSE_UP, button_onRelease);
function button_onRelease(e:MouseEvent):void {
    button.stopDrag();
    is_dragged = false;
}

You can see this code working here.

Hope that can help.



来源:https://stackoverflow.com/questions/28869325/changing-text-dynamically-with-drag-drop-in-flash-cs6-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!