PDA

View Full Version : Need help targeting movie clip instances


tryin_to_learn
06-01-2005, 06:27 PM
I'm working on a scrambled word game. I've written 4 functions, one to randomize, one to place the instances of the letters in the scrambled word; one to place target movie clips(will not show on the stage); and the forth places the lines to drag the scrambled letters to. I'm trying to figure out how to do a hitTest so I can indicate if someone drags a letter into the correct position. I've got an if statement at the very bottom of the code below that finds the matching pairs. However, it's working by using charAt, which means that we're comparing strings. How do I target the clips that are holding those strings? Here's the code:



function rand() {
return random(3)-1;
}

words_arr = new Array('POND', 'BUGS', 'ANIMALS', 'PLANTS', 'SWAMP', 'INSECTS', 'FROGS', 'ANTS', 'MOSQUITOS');
randomIncr = Math.floor(Math.random() * words_arr.length);
randomWord = String(words_arr[randomIncr]);
wordCorrectOrder = randomWord.split("").join("");
shuffledWord=randomWord.split("").sort(rand).join("");

function placeLines () {
for(var i = 0; i < shuffledWord.length; i++){
this.attachMovie("mcTargetSymbol", "line_mc" + i, this.getNextHighestDepth());
targetLine = this["line_mc"+i];
}
}

function placeTargetClips () {
for(var i = 0; i < shuffledWord.length; i++){
this.attachMovie("targetLetterSY","targetWord_" + i, this.getNextHighestDepth());
targetLetterClip = this["targetWord_" + i];

targetLetterClip.target_letter.text = wordCorrectOrder.charAt(i);
}
}

function placeScrambledLetters () {
for(var i = 0; i < shuffledWord.length; i++){
this.attachMovie(shuffledWord.charAt(i) + "_letterSY","letter_" + i, this.getNextHighestDepth());
scrambledLetter = this["letter_"+i];

scrambledLetter.onPress = function():Void {
startDrag(this);
correct_mc._visible = false;
}
scrambledLetter.onRelease = function():Void {
stopDrag();
}
}

}


if (shuffledWord.charAt(i) == WordCorrectOrder.charAt(i)) {

a = shuffledWord.charAt(i);
b = wordCorrectOrder.charAt(i);
trace(a);
trace(b);
if (a.hitTest(b)) {
trace("It hit a target");

// This doesn't work because a & b are strings. How do I target the clips that hold a & b?
}
}


placeLines();
placeTargetClips ();
placeScrambledLetters ();

snapple
06-01-2005, 07:59 PM
Just an ideaL: When you attachMovie, why don't you use the word the user is trying to sort out, as the movieClips instance name, that to me would make like a lot easier.

That way you could refer to the movieClip directly in the hitTest. i think there are a few design issues with your code.

tryin_to_learn
06-02-2005, 12:53 AM
I don't think that would work -- at least not the way I've designed it. The movie clips are individual letters and the word, as well as the order of the letters, are chosen at random. So if the word chosen is "pond", there are 4 different movie clip instances put up on the stage -- one for p, one for o, etc. Unless I'm misunderstanding what you're saying, I'm not sure how I could name those instances for the word. Can you explain?

There may well be design issues. I'm really stumbling through this trying to learn as I go. Anyone have a better idea of how to do it?

tryin_to_learn
06-02-2005, 02:28 AM
Snapple, I've been thinking more about your suggestion. Are you saying I should rename each letter instance so the actual letter appears in the instance name? That sort of makes sense. I came up with the code below (new stuff is set off by comments) that will accomplish that. Now maybe if I can compare the last letter of each instance of ("letter_" + sScrambledLetter) with the last letter of each instance of ("targetWord_" + sletter) and find the matches, I could get the hitTest to work. I'm not sure if I'm getting warmer or more convoluted here! LOL Also, I'm still not sure how I would word the hitTest. Here's the new code if anyone can help:


function rand() {
return random(3)-1;
}

words_arr = new Array('POND', 'BUGS', 'ANIMALS', 'PLANTS', 'SWAMP', 'INSECTS', 'FROGS', 'ANTS', 'MOSQUITOS');

randomIncr=Math.floor(Math.random() * words_arr.length);
randomWord = String(words_arr[randomIncr]);
wordCorrectOrder = randomWord.split("").join("");
shuffledWord=randomWord.split("").sort(rand).join("");

function placeLines () {
for(var i = 0; i < shuffledWord.length; i++){
this.attachMovie("mcTargetSymbol", "line_mc" + i, this.getNextHighestDepth(), {_y:300, _x:470+(45*i) - (shuffledWord.length * 25)});
targetLine = this["line_mc"+i];
}
}

function placeTargetClips () {
for(var i = 0; i < shuffledWord.length; i++){
this.attachMovie("targetLetterSY","targetWord_" + i, this.getNextHighestDepth(), {_y:285, _x:470+(45*i) - (shuffledWord.length * 25)});
targetLetterClip = this["targetWord_" + i];
targetLetterClip.target_letter.text = wordCorrectOrder.charAt(i);

//New part starts here:
var sletter:String = wordCorrectOrder.charAt(i);
trace("targetWord_" + sletter);
}
}

function placeScrambledLetters () {
for(var i = 0; i < shuffledWord.length; i++){
this.attachMovie(shuffledWord.charAt(i) + "_letterSY","letter_" + i, this.getNextHighestDepth(), {_y:175, _x:470+(45*i) - (shuffledWord.length * 25)});
scrambledLetter = this["letter_"+i];

//New part starts here:
var sScrambledLetter:String = shuffledWord.charAt(i);
trace("letter_" + sScrambledLetter);

scrambledLetter.onPress = function():Void {
startDrag(this);
correct_mc._visible = false;
}
scrambledLetter.onRelease = function():Void {
stopDrag();
}
}
}

if (shuffledWord.charAt(i) == wordCorrectOrder.charAt(i)) {
a = shuffledWord.charAt(i);
b = wordCorrectOrder.charAt(i);
//This is the part that doesn't work because a & b are strings. I'm trying to figure out what to substitute for a and b so I can perform the hitTest:
if (a.hitTest(b)) {
trace("It hit a target");
}
}

placeLines();
placeTargetClips ();
placeScrambledLetters ();

snapple
06-02-2005, 06:26 AM
Apologies, i really on have a matter of seconds to look at this. Yes, that is what i meant, make the letter, the instance name of the mc, that way you could get the character, perform any tests you want on it (to see if it is in the right place) and perform the physical hitTest on it too.


this.attachMovie("targetLetterSY","targetWord_" + i, this.getNextHighestDepth(), {_y:285, _x:470+(45*i) - (shuffledWord.length * 25)});


would be


this.attachMovie("targetLetterSY", currentLetter + "_mc", this.getNextHighestDepth(), {_y:285, _x:470+(45*i) - (shuffledWord.length * 25)});


I just think, generally, you'll find it a lot easier, if you make all of the individual movieClips have an instance name of the letter, have an empty textbox inside that mc, fill it with the letter (which would also be the instance name) - and it all suddenly becomes a lot easier to check the letter, through:


trace( this._name )


and perform a hitTest too.

tryin_to_learn
06-02-2005, 06:47 PM
Thanks for your help. I think I understand part of what you're saying. Would you call the scrambled letter mcs and also the target (correct order) letters the same name? Like for pond, the scrambled and target mcs might be called:

O_mc
P_mc
D_mc
N_mc

P_mc
O_mc
N_mc
D_mc

If I have them set up like that, then how would I do the hitTest?