View Full Version : jigsaw puzzle
van_doodle
05-24-2007, 12:18 PM
Hi,
I'm trying to create a jigsaw puzzle,
I need code that allows the user to drag and drop, and snap into place if it is correct, or snap back into if its original position if it is incorrect.
Any ideas?
Thank you :)
sarumitai
05-24-2007, 02:20 PM
target_mc.onPress = startDrag;
target_mc.onRelease = function () {
this.stopDrag();
if (this.hitTest(Goal_mc)) {
this._x = 50;
this._y = 50;
this.enabled = false;
this.filters = [gf];
}
}
import flash.filters.GlowFilter;
var gf:GlowFilter = new GlowFilter(0x339900, 100, 3, 3, 5, 3, false, false);
Target_mc is simply the MC you want to drag around.
onPress = what happens when the mouse is PRESSED.
onRelease is what happens when the mouse is RELEASED.
Be careful of upper lower case.
this.stopDrag means "this MC that we are in" which is the target_mc.
IF the hitTest (overlap, touching) is true between this (target_mc) and another mc (here the Goal_mc), then do what is in the {}.
move the MC's X coordinate to 50
move the MC's Y coordinate to 50 (or wherever you want it to snap to)
this.enabled = false means they can't drag it anymore.
this.filters, along with the two lines at the bottom (which should be at the top) simply make it glow a bit once it has touched its target and then jumped into place. It's not needed, but it does make it clear it has been locked into place correctly.
sarumitai
05-24-2007, 02:25 PM
But for a jigsaw you probably wouldn't want it to glow.
Also, you would do this with each piece of the puzzle. Each piece would be a Movie Clip and you would write this script for each (minus the glow parts).
4 puzzle pieces = puzzlePiece1, puzzlePiece2, etc.
puzzlePiece1.onPress = startDrag;
There is a way to do this via a FOR loop, but I just learned it and couldn't explain it.
http://www.actionscript.org/forums/showthread.php3?t=136767
van_doodle
05-25-2007, 08:33 AM
once two movie clips(b1 and t1) touch(hitTest), i need the global to = 1;
but what do i need to add to code
b1.onPress = startDrag;
b1.onRelease = function () {
this. stopDrag ();
if (b1.hitTest (t1)) {
this._x = 266.2;
this._y = 489.1;
this. enabled = false;
}
}
but what and where do i add the text in?
sarumitai
05-31-2007, 01:45 PM
b1.onPress = startDrag;
b1.onRelease = function () {
this. stopDrag ();
if (b1.hitTest (t1)) {
this._x = 266.2;
this._y = 489.1;
global++; // if global is set to zero, now it's one
global = 1; // or global is now one. Either or, not both.
this. enabled = false;
}
}
Assuming global is a variable, just make it equal whatever in the IF condition. If they are touching via the hitTest, it will do everything in the {}. So here it moves the MC to 266.2 and 489.1, then it locks it, finally it adds one to the variable. Or it could just equal 1.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.