Ok, I hope somebody can help. What am I doing wrong? I’m a total newb at Flash, but I’d really like to make this work. I’ve been working with an example of code I’ve found, trying to make my character respond to key input, but when I test the movie it just cycles though all of the animations and doesn’t respond to key presses at all. I know I did something dumb, but I’m not savvy enough to see it. Any help would be greatly appreciated, and thank you in advance!
CODE:
package
{
//These are the imported files. * is a wildcard meaning that everything in that section can be used
import flash.display.MovieClip;
import flash.ui.;
import flash.events.;
import Noir;
//This is the class function that holds all the functions and methods
public class Remnance extends MovieClip
{
public var noir:Noir;
public var keyPressed:uint;
public var leftKeyIsDown:Boolean;
public var rightKeyIsDown:Boolean;
public var upKeyIsDown:Boolean;
public var downKeyIsDown:Boolean;
private var speed:Number = 10;
/*constructor function must be the same name as the file name.
This function runs at the start of the script*/
public function Remnance()
{
//setting the variables
leftKeyIsDown = false;
rightKeyIsDown = false;
upKeyIsDown = false;
downKeyIsDown = false;
//adding an instance of the hero to the stage
noir = new Noir();
addChild(noir);
noir.x = 100;
noir.y = 500;
noir.scaleX = 1;
noir.gotoAndStop("Forward Stand and Blink");
//creating event listeners for the keys being pressed down
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressKey);
}
// a function that looks to see if the right, left, up or down key is pressed
function pressKey(event:KeyboardEvent):void
{
keyPressed = event.keyCode;
if (keyPressed == Keyboard.RIGHT)
{
rightKeyIsDown = true;
noir.gotoAndPlay("R Prf Walk");
}
if (keyPressed == Keyboard.LEFT)
{
leftKeyIsDown = true;
noir.gotoAndPlay("L Prf Walk");
}
if (keyPressed == Keyboard.UP)
{
upKeyIsDown = true;
noir.gotoAndPlay("Bkwd Walk");
}
if (keyPressed == Keyboard.DOWN)
{
downKeyIsDown = true;
noir.gotoAndPlay("F Walk");
}
noir.addEventListener(Event.ENTER_FRAME, moveNoir);
}
// a function that check to see if the key has been released
function releaseKey(event:KeyboardEvent):void
{
var thisKey:uint = event.keyCode;
if (thisKey == Keyboard.RIGHT)
{
rightKeyIsDown = false;
noir.gotoAndStop("Right Profile Stand and Blink");
}
if (thisKey == Keyboard.LEFT)
{
leftKeyIsDown = false;
noir.gotoAndStop("Left Profile Stand");
}
if (thisKey == Keyboard.UP)
{
upKeyIsDown = false;
noir.gotoAndStop("Backwards Stand");
}
if (thisKey == Keyboard.DOWN)
{
downKeyIsDown = false;
noir.gotoAndStop("Forward Stand and Blink");
}
}
//a function that moves the hero based on the speed variable set earlier
function moveNoir(event:Event):void
{
if (rightKeyIsDown && noir.x < stage.width - noir.width)
{
noir.x += speed;
noir.scaleX = 1;
}
if (leftKeyIsDown && noir.x > 0 + noir.width )
{
noir.x -= speed;
noir.scaleX = -1;
}
if (upKeyIsDown && noir.y > 220 + noir.height)
{
noir.y -= speed;
noir.scaleY = 1
}
if (downKeyIsDown && noir.y < stage.height - 10)
{
noir.y += speed;
noir.scaleY = -1
}
}
}
}