flash on 2013-6-25
/**
* Copyright yurij.shaulov ( http://wonderfl.net/user/yurij.shaulov )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sAj2
*/
package{
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class Main extends Sprite {
public var space:Sprite = new Sprite();
public var _pcos:Number;
public var _psin:Number;
public var _endX:Number;
public var _endY:Number;
public var _bullets:Array = new Array();
public var _meteorites:Array = new Array();
private var _arrow:Arrow;
private var _speed:Number = 5;
private var time:Number=0;
public function Main() {
init();
}
private function init():void{
addChild(_arrow = new Arrow);
addChild(space);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler($event:Event):void{
var dx:Number = mouseX - _arrow.x;
var dy:Number = mouseY - _arrow.y;
if(Math.abs(dx) < 2 && Math.abs(dy) < 2){
return;
}
var angle:Number = Math.atan2(dy, dx);
_arrow.rotation = angle * 180 / Math.PI;
_arrow.x += Math.cos(angle) * _speed;
_arrow.y += Math.sin(angle) * _speed;
createBullet();
updateBullets();
if (time != 10) {
time++;
}else {
addMeteorite(Math.random() * 2);
time = 0;
}
}
private function addMeteorite(count:Number):void {
for (var i:int = 0; i < count; i++) {
var metiorit:Meteorite = new Meteorite();
metiorit.x = Math.random() * stage.stageWidth;
metiorit.y = Math.random() * stage.stageHeight;
_meteorites.push(metiorit);
space.addChild(metiorit);
}
}
private function createBullet():void
{
var tempBullet:MovieClip = new Bullet();
tempBullet.addEventListener(Event.ENTER_FRAME, hitObj);
_pcos = Math.cos(_arrow.rotation * Math.PI / 180);
_psin = Math.sin(_arrow.rotation * Math.PI / 180);
var _startX:Number = _arrow.x - 5 * _pcos;
var _startY:Number = _arrow.y - 5 * _psin;
//var _startX:Number = _arrow.x - 20 * _pcos;
//var _startY:Number = _arrow.y - 20 * _psin;
_endX = _arrow.x - 50 * _pcos + Math.random() * 20 - 20 * .5;
_endY = _arrow.y - 50 * _psin + Math.random() * 20 - 20 * .5;
// calculate velocity
tempBullet.vx = (_endX - _startX) / 2;
tempBullet.vy = (_endY - _startY) / 2;
// set position
tempBullet.x = _startX;
tempBullet.y = _startY;
// save starting location
tempBullet.startX = _startX;
tempBullet.startY = _startY;
// set maximum allowed travel distance
//tempBullet.maxDistance = 200;
// add bullet to bullets array
_bullets.push(tempBullet);
// add to display list
stage.addChild(tempBullet);
}
private function hitObj(e:Event):void {
for (var i:int = 0; i < _meteorites.length;i++){
if (e.target.hitTestObject(_meteorites[i])) {
this.space.removeChild(_meteorites[i])
}
}
}
private function updateBullets():void
{
var i:int;
var tempBullet:MovieClip;
// loop thru _bullets array
for (i = 0; i < _bullets.length; i++)
{
// save a reference to current bullet
tempBullet = _bullets[i];
// update bullet position
tempBullet.x -= tempBullet.vx;
tempBullet.y -= tempBullet.vy;
}
}
}
}
import flash.display.Sprite;
class Arrow extends Sprite{
function Arrow(){
init();
cacheAsBitmap = true;
}
private function init():void{
graphics.beginFill(0xd90028);
graphics.lineStyle(0);
graphics.moveTo(20, 0);
graphics.lineTo(0, 20);
graphics.lineTo(0, 10);
graphics.lineTo(-20, 10);
graphics.lineTo(-20, -10);
graphics.lineTo(0, -10);
graphics.lineTo(0, -20);
graphics.lineTo(20, 0);
graphics.endFill();
}
}
import flash.display.MovieClip
class Bullet extends MovieClip {
public var maxDistance:Number;
public var startX:Number;
public var startY:Number;
public var vx:Number;
public var vy:Number;
function Bullet(){
init();
cacheAsBitmap = true;
}
private function init():void{
graphics.beginFill(0x00000c);
graphics.drawCircle(0, 0, Math.random()*5);
graphics.endFill();
}
}
import flash.display.MovieClip
class Meteorite extends MovieClip {
public var vx:Number;
public var vy:Number;
function Meteorite(){
init();
cacheAsBitmap = true;
}
private function init():void{
graphics.beginFill(0x663333);
graphics.drawCircle(0, 0, Math.random() * 30);
graphics.endFill();
graphics.beginFill(0x743A3A);
graphics.drawCircle(Math.random() * this.width/2, Math.random() * this.height/2, 3);
graphics.drawCircle(Math.random() * this.width / 2, Math.random() * this.height / 2, 3);
graphics.drawCircle(Math.random() * this.width/2, Math.random() * this.height/2, 3);
graphics.drawCircle(Math.random() * this.width/2, Math.random() * this.height/2, 3);
graphics.endFill();
}
public function destroy(s:MovieClip):void {
root.parent.removeChild(s);
}
}