Search code examples
actionscript-3flashadobeflash-cs4flash-cs3

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


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;
}


Solution

  • 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;
    }