Flash Flex SDK-EventListener Part 3
This example is to make a small ball move in your page using your mouse. See my previous post for building this file using Ant in Eclipse. Paste this code in your .as file and compile it using your build.xml file. You can change the values in the drawCircle methods to change the radius of how big you want the circle to be. When you move your mouse, the ball will move with it in a circle. This example shows you how you can use your mouse to move an object using flash. Always make sure the name of the file that you paste this code in is the same name in this code. If you paste this code in a file with a different name, make sure you change the class name here from main4 to the new name. You also need to change the constructors name which is also called main4.
package {
import flash.display.Sprite;
import flash.events.Event;
public class main4 extends Sprite {
private var _sprite:Sprite= new Sprite( );
public function main4( ) {
_sprite.graphics.beginFill(0xffffff, 100);
_sprite.graphics.drawCircle(0, 0, 25);
_sprite.graphics.beginFill(0x000000, 100);
_sprite.graphics.drawCircle(20, 0, 5); //changin 20 can change radius
_sprite.x = 100; //location
_sprite.y = 100;
addChild(_sprite);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
var dx:Number = mouseX - _sprite.x;
var dy:Number = mouseY - _sprite.y;
var radians:Number = Math.atan2(dy, dx);
_sprite.rotation = radians * 180 / Math.PI;
}
}
}
(Part 4 coming soon) Read more...






