Copy link to clipboard
Copied
I am creating a game where the challange is to collect all the coins while avoiding obsticules and before the times runs out. What I am trying to do is when the game runs out of time or the player has died, The player simply presses the R key on their keyboard to reset the level they are playing on. With my attempt of coding I can get the game to reset and put all the collectables back in place but the problem I am having is that the camera that is following the character is not resetting correctly. The 2nd problem I am having is that the game is not resetting the variables/arrays so I can still open certain colour coded doors without having the keys or number amount of collected coins needed.
How can I fix this problem? Is there an easier way to create a reset function? Below is my attempt to try and fix it.
//========== resetting game ============
stage.addEventListener(KeyboardEvent.KEY_DOWN, resetKey);
function resetKey(event:KeyboardEvent):void {
if (event.keyCode == 82) {
var gameSpace:MovieClip = new MovieClip();
addChild(gameSpace);
gameSpace.x = -5 * 48 + 5;
gameSpace.y = -5 * 48 + 5;
gameSpace.addEventListener(Event.ENTER_FRAME, updateGame);
var levelMask:Mask = new Mask();
addChild(levelMask);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, kuHandler);
var north,south,east,west; // to control motion of gameSpace
north = south = east = west = false;
var hasRedKey,hasGreenKey,hasYellowKey,hasBlueKey;
hasRedKey = hasGreenKey = hasYellowKey = hasBlueKey = false;
var levelStart: Array = new Array();
var displayMap:Array= new Array();
var cols:int;
var rows:int;
var chip:Chip = new Chip();
setUpLevelMap();
drawLevelMap();
switch (levelStart) {
case EMPTY :
case ICCHIP :
case HINT:
case KEY_RED:
case KEY_BLUE:
case KEY_YELLOW:
case KEY_GREEN:
case DOOR_BLUE:
case DOOR_RED:
case DOOR_YELLOW:
case DOOR_GREEN:
}
}
}
all variables with "var" inside a function body will be local to the function and will NOT change a same named variable outside the function. ie, use something like:
...
//========== resetting game ============
stage.addEventListener(KeyboardEvent.KEY_DOWN, resetKey);
function resetKey(event:KeyboardEvent):void {
if (event.keyCode == 82) {
gameSpace = new MovieClip();
addChild(gameSpace);
gameSpace.x = -5 * 48 + 5;
gameSpace.y = -5 * 48 + 5;gameSpace.addEventListener(Event.ENTER_FRAME, updateGame);
va
Copy link to clipboard
Copied
all variables with "var" inside a function body will be local to the function and will NOT change a same named variable outside the function. ie, use something like:
//========== resetting game ============
stage.addEventListener(KeyboardEvent.KEY_DOWN, resetKey);
function resetKey(event:KeyboardEvent):void {
if (event.keyCode == 82) {
gameSpace = new MovieClip();
addChild(gameSpace);
gameSpace.x = -5 * 48 + 5;
gameSpace.y = -5 * 48 + 5;gameSpace.addEventListener(Event.ENTER_FRAME, updateGame);
var levelMask:Mask = new Mask();
addChild(levelMask);myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();stage.addEventListener(KeyboardEvent.KEY_DOWN, kdHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, kuHandler);
north = south = east = west = false;
hasRedKey = hasGreenKey = hasYellowKey = hasBlueKey = false;
levelStart= new Array();
displayMap= new Array();
cols=0
rows=0chip = new Chip();
setUpLevelMap();
drawLevelMap();switch (levelStart) {
case EMPTY :
case ICCHIP :
case HINT:
case KEY_RED:
case KEY_BLUE:
case KEY_YELLOW:
case KEY_GREEN:
case DOOR_BLUE:
case DOOR_RED:
case DOOR_YELLOW:
case DOOR_GREEN:
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now