-
1. Re: I need some help here...
Ned Murphy Dec 30, 2010 8:40 AM (in response to d0brin)What are the arguments in the constructor function?
-> (this, "photo", 1, 2);
-
2. Re: I need some help here...
d0brin Dec 30, 2010 9:51 AM (in response to Ned Murphy)this is at how many pieces the "photo"-bitMap is being separated
-
3. Re: I need some help here...
Ned Murphy Dec 30, 2010 10:23 AM (in response to d0brin)Your response doesn't answer the question. There are 4 arguments in that constructor... what are they defining?
-
4. Re: I need some help here...
d0brin Dec 30, 2010 10:49 AM (in response to Ned Murphy)The first one the IMPORT imports this:
import mx.events.EventDispatcher;
import mx.utils.Delegate;
class com.communitymx.CMXJigsawPuzzle {
// PROPERTIES
private var _finalPositionX:Number;
private var _finalPositionY:Number;
private var _pieces:Array;
private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
// CONSTRUCTOR
public function CMXJigsawPuzzle(target:MovieClip, picture:String, rows:Number, cols:Number, width:Number, height:Number) {
buildPuzzle(target, picture, rows, cols, width, height);
}
// METHODS
// Build Puzzle
private function buildPuzzle(target:MovieClip, picture:String, rows:Number, cols:Number, width:Number, height:Number):Void {
// Declare local variables used in this method
var piece:MovieClip;
var pieceWidth:Number;
var pieceHeight:Number;
var positionX:Number;
var positionY:Number;
var counter:Number;
// Initialize class instance as an event
// dispatcher
EventDispatcher.initialize(this);
// Attach picture from Library
piece = target.attachMovie(picture, "mcPiece0", target.getNextHighestDepth());
// If picture is small enough to fit and if
// picture falls within Stage boundaries,
// add it to pieces array; otherwise remove
// piece and issue warning
if (puzzleFits(piece, width, height)) {
if (puzzleWithinBounds(piece, width, height)) {
_pieces = new Array();
_pieces.push(piece);
} else {
piece.removeMovieClip();
trace("CMXJigsawPuzzle error: Puzzle dimensions must fall within boundaries of Stage.");
return;
}
} else {
piece.removeMovieClip();
trace("CMXJigsawPuzzle error: Puzzle dimensions must be smaller than specified width and height. If none specified, dimensions must be smaller than Stage.");
return;
}
// Initialize variables and properties
positionX = 0;
positionY = 0;
pieceWidth = piece._width / cols;
pieceHeight = piece._height / rows;
// If target has a parent clip, use zero as
// final position; otherwise, center final
// position to Stage
if (target._parent) {
_finalPositionX = 0;
_finalPositionY = 0;
} else {
_finalPositionX = (Stage.width / 2) - (piece._width / 2);
_finalPositionY = (Stage.height / 2) - (piece._height / 2);
}
// Draw and set mask on first piece
drawAndApplyMask(piece, positionX, positionY, pieceWidth, pieceHeight);
// Randomly position first piece and assign
// its event handlers
randomizePosition(piece, width, height);
assignEventHandlers(piece);
// Draw a container for the completed puzzle
drawPuzzleContainer(target, _finalPositionX, _finalPositionY, piece._width, piece._height);
// Place container behind first piece
piece.swapDepths(target.mcContainer);
// Attach, mask, randomly position, and assign
// event handlers to the other pieces
for (counter = 1; counter < (cols * rows); counter++) {
piece = target.attachMovie(picture, "mcPiece" + counter, target.getNextHighestDepth());
_pieces.push(piece);
if ((counter % cols) == 0) {
positionX = 0;
positionY += pieceHeight;
} else {
positionX += pieceWidth;
}
drawAndApplyMask(piece, positionX, positionY, pieceWidth, pieceHeight);
randomizePosition(piece, width, height);
assignEventHandlers(piece);
}
}
// Puzzle Fits
private function puzzleFits(mc:MovieClip, w:Number, h:Number):Boolean {
// Use specified width and height or Stage
// width and height to determine fit
return (mc._width < (w ? w : Stage.width)) && (mc._height < (h ? h : Stage.height));
}
// Puzzle Within Bounds
private function puzzleWithinBounds(mc:MovieClip, w:Number, h:Number):Boolean {
// If puzzle is in main timeline, it is
// guaranteed to be in bounds
if (!mc._parent._parent) {
return true;
}
// Otherwise, determine piece's coordinates
// relative to Stage and account for
// possible area required by jumbling
var point:Object = new Object();
point.x = 0;
point.y = 0;
mc.localToGlobal(point);
var bufferX:Number = 0;
var bufferY:Number = 0;
if (w && h) {
bufferX = (w / 2) - (mc._width / 2);
bufferY = (h / 2) - (mc._height / 2);
}
return !((point.x - bufferX) < 0 || (point.x + mc._width + bufferX) > Stage.width || (point.y - bufferY) < 0 || (point.y + mc._height + bufferY) > Stage.height);
}
// Draw Puzzle Container
private function drawPuzzleContainer(mc:MovieClip, x:Number, y:Number, w:Number, h:Number):Void {
// Declare local variable reference for container
var container:MovieClip = mc.createEmptyMovieClip("mcContainer", mc.getNextHighestDepth());
// Use Drawing API to draw a rectangle
container.lineStyle(1);
}
// Draw and Apply Mask
private function drawAndApplyMask(mc:MovieClip, x:Number, y:Number, w:Number, h:Number):Void {
// Declare local variable reference for mask
var mask:MovieClip = mc.createEmptyMovieClip("mcMask", mc.getNextHighestDepth());
// Use Drawing API to draw a rectangle
mask.beginFill(0xFF0000);
drawRect(mask, x, y, w, h);
// Apply mask to piece
mc.setMask(mask);
}
// Draw Rect
private function drawRect(mc:MovieClip, x:Number, y:Number, w:Number, h:Number):Void {
// Basic Drawing API
mc.moveTo(x, y);
mc.lineTo(x + w, y);
mc.lineTo(x + w, y + h);
mc.lineTo(x, y + h);
mc.lineTo(x, y);
}
// Randomize Position
private function randomizePosition(mc:MovieClip, w:Number, h:Number):Void {
// Declare point object with x and y properties
var point:Object = new Object();
// Generate random number between zero and
// either specified width/height or Stage
// width/height
point.x = Math.random() * ((w ? w : Stage.width) - mc.mcMask.getBounds(mc).xMax);
point.y = Math.random() * ((h ? h : Stage.height) - mc.mcMask.getBounds(mc).yMax);
// If width/height specified ...
if (w && h) {
// offset positioning based on specified
// width and height, or ...
if (mc._parent._parent) {
point.x -= ((w / 2) - (mc._width / 2));
point.y -= ((h / 2) - (mc._height / 2));
} else {
// offset based on Stage width and height
point.x += ((Stage.width / 2) - (w / 2));
point.y += ((Stage.height / 2) - (h / 2));
}
} else {
// ... otherwise compensate position
// if not in the main timeline
if (mc._parent._parent) {
mc.globalToLocal(point);
}
}
mc._x = point.x;
mc._y = point.y;
}
// Assign Event Handlers
private function assignEventHandlers(mc:MovieClip):Void {
// When piece is pressed, lift it above the
// other pieces and start dragging
mc.onPress = function() {
this.swapDepths(this._parent.getNextHighestDepth());
this.startDrag();
};
// When piece is released, stop dragging.
// Snap piece to final position if close
// enough, then check if puzzle is complete
mc.onRelease = Delegate.create(this, function () {
mc.stopDrag();
if (distanceBetween(mc._x, mc._y, _finalPositionX, _finalPositionY) < 6) {
mc._x = _finalPositionX;
mc._y = _finalPositionY;
}
if (puzzleIsComplete()) {
dispatchEvent({type:"onComplete", target:this});
}
});
}
// Distance Between
private function distanceBetween(x1:Number, y1:Number, x2:Number, y2:Number):Number {
// Determine distance between two points with
// Pythagorean theorem
var x:Number = x1 - x2;
var y:Number = y1 - y2;
return Math.sqrt((x * x) + (y * y));
}
// Puzzle Is Complete
private function puzzleIsComplete():Boolean {
// Loop through each piece to see if its
// position matches the final position
var counter:Number;
var total:Number = _pieces.length;
for (counter = 0; counter < total; counter++) {
if (_pieces[counter]._x != _finalPositionX) {
return false;
}
}
return true;
}
}
(This builds up a puzzle from a regular bitMAP. You are able to move the pieces and when you arrange a function is complete.)This defines the on complete what function to be executed:var listener:Object = new Object();
listener.onComplete = function() {
gotoAndStop(2);
};
puzzle.addEventListener("onComplete", listener);
This as i said defines at how many pieces the bitmap is broken:
var puzzle:CMXJigsawPuzzle = new CMXJigsawPuzzle(this, "photo", 1, 2);
-
5. Re: I need some help here...
Ned Murphy Dec 30, 2010 11:19 AM (in response to d0brin)No, I was asking about all of the arguments, not just two of them. The code you showed answered the question for me although I only needed one line of it...
CMXJigsawPuzzle(target:MovieClip, picture:String, rows:Number, cols:Number, width:Number, height:Number)
The first argument ("this" in your code) is the target that the puzzle is loaded into. When you use "this", you are loading it into whatever timeline that code is in, and dynamically created content doesn't have a home fixed into a frame of the timeline it gets added to, so it will persist throughout the timeline. You need to give it a home that is fixed to a frame in the timeline.
So if you manually create an empty movieclip in whatever frame the puzzle is supposed to be in and give it an instance name, say "puzzleHolder", then if you define your puzzle using....
var puzzle:CMXJigsawPuzzle = new CMXJigsawPuzzle(puzzleHolder, "photo", 1, 2);
when you move to frame 2, that puzzle SHOULD no longer be present. (Stressing SHOULD because things aren't always what they should be)



