Skip to main content
Participant
November 24, 2015
Question

drag and drop lines

  • November 24, 2015
  • 2 replies
  • 387 views

I want to create a drag and drop program that i can create number of vertical or horizontal lines to form squares. But i don't know how to do it as i am a newbee. Cry for help. Thx so much!!!!!

Here is my fla file.

http://42.2.102.117:17531/fbsharing/KaRkFkY1

This topic has been closed for replies.

2 replies

kaicharAuthor
Participant
November 25, 2015

Here is my code , I created two buttons that have the instance name called addLine and addLine2 , one for create vertical lines , and one for horizontal lines. THX MUCH!!!!!

var bWidth:Number = 1000;

var bHeight:Number = 680;

var grid:Number = 100; // Size of the grid and number of lattice points in each direction

var dotsWide:Number = Math.ceil(bWidth/grid) - 1;

var dotsHigh:Number = Math.ceil(bHeight/grid) - 1;

var board:Sprite = new Sprite();

var myLine:Sprite = new Sprite();

//var myLine:Array = new Array();

stage.addChild(board);

board.addChild(myLine);

board.graphics.lineStyle(1,0);

board.graphics.beginFill(0xCCCCCC);

board.graphics.drawRect(0,0,bWidth,bHeight);

board.graphics.endFill();

// Add a bunch of circles to represent lattice points

board.graphics.beginFill(0x000000);

for (var i=1; i<=dotsHigh; i++) {

  for (var j=1; j<=dotsWide; j++) {

  board.graphics.drawCircle(j*grid,i*grid,0.5);

  }

}

board.graphics.endFill();

board.x = 20;

board.y = 20;

addLine.addEventListener(MouseEvent.CLICK , addLineFunction);

function addLineFunction(event:MouseEvent):void{

  myLine.graphics.lineStyle(10,0);

  myLine.graphics.beginFill(0xFF5555);

  myLine.graphics.lineTo(0,100);

  myLine.graphics.endFill();

}

addLine2.addEventListener(MouseEvent.CLICK , addLineFunction2);

function addLineFunction2(event:MouseEvent):void{

  myLine.graphics.lineStyle(10,0);

  myLine.graphics.beginFill(0xFF5555);

  myLine.graphics.lineTo(100,0);

  myLine.graphics.endFill();

}

// Part II -- Add drag and drop functionality

myLine.addEventListener(MouseEvent.MOUSE_DOWN, startMove);

function startMove(evt:MouseEvent):void {

  stage.addEventListener(MouseEvent.MOUSE_MOVE, pointMove);

}

function pointMove(e:MouseEvent):void {

  myLine.x = goodX(board.mouseX);

  myLine.y = goodY(board.mouseY);

  e.updateAfterEvent();

}

stage.addEventListener(MouseEvent.MOUSE_UP, stopAll);

function stopAll(e:MouseEvent):void {

  stage.removeEventListener(MouseEvent.MOUSE_MOVE, pointMove);

  }

// Part III -- Helper functions to stay within boundary AND snap to grid

function goodX(inX:Number):Number {

  if (inX > grid*dotsWide) {

  return grid*dotsWide;

  }

  if (inX < grid) {

  return grid;

  }

  return grid*Math.round(inX/grid);

}

function goodY(inY:Number):Number {

  if (inY > grid*dotsHigh) {

  return grid*dotsHigh;

  }

  if (inY < grid) {

  return grid;

  }

  return grid*Math.round(inY/grid);

}

kglad
Community Expert
Community Expert
November 25, 2015

function addLineFunction(event: MouseEvent): void {

    myLine.graphics.lineStyle(10, 0);

    for (var i = 1; i <= dotsHigh; i++) {

        myLine.graphics.moveTo(0, i * grid);

        myLine.graphics.lineTo(dotsWide * grid, i * grid);

    }

}

function addLineFunction2(event: MouseEvent): void {

    myLine.graphics.lineStyle(10, 0);

    for (var j = 1; j <= dotsWide; j++) {

        myLine.graphics.moveTo(j * grid, 0);

        myLine.graphics.lineTo(j * grid, dotsHigh * grid);

    }

}

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

kglad
Community Expert
Community Expert
November 24, 2015

i've been watching your post drop down the list all day.

not many of us download and correct files.  you can get more help from the as3 forum with problems that you can pinpoint/isolate with a handful of lines of code.