PDA

View Full Version : Drawing API



PBKChris
November 25th, 2008, 06:48 PM
I have added a little to some code that another user had written for drawing a square, but I have some question. As of now when the user clicks and drags on the stage you draw a square. I have it so you can change the color and stroke color of the square. Here is what I want to do. Instead of the user clicking and dragging on the stage I want them to first click a button that represents the object to be drawn and then that activates the code to draw the rectangle as the click and drag on the stage. Here is the code:

ActionScript Code:

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Stage;
import fl.controls.ColorPicker;
import fl.events.ColorPickerEvent;

var mouseIniX:Number;
var mouseIniY:Number;

var chosenColour:uint = 0;
var chosenColour2:uint = 0;

// This will hold a reference to the current created object
// on the onMouseDownEvent and is going to be used to create shape
var mcRef:MovieClip;

fCP.addEventListener(ColorPickerEvent.CHANGE, changeHandler);
sCP.addEventListener(ColorPickerEvent.CHANGE, changeHandler2);

stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);

function changeHandler(event:*):void {
chosenColour = event.currentTarget.selectedColor;
}

function changeHandler2(event:*):void {
chosenColour2 = event.currentTarget.selectedColor;
}

// Functions to handle object dragging
function startDraging(e:MouseEvent):void
{
e.target.startDrag();
}

function stopDraging(e:MouseEvent):void
{
e.target.stopDrag();
}


function onMouseDownHandler(e:MouseEvent) {
if ( e.target is Stage )
{
mouseIniX = mouseX;
mouseIniY = mouseY;

// Create our new square and add some listeners to it to enabled dragging
var mc:MovieClip = new MovieClip();
mc.buttonMode = true;
mc.addEventListener(MouseEvent.MOUSE_DOWN, startDraging);
mc.addEventListener(MouseEvent.MOUSE_UP, stopDraging);
addChild( mc );
// Hold a reference so we can update its graphics using the drawing API
mcRef = mc;

// DRAW!
stage.addEventListener(Event.ENTER_FRAME, square);
}
}

function onMouseUpHandler(e:MouseEvent) {
stage.removeEventListener(Event.ENTER_FRAME, square);
}

function square(e:Event):void
{
mcRef.graphics.clear();
mcRef.graphics.lineStyle(1, chosenColour2, 1.0);
mcRef.graphics.beginFill(chosenColour, 1.0);
mcRef.graphics.drawRect(mouseIniX, mouseIniY, mouseX - mouseIniX, mouseY - mouseIniY);
mcRef.graphics.endFill();
}



I am just frustrated because I have tried changing the code up and can not seem to figure it out. Please, can someone help me. Here is a link to how it works as well. The square button as of now does not work, which is what I am trying to figure out.

http://www.pbk.com/draw/fromMario3.html