Here it is:
http://sainezart.blogspot.com/2011/03/art-351-final-project-game.html package
Programmer: Me
Graphics: Sam Soto
Animator:Frankie Rodrigues
Here is the tutorial that helped us get this working:
http://asgamer.com/2009/as3-flash-games-for-beginners-registering-hit-tests
Here is our code for rotating our guy:
stage.addEventListener(Event.ENTER_FRAME, rotateSonso); //my event listener
private function rotateSonso(Event):void //here is my function
{
xd = (stage.mouseX-this.x); /*leave this(suggested)*/
yd = (stage.mouseY-this.y); /*leave this(suggested)*/
// finds the x and y difference of the sprite and the mouse
radAngle = Math.atan2(yd, xd); /*leave this(suggested)*/
// finds the angle in radians (flash works in radians not degrees)
this.rotation = int(radAngle*360/(Math.PI*2)+90); /*leave this(suggested)*/
// uses PI because radians work with PI (not the tasty kind either)}//close rotateSonso
To rotate your bullet with the character:
stageRef.addChild(new LaserGreen(stageRef, x, y, rotation)); //this is the code that adds the bullet this is located in my Sonso.as
In my LaserGreen.as:
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;public class LaserGreen extends MovieClip
{private var stageRef:Stage;
private var bulletSpeed:Number = 16;
var shotSpeed:int = 16;public function LaserGreen (stageRef:Stage, x:Number, y:Number, rotation:Number) : void
{
this.stageRef = stageRef;
this.x = x;
this.y = y;
this.rotation = rotation;addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
addEventListener(Event.ENTER_FRAME, moveShot);}
private function loop(e:Event) : void
{
if (y 1200)
removeSelf();for (var i:int = 0; i < Engine.enemyList.length; i++)
{
if (hitTestObject(Engine.enemyList[i].hit))
{
trace("hitEnemy");
Engine.enemyList[i].takeHit();
removeSelf();
}
}}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);trace("remove");
if (stageRef.contains(this))
stageRef.removeChild(this);
}function moveShot(event:Event)
{ /*leave this(suggested)*/
with (event.target)
{ /*leave this(suggested)*/// with the object that called the event
this.x+= shotSpeed * Math.sin(rotation*(Math.PI/180)); /*leave this(suggested)*/
this.y-= shotSpeed * Math.cos(rotation*(Math.PI/180)); /*leave this(suggested)*/
// moves the shot depending on its rotation, uses build in math sin and cos functions
// remember that flash works in radians
}
}// close moveShot
}
}
To make sure when Mouse is held down the bullet keeps firing, you need to set an interval for each bullet that is being created:
Initialize:
private var bulletInterval = setInterval(createBullet, 150);
private var mouseIsDown:Boolean = false; //if mouse is down set to false
After that you gotta to get the event listener:
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
Here is the the function:
private function mDown(MouseEvent):void
{
mouseIsDown = true;
}//close mDownprivate function mUp(MouseEvent):void
{
mouseIsDown = false;}//close mUp
private function createBullet() : void
{
//if canFire is true, fire a bullet
//set canFire to false and start our timer
//else do nothing.if (canFire && mouseIsDown)
{
stageRef.addChild(new LaserGreen(stageRef, x, y, rotation));
canFire = false;
soundChannel = laserSound.play(0);
fireTimer.start();
}}