onsdag 14. oktober 2009

shooting

I started by creating a new child, "the bullet" i created it innside a click function. the problem whit this was that i did not have eany access to it outside the function "i need it in update so that i can get the bullets to fly." so i moved decleration of bullet to the global variable area. that made me able to access bullet outside. next problem was to get it moving the same speed in the right derection, after much testing and failing i turned to math, and found that the "unit circle" was what i needed to find out. so i tested sine and cosine to find the "unit circle" x and y cordinates reletive to the mouse and the charecter. but got wild nubers that i was unable to use, or figure out. so i went over to Pytagoras to find the "unit circle" x and y, this worked perfectly.
code i ended up using:

var a:int = _karakter.y - mouseY;
var b:int = _karakter.x - mouseX;

var total:Number = Math.sqrt(a * a + b * b);
var ferdigx:Number = b / total;
var ferdigy:Number = a / total;

bullet.x -= (ferdigx * 6);
bullet.y -= (ferdigy * 6);

Ingen kommentarer:

Legg inn en kommentar