actionscript-2flash-cs4

how to drag and drop button to target button in flash


I want to create a button which can be dragged and dropped toward another button so that the button location (x, y) is dragged into the location of the destination button. My code:

sejarah.onPress = function(){
     startDrag(this);}
sejarah.onRelease = function(){
     if(this.hitTest (atarget)){
       this._x = _root.atarget._x;
       this._y = _root.atarget._y;}
     else{stopDrag();}
} 

I want to achieve this: http://www.thibaud.be/#


Solution

  • on(press) {
        startDrag(this);
    }
    
    on(release) {
        stopDrag();
        _x = Math.round(_x/_width)*_width + 0;
        _y = Math.round(_y/_height)*_height + 0;
    }
    

    This is one of the most simple code that can do a job like this. You put this code inside a button and when you stop dragging this button it's position will snap to the grid.

    Your button's clip center must be at top-left corner of button else it can get weird. Also, you can change beginning of grid by changing zeros to position you need.

    You can draw a grid to see attachment.