Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 获取键盘输入的数组_Arrays_Actionscript 3_Indexing - Fatal编程技术网

Arrays 获取键盘输入的数组

Arrays 获取键盘输入的数组,arrays,actionscript-3,indexing,Arrays,Actionscript 3,Indexing,这段代码来自一个教程,我确实理解它——只是不理解数组。我知道数组在索引中保存值(array[1]=something,array[2]=something…) 但是数组“键”是如何在更新函数中具有键盘.RIGHT值的呢 package { -imports here- public class Main extends Sprite { var keys:Array = []; var ball:Sprite = new Sprite

这段代码来自一个教程,我确实理解它——只是不理解数组。我知道数组在索引中保存值(
array[1]
=something,
array[2]
=something…)

但是数组“键”是如何在更新函数中具有
键盘.RIGHT
值的呢

package 
{
    -imports here-

    public class Main extends Sprite 
    {
        var keys:Array = [];
        var ball:Sprite = new Sprite();

        public function Main():void 
        {
            ball.graphics.beginFill(0x000000);
            ball.graphics.drawCircle(0, 0, 50);
            ball.graphics.endFill;

            addChild(ball);

            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;

            ball.addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        }

        private function update(e:Event):void {
            if (keys[Keyboard.RIGHT]) {
                ball.x += 5;
            }            
        }

        function onKeyDown(e:KeyboardEvent):void {
            keys[e.keyCode] = true;
        }

        function onKeyUp(e:KeyboardEvent):void {
            keys[e.keyCode] = false;
        }
    }
}
(因为,以Pascal为例,为了给变量赋值,我必须编写
readln(array[1])
——这将给出用户键入的任何值
array[1]


所以,我不知道
keys[]
是如何获得键盘输入的:p

e.keyCode是一个整数。调用
onKeyDown
时,代码使用整数将数组中的值设置为true

例如,如果在键盘上按下右键,e.keyCode将为39,因此该代码与
keys[39]=true相同

如果您查看有关Keyboard的文档,您将看到Keyboard.RIGHT定义为39

在下一帧,将触发更新事件。 当您编写一个没有==的if语句时,它有点像是为了,所以if语句本质上是说

if(keys[39] == true) {
    ball.x += 5;
}