PDA

View Full Version : moving a dynamically added MC on a hitTestPoint



okashira
July 2nd, 2008, 03:42 PM
Hi,
I was reading through one of the tutorials from kirupa found here http://www.kirupa.com/developer/flashcs3/movieclips_classes_AS3_pg2.htm

I got a lot out of it =]
but when i tried modifying the code to add a hitTest on it, it became "weird"
here is my .as file
package {
import flash.display.*;
import flash.events.*;
import flash.geom.Point;

public class Sq extends MovieClip {
var radians =0;
var speed =0;
var radius =0;
var randomValue=0;

public function Sq() {
speed =.01+.5*Math.random();
radius =2+10*Math.random();
randomValue= Math.random()*1;

this.x = Math.random()*550;
this.y = Math.random()*400;
this.rotation = Math.random()*360;
this.scaleX = this.scaleY = randomValue;
this.alpha = 1-randomValue;
this.addEventListener(Event.ENTER_FRAME, rotateSq);
}
function rotateSq(e:Event) {
if (this.hitTestPoint(mouseX,mouseY,true)) {
radians+=speed;
//trace("test works");
this.x+=Math.round(radius*Math.cos(radians));
this.y+=Math.round(radius*Math.sin(radians));
}else{
trace("nope");
}
}
}
}

the effect i'm trying to accomplish is when the user rolls over one of the MC's added
by the following function inside the .fla

function AddSq() {
for (var i:int =0; i<50; i++) {
var newSq:Sq = new Sq();
this.addChild(newSq);
}
}
AddSq();

is that the MC(which is a square) starts moving in a circle. but it seems that the hit area is in a different location and the square is in another.

is there something i'm doing wrong?
Thanks!

okashira
July 3rd, 2008, 10:28 AM
never mind, i figured it out. just had to use target and localToGlobal.


function rotateSq(e:Event) {
var whichSq:MovieClip = MovieClip(e.target);
var local:Point = new Point(whichSq.mouseX,whichSq.mouseY);
if (whichSq.hitTestPoint(stage.mouseX,stage.mouseY,tr ue)) {
radians+=speed;
//trace("test works");
whichSq.x+=Math.round(radius*Math.cos(radians));
whichSq.y+=Math.round(radius*Math.sin(radians));
}
}