所有
我正在尝试在我的代码中使用计时器,以便在我正在进行的以战舰为主题的项目中进行船舶旋转,移动和炮塔旋转。但是,尽管我付出了努力,但错误1120仍在继续出现。有什么建议吗?
附件是我的代码。当我使用TurretArray以及为定时器执行addEventListener时会出现问题。
package
{
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.DisplayObject;
import flash.events.TimerEvent;
import flash.events.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.easing.None;
import flash.display.*;
import flash.ui.*;
[SWF(width = "600", height = "480")]
public class Main extends MovieClip
{
private var game:Game = new Game();
//Sets speeds to objects
private var bulletSpeed:int = 7;
private var playerSpeed:Number = 0.4;
private var playerRotateSpeed:Number = 0.1;
//Create an array to hold multiple sprites
private var mySpriteHolder = new Array();
private var BulletAngle = new Array();
private var BulletBounce = new Array();
//Create a counter to keep track of the sprites
private var lbCounter:int = 0;
//Maximum number of sprites (count starts a 1 not 0)
private var maxLB:int = 20;
//Variable for player 1
private var player1angle:Number = 270;
private var keyPressed1:int = 0;
private var keyPressed2:int = 0;
private var keyPressed3:int = 0;
private var keyPressed4:int = 0;
private var p1Score:int = 0;
private var reloaded:int = 0;
private var k:int = 0;
private var reloadTime:int = 10;
private var TurretArray:Array = new Array();
//public function loadTurrets(){
//TurretArray = new Array();
TurretArray[0] = mvi_PTurret1; //Problem occurs in these 3 lines
TurretArray[1] = mvi_PTurret2;
TurretArray[2] = mvi_PTurret3;
//}
private var Turret1SpinDirection:int = 2;
private var Turret2SpinDirection:int = 2;
private var PTurret1angle:Number = 270;
private var PTurret2angle:Number = 270;
//create a new Timer
private var p1RotateTimer:Timer = new Timer(10);
private var p1MoveTimer:Timer = new Timer(10);
private var PTurretRotateTimer:Timer = new Timer(10);
//public function loadTimers(){
//add a listener to the timer
p1RotateTimer.addEventListener("timer",p1Rotate); //Problem here
p1MoveTimer.addEventListener("timer",p1Move);
PTurretRotateTimer.addEventListener("timer",PTurretRotate);
//}
//Create a listener for the keypress
//this.addEventListener(KeyboardEvent.KEY_DOWN,PMove);
// this.addEventListener(KeyboardEvent.KEY_UP,PStop);
// this.addEventListener(MouseEvent.MOUSE_MOVE,mouseCoordinate);
public function p1Rotate(eventArgs:TimerEvent):void{
var i:int;
if(keyPressed1 == 68){
mvi_PBattleship.rotation += playerRotateSpeed;
player1angle += playerRotateSpeed;
if(player1angle == 360){
player1angle = 0;
}
/*for(i = 0; i < 3; i++){
TurretArray[i].rotation += playerRotateSpeed;
TurretArray[i].rotation = Math.round(TurretArray[i].rotation);
}*/
//txt_PlayerAngle.text = player1angle.toString();
}
if(keyPressed2 == 65){
mvi_PBattleship.rotation -= playerRotateSpeed;
player1angle -= playerRotateSpeed;
if(player1angle == 0){
player1angle = 360;
}
/*for(i = 0; i < 3; i++){
TurretArray[i].rotation -= playerRotateSpeed;
TurretArray[i].rotation = Math.round(TurretArray[i].rotation);
}*/
//txt_PlayerAngle.text = player1angle.toString();
}
//Keeps Turret 1 with the ship
mvi_PTurret1.x = Math.cos(player1angle* (Math.PI/180)) * 32.9 + mvi_PBattleship.x;
mvi_PTurret1.y = Math.sin(player1angle* (Math.PI/180)) * 32.9 + mvi_PBattleship.y;
//Keeps Turret 2 with the ship
mvi_PTurret2.x = Math.cos(player1angle* (Math.PI/180)) * 13.4 + mvi_PBattleship.x;
mvi_PTurret2.y = Math.sin(player1angle* (Math.PI/180)) * 13.4 + mvi_PBattleship.y;
//Keeps Turret 3 with the ship
mvi_PTurret3.x = Math.cos(player1angle* (Math.PI/180)) * -56.5 + mvi_PBattleship.x;
mvi_PTurret3.y = Math.sin(player1angle* (Math.PI/180)) * -56.5 + mvi_PBattleship.y;
}
public function p1Move(eventArgs:TimerEvent):void{
/*if(p1Score > 0){
p1Score -= pointsLost;
}*/
//This moves the player forward or backwards in the correct direction
if(keyPressed3 == 87){
x -= Math.cos(player1angle* (Math.PI/180)) * playerSpeed;
y -= Math.sin(player1angle* (Math.PI/180)) * playerSpeed;
}
if(keyPressed4 == 83){
x += Math.cos(player1angle* (Math.PI/180)) * playerSpeed;
y += Math.sin(player1angle* (Math.PI/180)) * playerSpeed;
}
//This keeps the player in the center of the screen
mvi_PBattleship.x =-x + 400;
mvi_PBattleship.y =-y + 300;
//txt_PBattleshipx.text = x.toString();
//txt_PBattleshipy.text = y.toString();
//Keeps Turret 1 with the ship
mvi_PTurret1.x = Math.cos(player1angle* (Math.PI/180)) * 32.9 + mvi_PBattleship.x;
mvi_PTurret1.y = Math.sin(player1angle* (Math.PI/180)) * 32.9 + mvi_PBattleship.y;
//Keeps Turret 2 with the ship
mvi_PTurret2.x = Math.cos(player1angle* (Math.PI/180)) * 13.4 + mvi_PBattleship.x;
mvi_PTurret2.y = Math.sin(player1angle* (Math.PI/180)) * 13.4 + mvi_PBattleship.y;
//Keeps Turret 3 with the ship
mvi_PTurret3.x = Math.cos(player1angle* (Math.PI/180)) * -56.5 + mvi_PBattleship.x;
mvi_PTurret3.y = Math.sin(player1angle* (Math.PI/180)) * -56.5 + mvi_PBattleship.y;
}
public function PMove(Evt:KeyboardEvent):void{
//trace(Evt.keyCode);
if(Evt.keyCode==68){
keyPressed1 = 68;
p1RotateTimer.start();
}
if(Evt.keyCode==65){
keyPressed2 = 65;
p1RotateTimer.start();
}
if(Evt.keyCode==87){
keyPressed3 = 87;
p1MoveTimer.start();
}
if(Evt.keyCode==83){
keyPressed4 = 83;
p1MoveTimer.start();
}
}
public function PStop(Evt:KeyboardEvent):void{
if(Evt.keyCode==87){
keyPressed3 = 0;
p1MoveTimer.stop();
}
else if(Evt.keyCode==83){
keyPressed4 = 0;
p1MoveTimer.stop();
}
else if(Evt.keyCode==68){
keyPressed1 = 0;
p1RotateTimer.stop();
}
else if(Evt.keyCode==65){
keyPressed2 = 0;
p1RotateTimer.stop();
}
}
public function PTurretRotate(Evt:Event){
var Mouse1x:Number;
var Mouse1y:Number;
var Mouse1Angle:Number;
Mouse1x = mvi_PTurret1.x - mouseX;
Mouse1y = mvi_PTurret1.y - mouseY;
Mouse1Angle = Math.round(Math.atan2(Mouse1y,Mouse1x) * (180/Math.PI) + 180);
if(Turret1SpinDirection == 0 && Mouse1Angle < 90 && PTurret1angle > 270){
Mouse1Angle = Math.round(Math.atan2(Mouse1y,Mouse1x) * (180/Math.PI) + 180)+ 360;
}
else if(Turret1SpinDirection == 1 && Mouse1Angle > 270 && PTurret1angle < 90){
Mouse1Angle = Math.round(Math.atan2(Mouse1y,Mouse1x) * (180/Math.PI) + 180)- 360;
}
if(PTurret1angle > 360){
PTurret1angle = 1;
}
if(PTurret1angle < 0){
PTurret1angle = 359;
}
if(PTurret1angle < Math.round(player1angle) - 210 || PTurret1angle > Math.round(player1angle) - 150){
if(Mouse1Angle > PTurret1angle){
mvi_PTurret1.rotation += 1;
PTurret1angle += 1;
Turret1SpinDirection = 0;
}
else if(Mouse1Angle < PTurret1angle){
mvi_PTurret1.rotation -= 1;
PTurret1angle -= 1;
Turret1SpinDirection = 1;
}
}
else{
if(PTurret1angle == Math.round(player1angle) - 210){
mvi_PTurret1.rotation -= 1;
PTurret1angle -= 1;
}
else if(Math.round(player1angle) - 210 < 0){
if(PTurret1angle + 90 == Math.round(player1angle) - 210 + 90){
mvi_PTurret1.rotation -= 1;
PTurret1angle -= 1;
}
}
if(PTurret1angle == Math.round(player1angle) - 150){
mvi_PTurret1.rotation += 1;
PTurret1angle += 1;
}
}
var Mouse2x:Number;
var Mouse2y:Number;
var Mouse2Angle:Number;
Mouse2x = mvi_PTurret2.x - mouseX;
Mouse2y = mvi_PTurret2.y - mouseY;
Mouse2Angle = Math.round(Math.atan2(Mouse2y,Mouse2x) * (180/Math.PI) + 180);
if(Turret2SpinDirection == 0 && Mouse2Angle < 90 && PTurret2angle > 270){
Mouse2Angle = Math.round(Math.atan2(Mouse2y,Mouse2x) * (180/Math.PI) + 180)+ 360;
}
else if(Turret2SpinDirection == 1 && Mouse2Angle > 270 && PTurret2angle < 90){
Mouse2Angle = Math.round(Math.atan2(Mouse2y,Mouse2x) * (180/Math.PI) + 180)- 360;
}
if(PTurret2angle > 360){
PTurret2angle = 1;
}
if(PTurret2angle < 0){
PTurret2angle = 359;
}
if(Mouse2Angle > PTurret2angle){
mvi_PTurret2.rotation += 1;
PTurret2angle += 1;
Turret2SpinDirection = 0;
}
else if(Mouse2Angle < PTurret2angle){
mvi_PTurret2.rotation -= 1;
PTurret2angle -= 1;
Turret2SpinDirection = 1;
}
//txt_Turret1Angle.text = Mouse1Angle.toString();
//txt_PTurret.text = PTurret1angle.toString();
}
public function mouseCoordinate(Evt:Event){
//txt_X.text = mouseX.toString();
//txt_Y.text = mouseY.toString();
PTurretRotateTimer.start();
}
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.frameRate = 30;
addChild(game);
}
private function mouseMove(e :MouseEvent) :void {
Game.mouse.x = e.stageX;
Game.mouse.y = e.stageY;
}
private function enterFrame(e :Event) :void {
game.update();
}
}
}
非常感谢。