View Full Version : Dragging and dropping
snilbert
08-07-2003, 08:27 PM
Hi,
I hope this problem isn't too complex for the newbies section...
Just say you've got a button, you push it and it places a random movieclip (1 selected out of possible 100) somewhere on the stage. I want to be then able to drag and drop that movieclip.
I've been advised already in these forums that one way to do it would be to have the movieclip placed (after pushing the button) indside another one on the stage, which has the drag and drop code attached to that.
This is fine, but I don't know how to go about writing the code to place the random clip inside the 'drag master' clip on the stage.
BTW, the random clips are all the same size (square) and I assume will be inserted in the master drag movieclip of exactly the same size, except invisible?
Appreciate any advice....thanks.
pixelwit
08-08-2003, 02:31 AM
Are you using FlashMX? What code are you using to place the clips? Are all the clips the same?
-PiXELWiT
http://www.pixelwit.com
snilbert
08-08-2003, 02:48 AM
Sorry, I should have specified: no, I"m using Flash 5.
This is code I've got attached to a button which selects random movie clips stored in the library and places them on an arbitrary place on the stage. It works in so far as what that is designed to do, but I don't know how to use the code attached to the button to place them inside clips already on the stage that I can then use to drag.
Here it is:
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;
}
}
And if this isn't clear enough, I attach the file I've been working with.
Thanks for responding.
pixelwit
08-08-2003, 12:13 PM
I'm not sure how to approach this.
There's a much better way to do what you're trying to do without creating 100 seperate clips in the library. So rather than give you a quick little fix I'll try to give you a better plan of attack for the big picture.
Your tile should be one clip rather than a hundred. Create a square button that looks like your tile without any letters. You can give it "over" and "down" states if you'd like but it's not necessary.
Select your button and turn it into a movieclip. Add two layers. In the first layer create a large dynamic text box the size of the button tile and assign it a variable name like "bigLetter". In the next layer create a smaller dynamic text box in the bottom right corner and give it a variable name of "littleNumber".
Select your button and give it these actions:on (press) {
startDrag(this);
}
on (release){
stopDrag();
}You now have 1 dynamic movieclip rather than 100 static ones which is much neater. Give your new clip a linkageID like "myTile".
Now the fun part. You seem to have a pretty good grasp of arrays and loops so hopefully this next bit won't lose you. The goal is to create an array of objects which will represent all of your tiles. Each object will hold a letter and point value for its respective tile.alphabet = " ABCDE";// fill in the rest;
points = [0, 1, 3, 3, 2, 1];// fill in the rest;
quantities = [2, 9, 2, 2, 4, 12];// fill in the rest;
allTiles = [];
function buildAllTiles() {
for (i=0; i<alphabet.length; i++) {
for (q=0; q<quantities[i]; q++) {
var obj = new Object();
obj.alph = alphabet.charAt(i);
obj.nmbr = points[i];
allTiles.push(obj);
}
}
}
buildAllTiles();Now hopefully this next bit of code is where everything comes together. You attach a blank tile clip then assign it a letter and a point value derived from the allTiles array:myDepth = 1;
function dealOutTiles(quant){
for (i=0; i<quant; i++) {
var nowTile = _root.attachMovie("myTile", "mc"+myDepth, myDepth++);
nowTile._x = i*50;
nowTile._y = 300;
var r = Math.floor(Math.random()*allTiles.length);
var nowObj = allTiles[r];
nowTile.bigLetter = nowObj.alph;
nowTile.littleNumber = nowObj.nmbr;
allTiles.splice(r, 1);
}
}
dealOutTiles(7);Hope this doesn't confuse you too much, but it really should be easier in the long run.
-PiXELWiT
http://www.pixelwit.com
snilbert
08-09-2003, 12:06 AM
PiXELWiT,
That is a magnificent response. When I get some time I'll have a go at it in the way that you said. It looks much more logical and neater, and I think I understand your method.
Thanks heaps!!:D
pixelwit
08-09-2003, 12:42 AM
Welcome.
-PiXELWiT
http://www.pixelwit.com
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.