For some odd reason when I try to create a timer nothing happens, so when I refer to that timer I get an error because it doesn’t exist.
This is my as code
package com.mygame.dodge
{
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.events.*;
import flash.utils.getTimer;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.events.TimerEvent;
public class Dodge extends MovieClip
{
private var mcBob:Bob;
private var mcStartButton:StartButton;
static const mcBobDX = 200;
//stride var
private var makeStride:uint = 1;
//used to move char
private var leftArrow:Boolean = false;
private var rightArrow:Boolean = false;
private var upArrow:Boolean = false;
private var bottomArrow:Boolean = false;
public var lastTime:int;
private var mcBobX:Number = 263;
private var mcBobY:Number = 199;
private var myTimer:Timer = new Timer(1000,0);
myTimer.addEventListener(TimerEvent.TIMER, differentStrides);
myTimer.start();
public function Dodge()
{
mcStartButton = new StartButton ;
mcStartButton.x = 152;
mcStartButton.y = 164;
MovieClip(mcStartButton).gotoAndStop(1);
addChild(mcStartButton);
mcStartButton.addEventListener(MouseEvent.CLICK, startGame);
}
//start game
public function startGame(event:MouseEvent):void
{
//deleting and switching
removeChild(mcStartButton);
mcStartButton.removeEventListener(MouseEvent.CLICK, startGame);
mcBob = new Bob();
mcBob.gotoAndStop(2);
mcBob.x = mcBobX;
mcBob.y = mcBobY;
addChild(mcBob);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
addEventListener(Event.ENTER_FRAME, bobAnimation);
lastTime = getTimer();
}
//Animation
private function bobAnimation(event:Event):void
{
var timePassed:int = getTimer() - lastTime;
lastTime += timePassed;
//check boundries////////////
////////////////////////////////////
if (leftArrow == true)
{
if (mcBob.x < 18)
{
leftArrow = false;
mcBob.x = 23;
}
}
if (rightArrow == true)
{
if (mcBob.x > 530)
{
rightArrow = false;
mcBob.x = 530;
}
}
if (upArrow == true)
{
if (mcBob.y < 15)
{
upArrow = false;
mcBob.y = 13;
}
}
if (bottomArrow == true)
{
if (mcBob.y > 381)
{
bottomArrow = false;
mcBob.y = 381;
}
}
///move object
if (leftArrow == true)
{
mcBobX += (mcBobDX*timePassed/1000)*-1;
}
if (rightArrow == true)
{
mcBobX += (mcBobDX*timePassed/1000);
}
if (upArrow == true)
{
mcBobY += (mcBobDX*timePassed/1000)*-1;
}
if (bottomArrow == true)
{
mcBobY += (mcBobDX*timePassed/1000);
}
mcBob.x = mcBobX;
mcBob.y = mcBobY;
/////rotate char
if (upArrow == true)
{
mcBob.rotation = 0;
}
if (bottomArrow == true)
{
mcBob.rotation = 180;
}
if (leftArrow == true)
{
if (upArrow == true)
{
mcBob.rotation = 315;
}
else if (bottomArrow == true)
{
mcBob.rotation = 225;
}
else
{
mcBob.rotation = 270;
}
}
if (rightArrow == true)
{
if (upArrow == true)
{
mcBob.rotation = 45;
}
else if (bottomArrow == true)
{
mcBob.rotation = 135;
}
else
{
mcBob.rotation = 90;
}
}
}
function differentStrides(e:TimerEvent):void
{
if (leftArrow == true || rightArrow == true || upArrow == true || bottomArrow == true)
{
makeStride++;
/*if (makeStride == 3)
{
makeStride = 1;
}*/
mcBob.gotoAndStop(makeStride);
trace(makeStride);
}
}
function keyDown(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftArrow = true;
}
else if (event.keyCode == 39)
{
rightArrow = true;
}
else if (event.keyCode == 38)
{
upArrow = true;
}
else if (event.keyCode == 40)
{
bottomArrow = true;
}
}
function keyUp(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftArrow = false;
}
else if (event.keyCode == 39)
{
rightArrow = false;
}
else if (event.keyCode == 38)
{
upArrow = false;
}
else if (event.keyCode == 40)
{
bottomArrow = false;
}
}
}
}
This is me trying to create a timer
private var myTimer:Timer = new Timer(1000,0);
myTimer.addEventListener(TimerEvent.TIMER, differentStrides);
myTimer.start();