javascript - Global variables undefined after using jQuery.getScript() to assign -


I have many global variables; var1, var2 , etc ...

I do not instantize these variables immediately, but when I later use jQuery I have a separate javascript file.

However; After I get getScript () ; And I am trying to get properties of my new immediate items;

I get the following error:

Unkit type error: Undefined property 'x' can not be read

Is there a reason? Or can not you instantiate global variable like this?

Code getScript () ; OpenOffice.org function () $ $ .getScript ('Objects.js', function () {// objects game screen = new game object (0,0,750,1500,' Art_asset / Game_screen / colaborate layout copy page ', 0); menu = new game object (0,0750,750, Art_Assets / main_menu / bkg_start2.png', 0); startBtn = new game object (50,50,65,160, 'Art_Assets / main_menu / Credit button = new game object (50,150, 65,160, 'Art_Assets / main_menu / btn_help.png', 'Art_Assets / main_menu / btn_helph.png') credits = New game objects (0,0,750,750, 'Art_Assets / credits.png', 0); // View station Station = new workstation ('Art_Assets / game_screen / workstation.png', 0, "sawView"); drillestation = new workstation ('Art_Assets / game_screen / workstation.png', 0, "drillView"); Bendstation = new workstation (' Art_Assets / game_screen /workstation.png'0,"bendView "); Weldstation = new workstation ('Art_Assets / game_screen / workstation.png', 0," weldView "); GrindStation = new workspace ('Art_Assets / game_screen / workstation.png', 0, "grindView"); PaintStation = New Workstation ('Art_assets / Game_screen / Workstation Pages', 0, "Paintview"); AssemblyStation = new workspace ('Art_Assets / game_screen / workstation.png', 0, "assemblyView"); Fabrication = new workstation ('Art_Assets / game_screen / workstation.png', 0, "clothing scene"); Sealestation = new workstation ('art_assets / game_screen / workstation page', 0, 'sealviewview'); Console.log (startBtn.x, "inside the startBtn X loader function"); Console.log ("stuff is going") loadImg (menu); LoadImg (startBtn); LoadImg (creditBtn); LoadImg (credit); LoadImg (sawstation); LoadImg (drillStation); LoadImg (gameScreen); LoadImg (bendStation); LoadImg (weldStation); LoadImg (grindStation); LoadImg (paintStation); LoadImg (assemblyStation); LoadImg (fabricStation); LoadImg (sewingStation); Station = [Sustestation, Drillstation, Bendstation, Weldstation, Grindstation, PaintStation, Assembly Station, Falestation, Sailestation]; For (var i = 0; i <9; i ++) {station [i] .position = i; } Desk = New game object (250550,128,256, 'Art_asset / game_screen / desk page', 0); LoadImg (desk); Office = new game object (750,0,750,750, 'art_asset / game_screen / office page', 0); LoadImg (office); })};

Main method:

  var main = function () {var now = date.now (); Var delta = now - then; FunctionLoadObjects (); window.addEventListener ('mousemove', tracker, wrong); console.log (startBtn.x, "function call startBtn.x"); Update (delta / 1000); to present(); So = now; RequestAnimationFrame (main);   

};

The error is found in the console line in the main method; Property can not be entered with x because startBtn is undefined; Although all buttons and other wars are defined at the top of the JS file

$ .getScript is an asynchronous function - its success < / Code> handler comes from the server after the contents of your script file - OK after main , while Console.log (startBtn.x, ... execute immediately .

  var function objects = functions (callback) {$ .getScript ('objects.js', function) {... your code ... callback ();})}}. var main = function () {var now = date.now (); Var delta = now - then; Functions () (functions () {// put All code here requires things from dynamically loaded scripts, such as: console.log (startBtn.x, "function call startBtn.x" outside);}) }}    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -