Hello all
I am building a simple (ho ho) game. A number of mc’s are added dynamically to the stage using ‘attachMovie’ in a ‘for’ loop. The mc’s appear, in the exact place I want them. But how can I set them up so they can be dragged? I thought setting an event listener would do the trick - but no joy so far.
Thanks in advance
db
var mc:MovieClip=this.attachMovie("my_mc","my_mc",1)
mc.onPress=startDrag;
mc.onRelease=mc.onReleaseOutside=stopDrag;
it is as easy as above
my_mc is the linkage name
note you do not need () on the functions
Thanks for that - real progress. However (theres always one, isn’t there?!), once in my ‘for’ loop, only the last attached mc is draggable. BUT I remember reading this afternoon, either here or over at actionscript.org, how to tackle that. Will get back to this in the morning.
regards
db
post your code all should be draggable
for (i=0; i<5; i++) {
var mc:MovieClip = this.attachMovie("my_mc", "my_mc"+i, i);
mc.onPress = startDrag;
mc.onRelease = mc.onReleaseOutside=stopDrag;
mc._x = Math.floor(Math.random()*Stage.width-mc._width);
mc._y = Math.floor(Math.random()*Stage.height-mc._height);
}
see attached fla
Thanks very very much for that!! Soon as I saw your code, realised I’d put the ‘onPress’ and ‘onRelease’ statements in the wrong place - had it outside of the ‘for {}’ (but still within the function creating all the mc’s).
cheers
db
Very sorry to come back with this - spent all afternoon on it and feeling pretty dense. I’ve no got all my tiles laid out and dragging around the stage. I have another set of tiles, also added dynamically, and I want to perform a hitTest. Only having limited success. The comment in the code explains. Have read the ‘hitTest’ entry in the help, and scope - but not getting it.
Think I’ll go back to Letraset…
heres the relevant code:
function tilesSetUp() {
if(cutup.length<=10) {
wordlength = cutup.length * 64;
} else {
wordlength = 640;
}
for(var i = 0;i<cutup.length; i++) {//lays out top tiles
buildtop(i);
}
for(var bi = 0;bi<original.length; bi++) {//lays out static bottom tiles
buildbottom(bi);
}
}
//define draggable top tiles
function buildtop(i:Number):Void {
tnum = "tile"+i;
var t_mc:MovieClip = this.attachMovie("Tile",tnum,i+original.length);
t_mc._x = (i % 10) * 64 + (400 - wordlength/2);
t_mc._y = Math.floor(i/10) * 64 + 22;
t_mc.theText.text = cutup.charAt(i);//populates a text field from array
t_mc.onPress=startDrag;
t_mc.onRelease=t_mc.onReleaseOutside=stopDrag;
//start experiment to detect hit
t_mc.onRelease = function() {
this.stopDrag();
for (var i = 0; i < 11; i++) {
tileB = "chktile" + i;
trace("tileB "+tileB);
if (this.hitTest(tileB))
//this 'if' fails - but replace 'tileB' with any distinct instance name i.e. 'chktile8' and the trace works
{
trace("here");
}
}
}
}
//define static bottom tiles
function buildbottom(bi:Number):Void {
btnum = "chktile"+bi;
var h_mc:MovieClip = this.attachMovie("chk_Tile", btnum, bi);//_root.
h_mc._x = (bi % chkt_y) * 64 + 10;//_root[btnum]
h_mc._y = Math.floor(bi/chkt_y) * 64 + 464; //_root[btnum]
}
Thanks in advance
db
Hopefully solved. Built a much simpler model in a new .fla, got that working and ported it over.
Heres the fix:
for (var i = 0; i < 11; i++) {
var h_mc = "chktile" + i;
trace("h_mc "+h_mc);
if (t_mc.hitTest(_root[h_mc])) {
trace("here");
}
}
Next job - make the dragged tile ‘stick’ to the static tile, and return a variable! I could well be back…
cheers
db
firstly you have 2 onrelease
// first onrelase
t_mc.onRelease=t_mc.onReleaseOutside=stopDrag;
//start experiment to detect hit
//second onRelease
t_mc.onRelease = function() {
this.stopDrag();
for (var i = 0; i < 11; i++) {
tileB = "chktile" + i;
trace("tileB "+tileB);
if (this.hitTest(tileB))
//this 'if' fails - but replace 'tileB' with any distinct instance name i.e. 'chktile8' and the trace works
{
trace("here");
}
}
}
you also have some sort of scope problem
tileB = "chktile" + i;
needs to be soemthing like
tileB = _root[“chktile” + i];
hope this helps
elo
Yes - fixed the ‘tileB = “chktile” + i;’ line - thats when it started working! And have now got the drag tiles snapping to the static tiles and returning the contents of the text field. Need to sort out having x2 ‘onRelease’ still.
Thanks for all your help.
db