Game Development Reference
In-Depth Information
Working with the control code
In our main control code, the first change we did was to rename
addStageObject
to
loadStageObject
and add a new
addStageObject
function. This was done to
improve the readability of the code, and we also moved the adding of
StageObject
to the
addStageObject
function.
Let's refer to our basic strategy once again. When we load our JSON model and if
the model has an associated texture file, then we first check to see whether that file
has already been loaded. If it has, then we simply get its key and assign it to the
textureIndex
property of
StageObject
and if not, then we load it, assign a unique
key to it, and then associate it with the
StageObject
.
To implement this strategy, we have created a
vartextureList=[];
array. The key
of the element in the array will be the name of the texture file, and the value would
be the unique key to the texture object in the array (
textures
) of the
Stage
class. The
following code loads the model and then its corresponding texture and assigns
a texture index for the texture:
function loadStageObject (url,location,rotationX,rotationY,rotationZ){
$.getJSON(url,function(data){
Var stageObject=new StageObject();
stageObject.loadObject(data);
if(stageObject.materialFile!=null){
if((textureList[stageObject.materialFile.trim()]===undefined)){
var currentDate=new Date();
var textureIndex=currentDate.getMilliseconds();
stageObject.textureIndex=textureIndex;
textureList[stageObject.materialFile.trim()]=textureIndex;
initTextures(stageObject.materialFile.trim(),textureIndex);
}
else{
stageObject.textureIndex=textureList[stageObject.
materialFile.trim()];
}
}
addStageObject(stageObject,location,rotationX,rotationY,rotationZ);
});
}