Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualHappyCoder

Posted 20 March 2013 - 10:50 AM

I would have to confirm this but I would think that even after removing the script tag the code in it would still be defined and would continue to run. Also, trying to rely on loading and unloading a source file to control behavior sounds a little shaky to me. I would search for a way to load all of your source at the beginning then have another way to control what code is active.

I am assuming you are using the same names for functions in the different source files, so loading both at the same time would cause conflicts. If that is so the following might work well for you.

game.js
// create the game one object
var GameOne = {}

// add all the functions for game one into 
// the GameOne object
GameOne.updateGame = function(){
  ctxBg.drawImage(startmeny, 0, 0); 
}

// then add these
GameOne.becomeActive = function() {
   // add event listeners, or do other game setup
}

GameOne.resignActive = function() {
   // remove event listeners, do any cleanup
}

game2.js
// do the same thing for game two, only with whatever logic you want there
var GameTwo = {}
GameTwo.updateGame = function(){
   // game logic
}
GameTwo.becomeActive = function() {
   // add event listeners, or do other game setup
}

GameTwo.resignActive = function() {
   // remove event listeners, do any cleanup
}
Now each game is defined in a separate object so in your main game loop code you could have something like this
var activeScene = null;

function setActiveScene(game)
{
    // tell the active game it is going away
    if (activeScene)
        activeScene.resignActive();

    activeScene = game;

    // alert the active game it is now active
    if (game)
        game.becomeActive();
}

// set the starting scene
setActiveScene(GameOne);

function loop() {
     // update the active game
     activeScene.updateGame();  
     requestAnimFrame(loop);
}

I would give more meaningful names to your GameOne and GameTwo object. I just used those names for this example. Perhaps something like MainMenu, Level1, ect... Then whenever you want to change to another scene you simply call setActiveScene(Level1)

#1HappyCoder

Posted 20 March 2013 - 10:49 AM

I would have to confirm this but I would think that even after removing the script tag the code in it would still be defined and would continue to run. Also, trying to rely on loading and unloading a source file to control behavior sounds a little shaky to me. I would search for a way to load all of your source at the beginning then have another way to control what code is active.

I am assuming you are using the same names for functions in the different source files, so loading both at the same time would cause conflicts. If that is so the following might work well for you.

game.js
// create the game one object
var GameOne = {}

// add all the functions for game one into 
// the GameOne object
GameOne.updateGame = function(){
  ctxBg.drawImage(startmeny, 0, 0); 
}

// then add these
GameOne.becomeActive = function() {
   // add event listeners, or do other game setup
}

GameOne.resignActive = function() {
   // remove event listeners, do any cleanup
}

game2.js
// do the same thing for game two, only with whatever logic you want there
var GameTwo = {}
GameTwo.updateGame = function(){
   // game logic
}
GameTwo.becomeActive = function() {
   // add event listeners, or do other game setup
}

GameTwo.resignActive = function() {
   // remove event listeners, do any cleanup
}
Now each game is defined in a separate object so in your main game loop code you could have something like this
var games = [GameOne, GameTwo];
var activeScene = null;

function setActiveScene(game)
{
    // tell the active game it is going away
    if (activeScene)
        activeScene.resignActive();

    activeScene = game;

    // alert the active game it is now active
    if (game)
        game.becomeActive();
}

// set the starting scene
setActiveScene(GameOne);

function loop() {
     // update the active game
     activeScene.updateGame();  
     requestAnimFrame(loop);
}

I would give more meaningful names to your GameOne and GameTwo object. I just used those names for this example. Perhaps something like MainMenu, Level1, ect...

PARTNERS