Search code examples
flashevent-handlingmouseeventflash-cs3adobe-flash-cs3

Flash CS3 startDrag on object in movieclip inside main fla


I've searched everywhere for someone else with this problem and can't seem to find a solution so here it is.

I found a tutorial on Flash CS3 in my textbook about a puzzle game. It's pretty straight forward, if you grab a piece it calls startDrag and stopDrag.

So I started making my own game and within it, I created a movieClip called puzzleGame. I have pretty much copied the code from the original tutorial to inside my puzzleGame movieClip but when I test it, i always get an error saying that startDrag and stopDrag is not a function. It has something to do with this movieclip being inside my main stage.

If I change the code to this.startDrag then it actually drags the entire frame. I think that for whatever reason, it's not drilling down to the actual object Im dragging and instead just seeing me touch the movieclip itself.

Does this make sense?

Here is my code for frame2.

stop();

import flash.utils.*;

var mySound:Sound = new correctSound(); 

var score:Number = 0;

var numClips:Number = 7;

var myClip = new Array(numClips);

myClip[0] = addChild(new a0());
myClip[1] = addChild(new a1());
myClip[2] = addChild(new a2());
myClip[3] = addChild(new a3());
myClip[4] = addChild(new a4());
myClip[5] = addChild(new a5());
myClip[6] = addChild(new a6());
//myClip[7] = addChild(new a7());
//myClip[8] = addChild(new a8());
//myClip[9] = addChild(new a9());

myClip[0].name = "piece0";
myClip[1].name = "piece1";
myClip[2].name = "piece2";
myClip[3].name = "piece3";
myClip[4].name = "piece4";
myClip[5].name = "piece5";
myClip[6].name = "piece6";
//myClip[7].name = "piece7";
//myClip[8].name = "piece8";
//myClip[9].name = "piece9";

var nph = new Array(numClips);

nph[0] = nph0_mc;
nph[1] = nph1_mc;
nph[2] = nph2_mc;
nph[3] = nph3_mc;
nph[4] = nph4_mc;
nph[5] = nph5_mc;
nph[6] = nph6_mc;
//nph[7] = nph7_mc;
//nph[8] = nph8_mc;
//nph[9] = nph9_mc;

var tpg = new Array(numClips);

tpg[0] = tpg0_mc;
tpg[1] = tpg1_mc;
tpg[2] = tpg2_mc;
tpg[3] = tpg3_mc;
tpg[4] = tpg4_mc;
tpg[5] = tpg5_mc;
tpg[6] = tpg6_mc;
//tpg[7] = tpg7_mc;
//tpg[8] = tpg8_mc;
//tpg[9] = tpg9_mc;

var x0 = myClip[0].x = Math.random()*400+50;
var y0 = myClip[0].y = Math.random()*50+50;
var x1 = myClip[1].x = Math.random()*400+50;
var y1 = myClip[1].y = Math.random()*50+50;
var x2 = myClip[2].x = Math.random()*400+50;
var y2 = myClip[2].y = Math.random()*50+50;
var x3 = myClip[3].x = Math.random()*400+50;
var y3 = myClip[3].y = Math.random()*50+50;
var x4 = myClip[4].x = Math.random()*400+50;
var y4 = myClip[4].y = Math.random()*50+50;
var x5 = myClip[5].x = Math.random()*400+50;
var y5 = myClip[5].y = Math.random()*50+50;
var x6 = myClip[6].x = Math.random()*400+50;
var y6 = myClip[6].y = Math.random()*50+50;
/*var x7 = myClip[7].x = Math.random()*400+50;
var y7 = myClip[7].y = Math.random()*50+50;
var x8 = myClip[8].x = Math.random()*400+50;
var y8 = myClip[8].y = Math.random()*50+50;
var x9 = myClip[9].x = Math.random()*400+50;
var y9 = myClip[9].y = Math.random()*50+50;*/

var j:Number;

for (var k:Number = 0; k < numClips; k++) {
    myClip[k].addEventListener("mouseDown", pieceMove);
    myClip[k].addEventListener("mouseUp", pieceMove);
}

function pieceMove(evt:Event):void {
    if (evt.type == "mouseDown") {
        //mySound.play();
        evt.target.startDrag();
    }
    else if (evt.type == "mouseUp") {
        //mySound.play();
        evt.target.stopDrag();

for (j = 0; j < numClips; j++) {
    if (evt.target.name == "piece" + j && 
        evt.target.hitTestObject(nph[j]) == true) {
            removeChild(myClip[j]);
            nph[j].alpha = 0;
            tpg[j].alpha = 100;
            score++;
        }
    else if (evt.target.name == "piece" + j) {
        evt.target.x = Math.random()*400+50;
        evt.target.y = Math.random()*50+50;
    }
}

scor.text = score.toString();
if (score == 10) {
    msgbox.text = "Congratulations !";
}

}
}


I'm using Flash CS3 because it's required by my course I'm taking so a suggestion to use a different version of flash is not helpful. Thanks.                                                                         

Solution

  • So what I did was instead of using an embedded movie clip, I just remade it inside my main timeline and it works fine. It's messier because of this but it works without issue. No idea why it didn't work the first way.