I'm making an HTML5 game and thinking 15 levels for it. I have an levels class that include level objects. This is the code part of it.
function levels(){
this.allLevels=[];
for (var i = 1; i <=15; i++) {
this.allLevels[this.allLevels.length]=new level(); //LEVEL OBJECTS CREATED INTO OBJECT
this.allLevels[this.allLevels.length-1].levelNumber=i; //LEVEL NUMBERS ARE SET
(i==1)? this.allLevels[this.allLevels.length-1].state='ZERO':this.allLevels[this.allLevels.length-1].state='LOCKED'; //LEVEL STATES ARE SET
}
}
As you might see, I have an allLevels array to keep the level information. So far so good everything works. After creating this class I added init and draw methods to this class. Here it is;
levels.prototype.initAll = function(){
for(var i=0;i<=15;i++){
this.allLevels[i].initAllCoor();
}
};
levels.prototype.draw=function(){
this.cleanCanvas();
for(var i=0;i<=15;i++){
this.allLevels[i].drawLevelIcon();
}
};
levels.prototype.cleanCanvas=function(){
ctxCanvasLevels.clearRect(0,0,800,570);
};
It has just three methods for now. The functions you see(initAllCoor and drawLevelIcon) are the level object's methods. When I called these methods, everything works.
You can say if everything works what are you asking. Although everything works smoothly, I'm getting error at the Chrome's console.
Uncaught TypeError: Cannot call method 'initAllCoor' of undefined
Uncaught TypeError: Cannot call method 'drawLevelIcon' of undefined
I can't understand, it says 'cannot call' but it does. Because of this errors I can't use console.log function, too.
When you create the levels:
for (var i = 1; i <=15; i++) {
In the "initAll" function:
for(var i=0;i<=15;i++){
There is never a level 0, yet the "initAll" function expects one.
It's not invalid or wrong , but it's somewhat weird to start filling a JavaScript array at position 1 instead of 0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.