Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 3.0 (http://www.flasher.ru/forum/forumdisplay.php?f=83)
-   -   проблема с KeyboardEvent (http://www.flasher.ru/forum/showthread.php?t=137347)

WERt 11.03.2010 04:42

проблема с 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>

ну соответственно описание класса танка и пули в других файлах...)
Заранее-спасибо!:)

F1esk 11.03.2010 06:33

3. Каждый должен обрамлять код в своем сообщении тэгами
Код:

// Ваш Код
.
Чтобы ваш код можно было читать, и просто показать уважение к глазам других форумчан.
Правила разделов "ActionScript", "ActionScript 3", "Flex" и "Общие вопросы"

WERt 11.03.2010 23:31

народ,помогите,срочно нужно)


Часовой пояс GMT +4, время: 11:43.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.