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
Actionscript 3 如何在as3中使用“a”键松开钥匙时触发;“钥匙”;对象_Actionscript 3_Event Handling_Keyboard_Keyboard Events - Fatal编程技术网

Actionscript 3 如何在as3中使用“a”键松开钥匙时触发;“钥匙”;对象

Actionscript 3 如何在as3中使用“a”键松开钥匙时触发;“钥匙”;对象,actionscript-3,event-handling,keyboard,keyboard-events,Actionscript 3,Event Handling,Keyboard,Keyboard Events,我目前正在做的项目是一个自上而下的视频游戏,类似于原始的塞尔达游戏 如果玩家按下箭头键,在行走动画中间放过它,就会出现错误。它会在当前动画的任何帧上停止,导致播放器在不移动时看起来像是站在一条腿上,这看起来非常愚蠢。我认为这可以通过将角色的sprNum设置为松开键时方向的原始数字来解决。但我不知道该怎么做。(另一个错误是,如果你在行走动画的中间停下来,然后继续朝同一个方向前进,它就停留在你停下来的任何框架上,看起来好像在四处漂流,我认为这是由同一件事引起的。) 在游戏中,我使用箭头键在屏幕上移动

我目前正在做的项目是一个自上而下的视频游戏,类似于原始的塞尔达游戏

如果玩家按下箭头键,在行走动画中间放过它,就会出现错误。它会在当前动画的任何帧上停止,导致播放器在不移动时看起来像是站在一条腿上,这看起来非常愚蠢。我认为这可以通过将角色的sprNum设置为松开键时方向的原始数字来解决。但我不知道该怎么做。(另一个错误是,如果你在行走动画的中间停下来,然后继续朝同一个方向前进,它就停留在你停下来的任何框架上,看起来好像在四处漂流,我认为这是由同一件事引起的。) 在游戏中,我使用箭头键在屏幕上移动玩家,按下箭头键时会告诉角色移动的方向、面对的方向,并在移动时播放行走动画

这是通过创建一个“keys”对象,然后将箭头键添加到该对象中来完成的

在构造函数中,我调用prepareGame函数,其中如下所示

keys = new Object();
//fill the object with arrow keys
keys[Keyboard.UP] = {down :false, dirx:0, diry:-1, sprNum:1, anim:[1,5,5,5,1,5,9,5]};
keys[Keyboard.DOWN] = {down:false, dirx:0, diry:1, sprNum:0, anim:[0,5,4,5,0,5,8,5]};
keys[Keyboard.LEFT] = {down:false, dirx:-1, diry:0, sprNum:3, anim:[3,5,7,5,3,5,11,5]};
keys[Keyboard.RIGHT] = {down:false, dirx:1, diry:0, sprNum:2, anim:[2,5,6,5,2,5,10,5]};
myParent.addEventListener (KeyboardEvent.KEY_DOWN, downKeys);
myParent.addEventListener (KeyboardEvent.KEY_UP, upKeys);
以下是向上键和向下键功能

//this function will detect keys that are being pressed
    private function downKeys (ev:KeyboardEvent):void 
    {
        //check if the is arrow key
        if (keys[ev.keyCode] != undefined) 
        {
            //set the key to true
            keys[ev.keyCode].down = true;
        }
    }
    //this function will detect keys that are being released
    private function upKeys (ev:KeyboardEvent):void 
    {
        //check if the is arrow key
        if (keys[ev.keyCode] != undefined) 
        {
            //set the key to false

            keys[ev.keyCode].down = false;


        }
    }
如何检测箭头键何时未按下?我需要这样做,这样我就可以在行走动画的第一帧上,将播放器的sprNum设置为它所面对的任何方向,使它看起来像是站着的

非常感谢您的帮助:)

编辑: 下面是所有处理播放器动画的代码

在英雄班

            public var anim:Array;
    public var animCount:int;
    public var animTime:int;
            public var sprNum:Number;


    public function Hero(spr:Number, tileSize:int, xt:int, yt:int) 
    {
        // constructor code
        sprNum = spr;
        sheet = new HeroSheet(0,0);
        ts = tileSize;
        anim = new Array();
        animCount = 0;
        animTime = 0;
        dist = 0;
        xtile = xt;
        ytile = yt;
    }
在TBG引擎类中,这在prepare game函数中

animatedObjects = new Array();
hero = new Hero(0, 24, 2, 1);
animatedObjects.push(hero);
private function animateSprite ():void 
    {
        //run through all objects needing the animation
        for (var n:int = 0; n < animatedObjects.length; n++) 
        {
            var ob:Object = animatedObjects[n];
            if(ob.anim.length > 0)
            {
                //add 1 to time counter
                ob.animTime++;
                var reachedEnd:Boolean = false;
                //check if the time has counted up
                if(ob.animTime == ob.anim[ob.animCount + 1])
                {
                    //add to current image counter
                    ob.animCount += 2;
                    //check if end of animation is reached
                    if(ob.animCount == ob.anim.length)
                    {
                        //reset to start
                        ob.animCount = 0;
                    }
                    //check if its tile or separate object like hero
                    if(ob.bmp == null)
                    {
                        //clear the current tile image
                        var rect:Rectangle = new Rectangle(ob.xt * ts, ob.yt * ts, ts, ts);
                        tilesBmp.bitmapData.fillRect (rect, 0x00000000);
                        //change the image if time is right
                        ob.s = ob.anim[ob.animCount];
                        drawTile (ob.baseSpr, ob.xt, ob.yt);
                        drawTile (ob.s, ob.xt, ob.yt);

                    }else
                    {
                        //its hero
                        ob.sprNum = ob.anim[ob.animCount];
                        ob.bmp.bitmapData = getImageFromSheet (ob.sprNum, ob).bitmapData;
                    }
                    //reset animation timer
                    ob.animTime = 0;
                }
            }
        }
    }
下面是“每帧”功能,处理移动并触发英雄移动

var moveOb:Object = new Object();
        //find if any movement key is down
        for each (var keyOb in keys) 
        {
            //yep, arrow key is down
            if (keyOb.down == true) 
            {
                //check if tile is walkable
                if (getMyCorners(hero.x + keyOb.dirx * hero.speed, hero.y + keyOb.diry * hero.speed, hero) == true) {
                    moveOb = keyOb;
                }else
                {
                    //we have hit the wall, place near it
                    if(keyOb.dirx < 0)
                    {
                        hero.x = hero.xtile * ts;
                    }else if(keyOb.dirx > 0)
                    {
                        hero.xtile = Math.floor((hero.x + hero.speed) / ts);
                        hero.x = (hero.xtile + 1) * ts - hero.ts;
                    }else if(keyOb.diry < 0)
                    {
                        hero.y = hero.ytile * ts;
                    }else if(keyOb.diry > 0)
                    {
                        hero.ytile = Math.floor((hero.y + hero.speed) / ts);
                        hero.y = (hero.ytile + 1) * ts - hero.ts;
                    }
                    moveOb.dirx = 0;
                    moveOb.diry = 0;
                    moveOb.sprNum = keyOb.sprNum;
                    moveOb.anim = [];
                    //try to move hero around the wall tiles
                    if(keyOb.dirx != 0)
                    {
                        var ytc:int = Math.floor((hero.y + hero.ts/2) / ts);
                        if(isWalkable(hero.xtile + keyOb.dirx, ytc)){
                            //align vertically
                            var centerY:int = ytc * ts + (ts - hero.ts) / 2;
                            if(hero.y > centerY)
                            {
                                //move up
                                hero.y--;
                                moveOb.anim = keyOb.anim;
                            }else if(hero.y < centerY)
                            {
                                //move down
                                hero.y++;
                                moveOb.anim = keyOb.anim;
                            }
                        }
                    }else
                    {
                        var xtc:int = Math.floor((hero.x + hero.ts/2) / ts);
                        if(isWalkable(xtc, hero.ytile + keyOb.diry))
                        {
                            //align horisontal
                            var centerX:int = xtc * ts + (ts - hero.ts) / 2;
                            if(hero.x > centerX)
                            {
                                //move left
                                hero.x--;
                                moveOb.anim = keyOb.anim;
                            }else if(hero.x < centerX)
                            {
                                hero.x++;
                                moveOb.anim = keyOb.anim;
                            }
                        }
                    }
                }
                break;
            }
        }
        if(moveOb.dirx != null)
        {
            //move the hero
            moveObject (hero, moveOb);
        }else
        {
            hero.anim = [];
        }
        //animate the objects
            animateSprite ();
var moveOb:Object=new Object();
//查找是否有任何移动键已按下
对于每个(var keyOb in键)
{
//是的,箭头键是向下的
如果(keyOb.down==true)
{
//检查瓷砖是否可行走
如果(getMyCorners(hero.x+keyOb.dirx*hero.speed,hero.y+keyOb.diry*hero.speed,hero)=true){
moveOb=keyOb;
}否则
{
//我们撞到墙了,靠近墙的地方
if(keyOb.dirx<0)
{
hero.x=hero.xtile*ts;
}else if(keyOb.dirx>0)
{
hero.xtile=Math.floor((hero.x+hero.speed)/ts);
hero.x=(hero.xtile+1)*ts-hero.ts;
}else if(keyOb.diry<0)
{
hero.y=hero.ytile*ts;
}否则如果(keyOb.diry>0)
{
hero.ytile=Math.floor((hero.y+hero.speed)/ts);
hero.y=(hero.ytile+1)*ts-hero.ts;
}
moveOb.dirx=0;
moveOb.diry=0;
moveOb.sprNum=keyOb.sprNum;
moveOb.anim=[];
//试着在墙砖周围移动英雄
if(keyOb.dirx!=0)
{
变量ytc:int=Math.floor((hero.y+hero.ts/2)/ts);
if(可行走(hero.xtile+keyOb.dirx,ytc)){
//垂直对齐
var centerY:int=ytc*ts+(ts-hero.ts)/2;
如果(hero.y>centerY)
{
//上移
英雄;
moveOb.anim=keyOb.anim;
}else if(hero.ycenterX)
{
//向左移动
英雄x;
moveOb.anim=keyOb.anim;
}否则如果(hero.x
最后是animateSprite函数

animatedObjects = new Array();
hero = new Hero(0, 24, 2, 1);
animatedObjects.push(hero);
private function animateSprite ():void 
    {
        //run through all objects needing the animation
        for (var n:int = 0; n < animatedObjects.length; n++) 
        {
            var ob:Object = animatedObjects[n];
            if(ob.anim.length > 0)
            {
                //add 1 to time counter
                ob.animTime++;
                var reachedEnd:Boolean = false;
                //check if the time has counted up
                if(ob.animTime == ob.anim[ob.animCount + 1])
                {
                    //add to current image counter
                    ob.animCount += 2;
                    //check if end of animation is reached
                    if(ob.animCount == ob.anim.length)
                    {
                        //reset to start
                        ob.animCount = 0;
                    }
                    //check if its tile or separate object like hero
                    if(ob.bmp == null)
                    {
                        //clear the current tile image
                        var rect:Rectangle = new Rectangle(ob.xt * ts, ob.yt * ts, ts, ts);
                        tilesBmp.bitmapData.fillRect (rect, 0x00000000);
                        //change the image if time is right
                        ob.s = ob.anim[ob.animCount];
                        drawTile (ob.baseSpr, ob.xt, ob.yt);
                        drawTile (ob.s, ob.xt, ob.yt);

                    }else
                    {
                        //its hero
                        ob.sprNum = ob.anim[ob.animCount];
                        ob.bmp.bitmapData = getImageFromSheet (ob.sprNum, ob).bitmapData;
                    }
                    //reset animation timer
                    ob.animTime = 0;
                }
            }
        }
    }
私有函数animateSprite():void
{
//运行所有需要动画的对象
for(var n:int=0;n0)
{
//将1添加到时间计数器
ob.animTime++;
var-reachedEnd:Boolean=false;
//检查时间是否已到
如果(ob.animTime==ob.anim[ob.animtount+1])
{
//添加到当前图像计数器
ob.animCount+=2;
//检查动画结束是否为rea