Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code to move character
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.ui.*;
- import Shark;
- public class Character extends MovieClip {
- public var maxSpeed=10;
- public var dx=0;
- public var dy=0;
- public var rot=0;
- private var keysDown: Array = new Array();
- // Declare variables:
- var bullet:Bullet;
- private var dir=0;
- private var speed=0;
- var reload:int = 10;
- private var bullets:Array;
- public function Character(bulletList) {
- bullets=bulletList;
- // Initialize variables:
- addEventListener(Event.ADDED_TO_STAGE, init);
- }
- function init(e:Event):void {
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
- stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
- this.addEventListener(Event.ENTER_FRAME, processInput);
- this.addEventListener(Event.ENTER_FRAME, moveMe);
- removeEventListener(Event.ADDED_TO_STAGE, init);
- }
- private function keyPressed(e:KeyboardEvent):void {
- keysDown[e.keyCode]=true;
- // Handle keys pressed:
- }
- private function keyReleased(e:KeyboardEvent):void {
- keysDown[e.keyCode]=false;
- }
- private function processInput(e:Event) {
- // Handle keys held:
- if (keysDown[Keyboard.LEFT]) {
- dir-=5;
- if (dir<0) {
- dir+=360;
- }
- }
- if (keysDown[Keyboard.RIGHT]) {
- dir+=5;
- if (dir>360) {
- dir-=360;
- }
- }
- if (keysDown[Keyboard.DOWN]) {
- speed-=1;
- }
- if (keysDown[Keyboard.UP]) {
- speed+=1;
- }
- if (--reload==0) {
- reload=1;
- if (keysDown[Keyboard.SPACE]) {
- reload=10;
- bullet=new Bullet(x,y,20*Math.cos(rot*Math.PI/180),20*Math.sin(rot*Math.PI/180),rot);
- stage.addChild(bullet);
- bullets.push(bullet);
- }
- }
- // Optional: friction and maximum speed:
- if (speed>maxSpeed) {
- speed=maxSpeed;
- } else if (speed>0) {
- speed-=0.1;
- } else if (-speed>maxSpeed) {
- speed=- maxSpeed;
- } else if (speed<0) {
- speed+=0.1;
- }
- dx=speed*Math.cos(dir*Math.PI/180);
- dy=speed*Math.sin(dir*Math.PI/180);
- rot=dir;
- }
- public function get Xpos():Number {
- return this.x;
- }
- public function get Ypos():Number {
- return this.y;
- }
- private function moveMe(e:Event) {
- this.x+=dx;
- this.y+=dy;
- this.rotation=rot;
- // stop if it tries to go off the screen
- if (this.x<0) {
- this.x=0;
- }
- if (this.x>stage.stageWidth) {
- this.x=stage.stageWidth;
- }
- if (this.y<0) {
- this.y=0;
- }
- if (this.y>stage.stageHeight) {
- this.y=stage.stageHeight;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement