Changing frames in a class
So I'm working on a pretty basic game at the moment. When I press the down key, I want the character (at the moment, just a square) to slide underneath obstacles. I have the standing animation as one frame and the sliding as another within the same class, but when I press the down key, it switches to the second frame yet another object on the first frame remains over top. I suspect that because I created the square like so:
//Create square
var newSquare:Number = 1;
var Square:square = new square;
Square.x = 50;
Square.y = 180;
stage.addChild(Square);
that it's constantly creating new squares overtop the one that's sliding. The code for the sliding is as follows:
//Slide
var down:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, _slideDown);
function _slideDown(e:KeyboardEvent):void {
if (e.keyCode == 40) {
down = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, _slideUp);
function _slideUp(e:KeyboardEvent):void {
if (e.keyCode == 40) {
down = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, _slide);
function _slide(e):void {
if (down == true) {
Square.gotoAndPlay(2);
}
if (down == false) {
Square.gotoAndPlay(1);
}
}
If I'm right about generating infinite squares, could someone help me fix it? I tried using a conditional and number variable and then changing the number when the square was created, but that didn't help. If I'm not right, any other help would be appreciated.
