Letou nhà cái đánh giá

true); //remove this stage.addEventListener(MouseEvent

true); //remove this stage.addEventListener(MouseEvent

Search.
Bookmark the.

By Welcome to Part 4 of the Top-Down RPG Shooter flash game tutorial

So far, we’ve the project, and added a player that with the arrow keys and to face the mouse.
In this part, we’re going to add bullets which the player can shoot by clicking and holding the mouse down.
The very basic concepts relating to shooting have already been covered in-depth in  and of my Sidescrolling Platformer tutorial series, so this tutorial might seem a little faster paced.
If you find yourself confused with the basic ideas, it might be a good idea to review those tutorials.
(But don’t worry, I’ll still try to explain things clearly in this post, too 🙂 package { import flash.display.
Stage; import flash.display.
MovieClip; import flash.events.
Event; public class Bullet extends MovieClip { private var stageRef:Stage; //we”ll use this to check if the bullet leaves the screen borders private var speed:Number = 10; //speed that the bullet will travel at private var xVel:Number = 0; //current x velocity private var yVel:Number = 0; //current y velocity private var rotationInRadians = 0; //convenient to store our rotation in radians instead of degrees //our constructor requires: the stage, the position of the bullet, and the direction the bullet should be facing public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void { this.stageRef = stageRef; this.x = X; this.y = Y; this.rotation = rotationInDegrees; this.rotationInRadians = rotationInDegrees * Math.
PI / 180; //convert degrees to radians, for trigonometry } } } //you”ll need to import the MouseEvent to use it import flash.events.
MouseEvent; //add this line to the bottom of the “Main()” constructor function stage.addEventListener(MouseEvent.
CLICK, shootBullet, false, 0, true); //then add this new function to handle the mouse event public function shootBullet(e:MouseEvent):void { var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation); stage.addChild(bullet); } We can add an EventListener to the Bullet.as class, .

So each Bullet automatically loops

(Like we did in of the Platformer)

package { import flash.display.
Stage; import flash.display.
MovieClip; import flash.events.
Event; import flash.events.
MouseEvent; public class Main extends MovieClip { public var player:Player; public var bulletList:Array = []; //new array for the bullets public function Main():void { player = new Player(stage, 320, .

240); stage.addChild(player); stage.addEventListener(MouseEvent

CLICK, shootBullet, false, 0, .

True); stage.addEventListener(Event

ENTER_FRAME, loop, false, 0, true); //add an EventListener for the loop } public function loop(e:Event):void //create the loop function { if(bulletList.length > 0) //if there are any bullets in the bullet list { for(var i:int = bulletList.length-1; i >= 0; i–) //for each one { bulletList[i].loop(); //call its loop() function } } } public function shootBullet(e:MouseEvent):void { var bullet:Bullet = new Bullet(stage, player.x, player.y, .

Player.rotation); bullet.addEventListener(Event

REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); //triggers the “bulletRemoved()” function whenever this bullet is removed from the stage bulletList.push(bullet); //add this bullet to the bulletList array stage.addChild(bullet); } public function bulletRemoved(e:Event):void { e.currentTarget.removeEventListener(Event.
REMOVED_FROM_STAGE, bulletRemoved); //remove the event listener so we don”t get any errors bulletList.splice(bulletList.indexOf(e.currentTarget),1); //remove this bullet from the bulletList array } } } We want to move the bullet forward with a speed of 10 pixels per frame, in any possible direction.
But we can only update its x and y position every frame… there is no magic built-in concept of moving “forward”. We solve this by , depending on the bullet’s rotation, and using those velocities to update the x and y positions of the bullet.
public function loop():void //we don”t need to include the “e:Event” because we aren”t using an EventListener { xVel = Math.cos(rotationInRadians) * speed; //uses the cosine to get the xVel from the speed and rotation yVel = Math.sin(rotationInRadians) * speed; //uses the sine to get the yVel x += xVel; //updates the position y += yVel; //if the bullet goes off the edge of the screen.
if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0) { this.parent.removeChild(this); //remove the bullet } } package { import flash.display. Stage; import flash.display. MovieClip; import flash.events. Event; import flash.events. MouseEvent; public class Main extends MovieClip { public var player:Player; public var bulletList:Array = []; public var mousePressed:Boolean = false; //keeps track of whether the mouse is currently pressed down public var delayCounter:int = 0; //we use this to add delay between the shots public var delayMax:int = 8; //try changing this number to shoot more or less rapidly public function Main():void { player = new Player(stage, 320, .

240); stage.addChild(player); //stage.addEventListener(MouseEvent

CLICK, shootBullet, false, 0, true); //remove this stage.addEventListener(MouseEvent.
MOUSE_DOWN, mouseDownHandler, false, 0, .

True); stage.addEventListener(MouseEvent

MOUSE_UP, mouseUpHandler, false, 0, .

True); stage.addEventListener(Event

ENTER_FRAME, loop, false, 0, true); } public function loop(e:Event):void { if(mousePressed) // as long as the mouse is pressed.
{ delayCounter++; //increase the delayCounter by 1 if(delayCounter == delayMax) //if it reaches the max.
{ shootBullet(); //shoot a bullet delayCounter = 0; //reset the delay counter so there is a pause between bullets } } if(bulletList.length > 0) { for(var i:int = bulletList.length-1; i >= 0; i–) { bulletList[i].loop(); } } } public function mouseDownHandler(e:MouseEvent):void //add this function { mousePressed = true; //set mousePressed to true } public function mouseUpHandler(e:MouseEvent):void //add this function { mousePressed = false; //reset this to false } public function shootBullet():void //delete the “e:MouseEvent” parameter { var bullet:Bullet = new Bullet(stage, player.x, player.y, .

Player.rotation); bullet.addEventListener(Event

REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); bulletList.push(bullet); stage.addChild(bullet); } public function bulletRemoved(e:Event):void { e.currentTarget.removeEventListener(Event.
REMOVED_FROM_STAGE, bulletRemoved); bulletList.splice(bulletList.indexOf(e.currentTarget),1); } } } package { import flash.display.
Stage; import flash.display.
MovieClip; import flash.events.
Event; public class Bullet extends MovieClip { private var stageRef:Stage; private var speed:Number = 10; private var xVel:Number = 0; private var yVel:Number = 0; private var rotationInRadians = 0; public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void { this.stageRef = stageRef; this.x = X; this.y = Y; this.rotation = rotationInDegrees; this.rotationInRadians = rotationInDegrees * Math.
PI / 180; } public function loop():void { xVel = Math.cos(rotationInRadians) * speed; yVel = Math.sin(rotationInRadians) * speed; x += xVel; y += yVel; if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0) { this.parent.removeChild(this); } } } } That’s it for today’s tutorial. We have successfully programmed a player that can move, rotate, and shoot. Up next, we will begin to add more elements to the game for the player to interact with. You can get the full source files for this tutorial. This entry was posted in , , , and tagged , , , , , , , , , , ,. 18 comments on “”. Ben L. James A. walker. Nick W. Gman. Medo mdn. Daniel. Martynas. steve. CMS. kimster. William. Ben L. Ben L. Leave a Reply. Enter your comment here. Email Name Website You are commenting using your WordPress.com account. (  /   ) You are commenting using your Google account. (  /   ) You are commenting using your Twitter account. (  /   ) You are commenting using your Facebook account. (  /   ) on the App Store for relaxing bubble-popping and 50 characters to unlock.

On the App Store for dozens of brain-twisting puzzle challenges

on the App Store for cute, reflex-based fun.
Monthly Archives Select Month July 2013  (1) June 2013  (1) May 2013  (2) November 2012  (1) September 2012  (2) July 2012  (1) June 2012  (2) May 2012  (1) April 2012  (2) March 2012  (1) February 2012  (4) January 2012  (3) November 2011  (2) September 2011  (4) August 2011  (3) March 2011  (3).
Post to bloggers like this:.