Flash tutorial make multiple turrets or eyes rotate towards mouse or objects



Learn a very efficient method of rotating multiple objects on stage. We are using Flash Actionsctipt 3.0 in this lesson which is suitable for online game programming and cool user interactive animations.

// Actionscript 3.0 Written By Adam Khoury
// Script for rotating mass objects to face a target
var eyeArray:Array = [eye1, eye2, eye3, eye4, eye5, eye6];
stage.addEventListener(MouseEvent.MOUSE_MOVE, allEyesOnMe);
function allEyesOnMe(event:MouseEvent):void {
for(var eye:String in eyeArray){
var mousdiff_a:Number = mouseY – eyeArray[eye].y;
var mousdiff_b:Number = mouseX – eyeArray[eye].x;
var radians = Math.atan2(mousdiff_a,mousdiff_b);
var degrees = radians / (Math.PI / 180);
eyeArray[eye].rotation = degrees;
}
}

source

6 thoughts on “Flash tutorial make multiple turrets or eyes rotate towards mouse or objects”

  1. Hmm, how would you make the eyes or whatever you´re rotating move towards the wanted rotation? Rather than just instantly setting it to the wanted rotation? I obviously want it to find the shortest path to the wantedrotation..

  2. @GeorgeLRockwell87 – hmmm… that would be interesting. What about making your turret think the one ship is multiple targets. Trick it by making the width of the big ship seem like 4 separate targets for your turret to eliminate. But then you'd have to program the turret to take turns firing on multiple targets in its range.

Leave a Reply

Your email address will not be published. Required fields are marked *