Показать сообщение отдельно
Старый 11.03.2010, 04:42
WERt вне форума Посмотреть профиль Отправить личное сообщение для WERt Найти все сообщения от WERt
  № 1  
Ответить с цитированием
WERt

Регистрация: Mar 2010
Сообщений: 29
По умолчанию проблема с KeyboardEvent

Доброе всем время суток.
У меня есть такая проблемка,не работает слушатель событий клавиатуры почему-то.
я делаю игру, и хотел чтобы танчик двигался по нажатию клавиш, но в связи с косяком пришлось перейти на мышку(ее события отслеживаются нормально). Причем, если я создаю просто action script project - то все впаряде, а если я создаю flex project - то не реагирует на собитие клавиатуры.
Люди добрые, помогите мне разобраться, я совсем новичек, мало опыта, так бы не спрашивал)а то с мышкой двигаться вообще бред)
вот код:
Код AS3:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initial()" >
<mx:Canvas id="mainCan" width="800" height="600" styleName="can1" x="250" y="35" verticalScrollPolicy="off" horizontalScrollPolicy="off" >
 
</mx:Canvas>
<mx:Script>
	<![CDATA[
		import Display.*;
		import flash.events.MouseEvent;
		import flash.events.TimerEvent;
		import flash.events.KeyboardEvent;
		import flash.utils.Timer;	
		import flash.ui.Keyboard;
 
		public var tank1:tankView;
		public var moveTimer:Timer;
		public var patron:Bullet;
		public var moveTimer1:Timer;
		public var bullet:Bullet;
		public static var startDirection:Number;
		public var directTimer:Timer;
		public function initial():void
		{
 
			var Bitm:hearthView = new hearthView();
			mainCan.addChild(Bitm);
			tank1 = tankView.singleton();
			mainCan.addChild(tank1);
 
 
			var hpBar:Hpbar = new Hpbar();
			mainCan.addChild(hpBar);
			hpBar.x = 785;
 
			mainCan.addEventListener(MouseEvent.CLICK,onClick);
			moveTimer = new Timer(1);
			moveTimer.addEventListener(TimerEvent.TIMER,onMove);
 
 
			startDirection = tank1.rotation;
			tank1.addEventListener(MouseEvent.MOUSE_OVER,showBullet);
 
			directTimer = new Timer(1);
			directTimer.addEventListener(TimerEvent.TIMER,changeDirection);
 
			application.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
 
 
		}
 
		public function keyDown(evt:KeyboardEvent):void
		{
			if(evt.keyCode == Keyboard.UP) {tank1.x +=50;}
			else if (evt.keyCode == Keyboard.DOWN) {tank1.x -=50;}
		}
 
 
		public function showBullet(evt:MouseEvent):void
		{
			bullet = Bullet.singleton();
			bullet.x = tank1.x;
			bullet.y = tank1.y;
			bullet.moveBul();
			mainCan.addChild(bullet);
		}
		public function onClick(evt:MouseEvent):void
		{
 
			moveTimer.start();
			tank1.positionX = mainCan.mouseX ;
			tank1.positionY = mainCan.mouseY ;
		}
 
		public function onMove(evt:TimerEvent):void
		{
 
				if(tank1.x <= tank1.positionX )
				{
					tank1.x += 0.2;
					tank1.direction = 1;		
				}
 
				else if(tank1.x >= tank1.positionX )
				{
					tank1.x -= 0.2;
					tank1.direction = 2;
				}
 
				if(tank1.x == tank1.positionX)
				{
					if(tank1.y <= tank1.positionY )
					{
						tank1.y += 0.2;	
						tank1.direction = 3;		
					}
 
					else if(tank1.y >= tank1.positionY )
					{
						tank1.y -= 0.2;
						tank1.direction = 4;
					}
 
				}
				trace(tank1.direction);
		}
 
 
 
		public function changeDirection(evt:TimerEvent):void
		{
			if (tank1.direction == 3){tank1.rotation = startDirection + 90; }
			else if (tank1.direction == 4){tank1.rotation = startDirection - 90; }
			else if (tank1.direction == 1){tank1.rotation = startDirection; }
			else if (tank1.direction == 2){tank1.rotation = startDirection + 180; }
		}
 
 
 
 
	]]>
</mx:Script>
<mx:Style source="../src/Display/Style.css" />
 
 
 
</mx:Application>
ну соответственно описание класса танка и пули в других файлах...)
Заранее-спасибо!


Последний раз редактировалось WERt; 11.03.2010 в 23:31.