snilbert
08-05-2003, 01:26 AM
Hi,
I have a button on a stage which, when pushed, gives you 7 tiles with letters on them, for a game I'm trying to make which is loosely based on scrabble.
The tiles are of course, movieclips, which have been numbered in the library 1-100, shuffled into a random array, and then selected each time the button is pushed (in lots of 7, each new 7 replacing the preceding ones, at this stage).
I can get all this to happen fine, but what I want to now introduce is be able to drag and drop any of the seven pieces from their positions they are placed on the stage to anywhere else on the stage. I've given it a go by copying other people's stuff, but because I can't really understand it, I can't work out where I'm going wrong.
I'll show you the 3 pieces of code I have so far.
First, attached to a movieclip called "actions", I have this code, which sets up the array and randomises it ready to be dealt:
onClipEvent (load) {
pieces = new Array();
for(i=1;i<101;i++){
pieces.push([i]);
}
bag = new Array();
while (pieces.length>0) {
r = int(Math.random()*pieces.length);
bag.push(pieces[r]);
pieces.splice(r,1);
}
}
Then attached to the button, I've got the following script which selects the last 7 elements of the randomised list, and attaches the corresponding movieclips from the library, placing it on a specified spot on the stage.
on (release) {
trace(_root.actions.bag);
hitMe = _root.actions.bag.splice(0,7);
for(i=0;i<hitMe.length;i++) {
_root.attachMovie([hitMe[i]], "mc"+i, i);
_root["mc"+i]._x = i*50;
_root["mc"+i]._y = 300;
}
}
Now I can't work out what to do, to make those 7 letters moveable. Is it in fact impossible? The code I lifted from another source (a help book) was this (below), but I can't find a way to have the movieclip instances of the letters referred to in the drag'n'drop script. At the moment, I have it attached to another token movieclip "actions2", but it is ineffective.
// start with nothing dragging
onClipEvent (enterFrame) {
dragPiece = 0;
}
// start drag
onClipEvent (mouseDown) {
// get current location
x = _root._xmouse;
y = _root._ymouse;
// find which, if any, part the cursor is over
for(i=1;i<=7;i++) {
if (_root["mc"+i].hitTest(x,y, true)) {
// set to drag this part, remember offset
dragPiece = i;
offsetx = _root["mc"+i]._x - x;
offsety = _root["mc"+i]._y - y;
break;
}
}
}
// end drag
onClipEvent (mouseUp) {
dragPiece = 0;
}
// if dragging, set new position
onClipEvent (enterFrame) {
if (dragPiece > 0) {
_root["mc"+dragPiece]._x = _root._xmouse + offsetx;
_root["mc"+dragPiece]._y = _root._ymouse + offsety;
}
}
And I can't attach this to the code attached to the button because you can't have onClipEvent on a button.
I can attach the file I've been working with if you require it.
Hope this wasn't too confusing. Thanks in anticipation.
I have a button on a stage which, when pushed, gives you 7 tiles with letters on them, for a game I'm trying to make which is loosely based on scrabble.
The tiles are of course, movieclips, which have been numbered in the library 1-100, shuffled into a random array, and then selected each time the button is pushed (in lots of 7, each new 7 replacing the preceding ones, at this stage).
I can get all this to happen fine, but what I want to now introduce is be able to drag and drop any of the seven pieces from their positions they are placed on the stage to anywhere else on the stage. I've given it a go by copying other people's stuff, but because I can't really understand it, I can't work out where I'm going wrong.
I'll show you the 3 pieces of code I have so far.
First, attached to a movieclip called "actions", I have this code, which sets up the array and randomises it ready to be dealt:
onClipEvent (load) {
pieces = new Array();
for(i=1;i<101;i++){
pieces.push([i]);
}
bag = new Array();
while (pieces.length>0) {
r = int(Math.random()*pieces.length);
bag.push(pieces[r]);
pieces.splice(r,1);
}
}
Then attached to the button, I've got the following script which selects the last 7 elements of the randomised list, and attaches the corresponding movieclips from the library, placing it on a specified spot on the stage.
on (release) {
trace(_root.actions.bag);
hitMe = _root.actions.bag.splice(0,7);
for(i=0;i<hitMe.length;i++) {
_root.attachMovie([hitMe[i]], "mc"+i, i);
_root["mc"+i]._x = i*50;
_root["mc"+i]._y = 300;
}
}
Now I can't work out what to do, to make those 7 letters moveable. Is it in fact impossible? The code I lifted from another source (a help book) was this (below), but I can't find a way to have the movieclip instances of the letters referred to in the drag'n'drop script. At the moment, I have it attached to another token movieclip "actions2", but it is ineffective.
// start with nothing dragging
onClipEvent (enterFrame) {
dragPiece = 0;
}
// start drag
onClipEvent (mouseDown) {
// get current location
x = _root._xmouse;
y = _root._ymouse;
// find which, if any, part the cursor is over
for(i=1;i<=7;i++) {
if (_root["mc"+i].hitTest(x,y, true)) {
// set to drag this part, remember offset
dragPiece = i;
offsetx = _root["mc"+i]._x - x;
offsety = _root["mc"+i]._y - y;
break;
}
}
}
// end drag
onClipEvent (mouseUp) {
dragPiece = 0;
}
// if dragging, set new position
onClipEvent (enterFrame) {
if (dragPiece > 0) {
_root["mc"+dragPiece]._x = _root._xmouse + offsetx;
_root["mc"+dragPiece]._y = _root._ymouse + offsety;
}
}
And I can't attach this to the code attached to the button because you can't have onClipEvent on a button.
I can attach the file I've been working with if you require it.
Hope this wasn't too confusing. Thanks in anticipation.