vendredi 26 juin 2015

Flash CS6/as3: Using cursor to draw rectangle dynamically in a published swf

I'm having a bit of a problem. I'm creating a swf that will allow someone to draw a rectangle dynamically in a published swf, then they can drag a shape and place it within the rectangle they just drew.

I have the swf working so that you can draw a rectangle, no problem. Where the issue arises is when trying to drag one of the shapes on the screen into the newly-drawn rectangle, it ends up drawing another rectangle while the shape is being dragged.

My question, I guess, is how do I "turn off" the code that allows for a rectangle to be drawn after I've drawn the one I want to use?

This is the code I used for drawing the rectangle (I got this from a tutorial online). The draggable shapes don't show up until the rectangle has been drawn (they are on the next frame):

stop();

import flash.display.Shape;

var temporaryDrawing:Shape = new Shape();
addChild(temporaryDrawing);
temporaryDrawing.graphics.lineStyle(3, 0x000000, 1);

var myDrawing:Shape = new Shape();
addChild(myDrawing);
myDrawing.graphics.lineStyle(3, 0x000000, 1);

var mouseHolding:Boolean=false;
var clickedX:Number;
var clickedY:Number;


stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);

function mDown(MouseEvent):void{
    mouseHolding = true;
    clickedX = mouseX;
    clickedY = mouseY;
}

function mUp(MouseEvent):void{
    mouseHolding = false;
    myDrawing.graphics.drawRect(clickedX, clickedY, mouseX-clickedX, mouseY-clickedY);
    nextFrame();
    clearTemp ()
}

stage.addEventListener(MouseEvent.MOUSE_MOVE, mMove);
function mMove(MouseEvent):void{
    if (mouseHolding){
        clearTemp ();
        temporaryDrawing.graphics.drawRect(clickedX, clickedY, mouseX-clickedX, mouseY-clickedY)

    }
}

function clearTemp():void{
    temporaryDrawing.graphics.clear();
    temporaryDrawing.graphics.lineStyle(3, 0x000000, 1)
}

Aucun commentaire:

Enregistrer un commentaire