PDA

View Full Version : drag and drop game with a problem


lucymadddison
11-24-2003, 05:29 PM
Help! I've created a drag and drop game in which 15 MC's are placed on a figure , and when correctly placed should go to a payoff frame. The drag and drop works fine, but I can't get the code to check correctly that all pieces are in place. The pieces are called 'bit0' to 'bit14' and I have been trying to make an array to check all pieces are placed. I include the code below....
onClipEvent (load) {
//Create an array to keep track of which ones found
found = [];
for (x=0; x<15; x++) {
found[x] = false;
}
}

onClipEvent (mouseDown) {
// get mouse location
mx = _root._xmouse;
my = _root._ymouse;
// loop through pieces looking for hit
for (x=0; x<15; x++) {
piece = _root["bit"+x];
// see whether the piece is under the cursor
// and if it has not yet been placed
if (piece.hitTest(mx, my) and (piece._alpha<100)) {
// this is the piece clicked
piece.startDrag();
// break out of all loops

x=15;

break;
}
}
}

onClipEvent (mouseUp) {

// stop piece from moving
stopDrag();
// get distance from center of piece
// to center of outline
dx = _root.outline._x-piece._x;
dy = _root.outline._y-piece._y;
dist = Math.sqrt(dx*dx+dy*dy);

// if close enough, then set it in place
if (dist<200) {
piece._x = _root.outline._x;
piece._y = _root.outline._y;

// set to full color
piece._alpha = 100;

// play sound on release
piece = new Sound();
piece.attachSound("whirp");
piece.start();
}
for (x=0; x<15; x++) {
if(piece._alpha = 100) {
// record that this bit was placed
found[x] = true;

break;
}
}


//see if all pieces placed

gameover = true;
for (x=0; x<15; x++) {
if (found[x] == false) {
gameover = false;
}
}
//go to anim if all found
if (gameover) {
_root.gotoAndPlay("end");
}
}




Please use the proper code syntax to format the code.
Read this page (http://www.actionscript.org/forums/misc.php3?action=bbcode#buttons) to find out more about the tag information

bassouma86
11-24-2003, 08:13 PM
Hey there

I think there's another way to do it (less complex).

when you insert a piece check if it's put in place & increase pieces in place variable

when that variable == 15 then YOU WIN

btw : if that is not much to ask, could you explain a bit more how do you know that a piece is in place ??

lucymadddison
11-25-2003, 06:03 AM
bassouma86 thanks for your reply.
The variable thing sounds good, I am relatively new to actionscript, would you be able to write what I need?
As to the positioning..
I created my 15 MCs , then I realigned them to the base figure (mc outline)
so the center point was the same for all clips.
This game is cobbled together from two games in "FlashMX actionscript for fun and games", so my actionscript knowlege might appear more than it is!

bassouma86
11-25-2003, 07:49 PM
well i still can't figure out how you're gonna know that a piece is in place !!

you're making some sort of a jigsaw puzzle right ??
cos the thing that i can't understand is how do you know that the piece is in it's place ??

if i set the piece to a place then how do i know that ??
do you get the coordinates of the piece & check it with what it should be ??

if you can get back 2 me on this it would be gr8

AS for the code i think it would be no problem but it's not ready so you'll have to put up with me a little

thx

bassouma86
11-26-2003, 08:05 AM
// the loop is between 0 & 15 (16 Pieces)
for (LoopOnYourPieces = 0; LoopOnYourPieces < 16; LoopOnYourPieces++)
{
// Insert code here which checks whether your piece is in place or not
}

if (NumOfCountries == 16)
{
// Insert code here which happens when you win
}


that should be it ;)

// mistake corrected ThanX 2 CyanBlue

lucymadddison
11-27-2003, 06:40 AM
thanks I'll try that. I did try and post up my game, but I had trouble uploading it
as a .zip containing the fla. I'll let you know how I get on with the new code

bassouma86
11-27-2003, 08:32 AM
i think we can only upload .fla to the forums but i'm not sure about that.

I guess it's worth a try ;)

CyanBlue
11-27-2003, 05:24 PM
Howdy... ;)

Check out the FAQ page to read about the file attachment...
http://www.actionscript.org/forums/misc.php3?s=&action=faq&page=3#attachments

@bassouma86if (NumOfCountries = 16)
// should be
if (NumOfCountries == 16)

lucymadddison
11-27-2003, 05:54 PM
hello
I have tried inserting the code but it is going to the end frame after the first piece is placed. The problem I have had all along is how to check if the piece is in place.
I enclose the zipped fla (flas can not be posted directly and it is also too big.
I don't undersatnd how the "NumofCountries" works is that just random words?
..Sorry tried to enclose the .fla but can't, heres the code or terrible mess...
onClipEvent (mouseDown) {
// get mouse location
mx = _root._xmouse;
my = _root._ymouse;
// loop through pieces looking for hit
for (x=0; x<15; x++) {
piece = _root["bit"+x];
// see whether the piece is under the cursor
// and if it has not yet been placed
if (piece.hitTest(mx, my) and (piece._alpha<100)) {
// this is the piece clicked
piece.startDrag();

// break out of all loops
x=15;

break;
}
}
}

onClipEvent (mouseUp) {
// stop piece from moving
stopDrag();
// get distance from center of piece
// to center of outline
dx = _root.outline._x-piece._x;
dy = _root.outline._y-piece._y;
dist = Math.sqrt(dx*dx+dy*dy);

for (LoopOnYourPieces = 0; LoopOnYourPieces < 15; LoopOnYourPieces++)
// if close enough, then set it in place
if (dist<200) {
piece._x = _root.outline._x;
piece._y = _root.outline._y;

// set to full color
piece._alpha = 100;

// play sound on release
piece = new Sound();
piece.attachSound("whirp");
piece.start();
}
}

onClipEvent (mouseUp) {
if (NumOfCountries = 15)
// Insert code here which happens when you win
{ _root.gotoAndPlay("end");
}
}




Please use the proper code syntax to format the code.
Read this page (http://www.actionscript.org/forums/misc.php3?action=bbcode#buttons) to find out more about the tag information

lucymadddison
11-27-2003, 05:57 PM
I tried it as NumOfCountries == 15 it doesnt work something else is wrong...

CyanBlue
11-27-2003, 06:06 PM
You are not using the comment line correctly...if (NumOfCountries = 15)
// Insert code here which happens when you win
{
_root.gotoAndPlay("end");
}You cannot have comment in between the if condition line and the brackets...
You also need to use '==' rather than the '=' to check the condition...
Read the Flash manual for more information... ;)

This block will be correct one in syntax wise...if (NumOfCountries == 15)
{
// Insert code here which happens when you win
_root.gotoAndPlay("end");
}I don't know how hard it is to use the AS tag, but let me tell you for the last time... Please use the AS tag so that your code will not confuse my eyes... I don't want to go blind any time soon... :(

[useastags]

bassouma86
11-27-2003, 06:54 PM
:o

quite a common mistake 4 all VB programmers switching to C, Java & AS like codes.

I thought i got past that but it seems like i'm not :D

thx again 4 ur help

CyanBlue
11-27-2003, 10:42 PM
Oh, come on... Give youself some break...
You didn't kill anybody... Don't worry too much about it... :)

lucymadddison
11-28-2003, 07:20 AM
sorry didn't know about the As format I''ll try it now...
trace("This is a test");