0

Pretty much, the walking animations won't work with the switch statement I think? That's what it looks like, but the randomizer itself works fine. What I wanted to do was rewrite the working code for the mc, but replace the key inputs with the cases (random walking directions chosen) from the switch statement and plug that rewritten code in separately into the NPC's brain (roshi).

The way I have the code set up is the NPC "roshi" is a movieclip on the stage. Inside are individual movieclips each with frames for each walking animation but they won't play (ex. roshiRightStand, roshiRightWalk, etc). It always gets stuck on the chosen frame, and never actually enters the movieclip no matter how I mess with the code, brackets or booleans. Maybe I'm not declaring or nesting something right or at all? The animations are frames within a movieclip nested within another movieclip and the names seem to match up in the properties? Not quite sure how to declare it, but at some point I believe I may have had the whole thing working and messed it up. Left my old leftover code if it could help? Any input would be much appreciated! Learning fairly quick. :)

*The Code w/ mistakes (slashed out): https://textuploader.com/108de

*The Code cleaned up and tidy: https://textuploader.com/108lw

*Game Upload (reflects the code as is; playable on your browser w/ plugin): 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
   }
}

~Solved by user 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;
}
CodeBlue
  • 3
  • 3
  • I apologize for the lack of code snippets within the platform, I linked them instead because it's easier to read the whole engine the way. I will isolate the snippet in question but it's a fairly long block within the gameLoop. – CodeBlue Sep 25 '19 at 18:43
  • Without regarding the fact that you are asking to help you debug quite a big piece of code, the last one is nothing but commented lines and is not supposed to work at all. – Organis Sep 26 '19 at 00:12
  • Doesn't really answer my question any. I leave my mistakes visible, and they do not run on the compiler. All the code is visible and I mentioned that the issue was isolated toward the loop; the NPC "roshi" is also labeled pretty clearly in the given code. The code runs fine, it's the movieclip walking animations nested within the npc movieclip on the stage. – CodeBlue Sep 26 '19 at 00:18
  • If you could at the very least point me in the right direction, you'd be a lifesaver though. It's not as large when it's trim, I swear lol. I'll try a book on coding game AI, if you know any suggestions? – CodeBlue Sep 26 '19 at 00:21
  • I'll trim it and upload a cleaner link to make it easier, thanks for the suggestion. – CodeBlue Sep 26 '19 at 00:25
  • *Update: Cleaned it up like you suggested, much easier on the eyes. https://textuploader.com/108lw – CodeBlue Sep 26 '19 at 00:34
  • I've looked and looked, and it turns out a couple others have gotten stuck on this as well to no avail. One user suggested that flash itself can't dig that deep for a class, but I'm wondering if I could either refer to one of the cases or do so individually within the cases themselves (0-3)? – CodeBlue Sep 26 '19 at 00:55
  • Ugh. I read the Roshi script over and over, and it seems pretty much sensible to me. I can suggest some stylistic improvements, but the logic is totally fine, I think. – Organis Sep 26 '19 at 01:04
  • Dude I'm in the same boat, I can't find any fault with my code and my traces don't indicate any sort of mismatch... thank you so much though. I got it working once but messed up and didn't save, I was tired but I think I either implemented a frame_handle, boolean into the cases or some sort of odd instance reference (or maybe declaration). Maybe if I experiment with integers? – CodeBlue Sep 26 '19 at 01:09
  • He walks in random patterns, good. He faces the directions he walks towards, good. The animations still won't trigger though... I'm at my wits end. – CodeBlue Sep 26 '19 at 01:15

1 Answers1

0

Well, let's try anyway. Put this into separate project along with Roshi graphics. The main difference between your code and this that I don't trigger Roshi.gotoAndStop every frame, just once upon Roshi changing directions.

// 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;
}
Organis
  • 7,243
  • 2
  • 12
  • 14
  • That's all folks, this madman found a functional alternative. You're a hero dude; much obliged!!! I'll take down your username and give proper credit in the source/credits. – CodeBlue Sep 26 '19 at 02:06
  • @CodeBlue I'm glad that my wild guess helped : ) – Organis Sep 26 '19 at 02:10