mardi 24 novembre 2015

Drag and Drop Lines For Squares

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. 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; 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); }

Aucun commentaire:

Enregistrer un commentaire