第一个问题! NPC 步行周期随机化器按预期工作(边界确实需要调整),但不播放嵌套的步行动画? (AS3)

First question! NPC walk-cycle randomizer is working as intended (boundaries do need tweaking), but not playing the nested walking animations? (AS3)

差不多,我认为行走动画不能与 switch 语句一起使用?这就是它的样子,但随机发生器本身工作正常。我想做的是重写 mc 的工作代码,但是用 switch 语句中的案例(选择的随机行走方向)替换关键输入,并将重写的代码分别插入 NPC 的大脑(roshi)。

我设置代码的方式是 NPC "roshi" 是舞台上的电影片段。里面是单独的动画片段,每个动画片段都有每个行走动画的帧,但它们不会播放(例如 roshiRightStand、roshiRightWalk 等)。它总是卡在选定的帧上,无论我如何弄乱代码、括号或布尔值,它都不会真正进入动画片段。也许我没有声明或嵌套一些正确的东西或根本没有?动画是嵌套在另一个动画片段中的动画片段中的帧,名称似乎与属性匹配?不太确定如何声明它,但在某些时候我相信我可能已经让整个事情正常进行并把它搞砸了。如果有帮助,请留下我的旧剩余代码?任何输入将不胜感激!学习还算快。 :)

*有错误的代码(斜线):https://textuploader.com/108de

*代码清理干净:https://textuploader.com/108lw

*游戏上传(按原样反映代码;可在带插件的浏览器上播放):https://www.newgrounds.com/dump/item/e06224a5f9fd5645ce5a4604173f8bbd?fbclid=IwAR3HJdXMXEqxUN5TH2xaDvV82QBDmI0ewnVej1EQJFkZLb3RYuEK0dvMz74

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;


mc.gotoAndStop("standingRight");
toggle_btn.stop();


var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();

var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var mcSpeed:Number = 10;

var roshiTimer:Number = 0; //random roshi-cycle duration between 0-25
var roshiDuration:Number = Math.random() * 25;
var roshiFacing:Number = Math.floor(Math.random() * 4); //random # bwt 0-3 (ex. 4 is rounded down to 3)
var roshiSpeed:Number = 3; //roshi's walk speed


toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);


function togglePlay(event:MouseEvent):void
{
   (!played)? played = true : played = false;
   (played)? myChannel=mySound.play():SoundMixer.stopAll();

   (played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
    myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
 played = false;
 toggle_btn.gotoAndStop(1);
}


function keyDownHandler(keyEvent:KeyboardEvent):void
{
   if(keyEvent.keyCode == Keyboard.RIGHT)
   {
    rightPressed = true;
   }
   else if(keyEvent.keyCode == Keyboard.LEFT)
   {
    leftPressed = true;
   }
   else if(keyEvent.keyCode == Keyboard.DOWN)
   {
    downPressed = true;
   }
   else if(keyEvent.keyCode == Keyboard.UP)
   {
    upPressed = true;
   }
}

function keyUpHandler(keyEvent:KeyboardEvent):void
{
   if(keyEvent.keyCode == Keyboard.RIGHT)
   {
    rightPressed = false;
    mc.gotoAndStop("standingRight");
   }
   else if(keyEvent.keyCode == Keyboard.LEFT)
   {
 leftPressed = false;
 mc.gotoAndStop("standingLeft");
   }
   else if (keyEvent.keyCode == Keyboard.DOWN)
   {
       downPressed = false;
    mc.gotoAndStop("standingDown");
   }
   else if(keyEvent.keyCode == Keyboard.UP)
   {
    upPressed = false;
    mc.gotoAndStop("standingUp");
   }
}


function gameLoop(loopEvent:Event):void 
{  
 //MC's Movement Controls
    if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
 { 
       mc.gotoAndStop("walkingRight");
    }
    if(rightPressed && mc.currentLabel =="walkingRight")
    {
    if(mc.x < 800)
    {
          mc.x += mcSpeed;
    }

    else if (backenvironment.x > -650) //right world borderwall
    {
    backenvironment.x -= mcSpeed; 
    frontenvironment.x -= mcSpeed;
    }
    } 
 
    if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
    {
       mc.gotoAndStop("walkingLeft");
    }
    if(leftPressed && mc.currentLabel =="walkingLeft")
    {
    if(mc.x > 200)
    {
          mc.x -= mcSpeed;
    }
    else if (backenvironment.x < -130) //left world borderwall
    {
       backenvironment.x += mcSpeed;
       frontenvironment.x += mcSpeed;
    }
    }
 
    if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)  
 {
       mc.gotoAndStop("walkingUp");
    }
    if(upPressed && mc.currentLabel == "walkingUp")
    {
    if(mc.y > 200) //og 200
    {
          mc.y -= mcSpeed;10
       }
    else if (backenvironment.y < -10) //top world borderwall
    {
       backenvironment.y += mcSpeed;
       frontenvironment.y += mcSpeed;
    }
 }
    
    if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
    {
       mc.gotoAndStop("walkingDown");
    }
 
    if (downPressed && mc.currentLabel == "walkingDown")
    {
    if(mc.y < 485) //og 568 LESS MOVES MC UP CAMERA
    {
          mc.y += mcSpeed;
       }
    else if (backenvironment.y > -577) //bottom world borderwall og-577
       {
     backenvironment.y -= mcSpeed;
     frontenvironment.y -= mcSpeed;
       }
   }


   if(roshiTimer < roshiDuration)
   {
      switch(roshiFacing) //x=horozontal y=vertical +=right/up -=left/down
      {
   case 0 :
   roshi.gotoAndStop("roshiUpWalk");
   //roshi.addEventListener(Event.ENTER_FRAME)
   roshi.y -= roshiSpeed;
   break;
  
   case 1 :
   roshi.gotoAndStop("roshiDownWalk");
   roshi.y += roshiSpeed;
   break;
   
   case 2 :
   roshi.gotoAndStop("roshiLeftWalk");
   roshi.x -= roshiSpeed;
   break;
   
   case 3 :
   roshi.gotoAndStop("roshiRightWalk");
   roshi.x += roshiSpeed;
   break;
   }
      roshiTimer ++; 
   }
   
   if(roshiTimer > roshiDuration)
   {
      roshiDuration = Math.random() * 25; //25
      roshiFacing = Math.floor(Math.random() * 4); //4
      roshiTimer = 0; //greater than 0
   }
}

~由用户Organis解决。

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;


mc.gotoAndStop("standingRight");
toggle_btn.stop();


var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();

var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var mcSpeed:Number = 10;

var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3

addEventListener(Event.ENTER_FRAME, onRoshi);
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);


function togglePlay(event:MouseEvent):void
{
   (!played)? played = true : played = false;
   (played)? myChannel=mySound.play():SoundMixer.stopAll();

   (played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
    myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
 played = false;
 toggle_btn.gotoAndStop(1);
}


function keyDownHandler(keyEvent:KeyboardEvent):void
{
   if(keyEvent.keyCode == Keyboard.RIGHT)
   {
    rightPressed = true;
   }
   else if(keyEvent.keyCode == Keyboard.LEFT)
   {
    leftPressed = true;
   }
   else if(keyEvent.keyCode == Keyboard.DOWN)
   {
    downPressed = true;
   }
   else if(keyEvent.keyCode == Keyboard.UP)
   {
    upPressed = true;
   }
}

function keyUpHandler(keyEvent:KeyboardEvent):void
{
   if(keyEvent.keyCode == Keyboard.RIGHT)
   {
    rightPressed = false;
    mc.gotoAndStop("standingRight");
   }
   else if(keyEvent.keyCode == Keyboard.LEFT)
   {
 leftPressed = false;
 mc.gotoAndStop("standingLeft");
   }
   else if (keyEvent.keyCode == Keyboard.DOWN)
   {
       downPressed = false;
    mc.gotoAndStop("standingDown");
   }
   else if(keyEvent.keyCode == Keyboard.UP)
   {
    upPressed = false;
    mc.gotoAndStop("standingUp");
   }
}


function gameLoop(loopEvent:Event):void 
{  
 
    if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
 { 
       mc.gotoAndStop("walkingRight");
    }
    if(rightPressed && mc.currentLabel =="walkingRight")
    {
    if(mc.x < 800)
    {
          mc.x += mcSpeed;
    }

    else if (backenvironment.x > -650) 
    {
    backenvironment.x -= mcSpeed; 
    frontenvironment.x -= mcSpeed;
    }
    } 
 
    if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
    {
       mc.gotoAndStop("walkingLeft");
    }
    if(leftPressed && mc.currentLabel =="walkingLeft")
    {
    if(mc.x > 200)
    {
          mc.x -= mcSpeed;
    }
    else if (backenvironment.x < -130)
    {
       backenvironment.x += mcSpeed;
       frontenvironment.x += mcSpeed;
    }
    }
 
    if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)  
 {
       mc.gotoAndStop("walkingUp");
    }
    if(upPressed && mc.currentLabel == "walkingUp")
    {
    if(mc.y > 200) 
    {
          mc.y -= mcSpeed;10
       }
    else if (backenvironment.y < -10) 
    {
       backenvironment.y += mcSpeed;
       frontenvironment.y += mcSpeed;
    }
 }
    
    if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
    {
       mc.gotoAndStop("walkingDown");
    }
 
    if (downPressed && mc.currentLabel == "walkingDown")
    {
    if(mc.y < 485) 
    {
          mc.y += mcSpeed;
       }
    else if (backenvironment.y > -577) 
       {
     backenvironment.y -= mcSpeed;
     frontenvironment.y -= mcSpeed;
       }
   }

}


var RF:Array = 
[
    "roshiUpWalk", "roshiDownWalk",
    "roshiLeftWalk", "roshiRightWalk",
];

var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];

function onRoshi(e:Event):void
{
      roshiTimer ++; 

      if (roshiTimer > roshiDuration)
    {
        roshiDuration = 10 + Math.random() * 25;
        roshiTimer = 0;
  
        while (Roshi.currentLabel == RF[roshiFacing])
        {
            roshiFacing = Math.random() * 4;
        }

        Roshi.gotoAndStop(RF[roshiFacing]);
    }

    Roshi.x += RX[roshiFacing] * roshiSpeed;
    Roshi.y += RY[roshiFacing] * roshiSpeed;
}

好吧,我们还是试试吧。将其与 Roshi 图形一起放入单独的项目中。您的代码与此代码之间的主要区别是我不会在每一帧触发 Roshi.gotoAndStop,仅在 Roshi 改变方向时触发一次。

// Integers, because you don't have to round them.
var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3;

addEventListener(Event.ENTER_FRAME, onRoshi);

// Let's do this instead of switch.
var RF:Array = [
    "roshiUpWalk", "roshiDownWalk",
    "roshiLeftWalk", "roshiRightWalk",
];

var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];

function onRoshi(e:Event):void
{
    roshiTimer++;

    if (roshiTimer > roshiDuration)
    {
        roshiDuration = 10 + Math.random() * 25;
        roshiTimer = 0;

        // Let's make Roshi ALWAYS change direction
        // without occasionally proceeding to the same one.
        while (Roshi.currentLabel == RF[roshiFacing])
        {
            roshiFacing = Math.random() * 4;
        }

        Roshi.gotoAndStop(RF[roshiFacing]);
    }

    Roshi.x += RX[roshiFacing] * roshiSpeed;
    Roshi.y += RY[roshiFacing] * roshiSpeed;
}