Remember R-Type? Well how about a really, REALLY cut down version in flash? How cool would that be?!
Well, ponder no more, I give you the answer!
21 lines of AS goodness (not including ‘else’) with room for improvement!
Changelog
Now even better with 21 lines of code (kookaburra) - 20/01/06
Can no longer shoot after dying (pointed out by hybrid101) - 20/01/06
Stole Ben’s idea through telepathy - 17/01/06
chain(chain(chain(chain(chain(chain(chain(chain(chain(chain(chain(chain(chain(createEmptyMovieClip("ship", 0), "beginFill", [0x000000, 100]), "lineTo", [20, 10]), "lineTo", [0, 20]), "lineTo", [0, 0]).duplicateMovieClip("enemy", 1, {_x:830, _y:-100, enemynum:200, onEnterFrame:generateEnemy}), "clear", []), "beginFill", [0x000000, 100]), "lineTo", [20, 0]), "lineTo", [20, 20]), "lineTo", [0, 20]), "lineTo", [0, 0]).duplicateMovieClip("shot", 2, {_x:-30, _y:-100, shotnum:50, lastshot:0, delay:3}), "clear", []), "lineStyle", [2, 0x000000, 100]), "lineTo", [10, 0]).onEnterFrame = function() {
this.lastshot -= (this.lastshot == 0 ? 0 : 1);
};
function chain(target:MovieClip, method:String, params:Array):MovieClip
{
target[method](params[0], params[1], params[2], params[3], params[4]);
return target;
}
function onEnterFrame():Void
{
this["ship"]._y = ((this["ship"]._y += 10 * (Number(Key.isDown(Key.DOWN)) + (-1 * Number(Key.isDown(Key.UP))))) < 0 ? 0 : (this["ship"]._y > (600 - this["ship"]._height) ? (600 - this["ship"]._height) : this["ship"]._y));
this["ship"]._x = ((this["ship"]._x += 10 * (Number(Key.isDown(Key.RIGHT)) + (-1 * Number(Key.isDown(Key.LEFT))))) < 0 ? 0 : (this["ship"]._x > (800 - this["ship"]._width) ? (800 - this["ship"]._width) : this["ship"]._x));
if (this["ship"] && Key.isDown(Key.SPACE) && this["shot"].lastshot <= 0)
chain(this["shot"], "duplicateMovieClip", ["shot" + (this["shot"].shotnum == 100 ? this["shot"].shotnum = 10 : this["shot"].shotnum), this["shot"].shotnum++, {_x:this["ship"]._x + 20, _y:this["ship"]._y + 10, onEnterFrame:checkShot}])._parent["shot"].lastshot = this["shot"].delay;
}
function checkShot():Void
{
if((this._x += 10) > 800)
this.removeMovieClip();
}
function generateEnemy():Void
{
if (Math.round(Math.random() * 10) == 0)
this.duplicateMovieClip("enemy" + (this["enemynum"] == 300 ? this["enemynum"] = 200 : this["enemynum"]), this["enemynum"]++, {_x:820, _y:Math.random() * 580, onEnterFrame:this._parent.checkEnemy});
}
function checkEnemy():Void
{
if (this.hitTest(_parent["ship"]))
{
_parent["ship"].removeMovieClip();
}
else if((this._x -= 10) < -this._width)
{
this.removeMovieClip();
}
else
{
for (var i:Number = 10; i <= _parent["shot"].shotnum; i++)
{
if (this.hitTest(_parent["shot" + i]))
{
_parent["shot" + i].removeMovieClip();
this.removeMovieClip();
break;
}
}
}
}
Have fun! :hitman: