arrays - javascript push() failing -
i'm trying add cards array in deck object reason push() failing. had working earlier after making changes, have messed up. (the "testx" writes there debugging purposes)
<!doctype html> <html> <head> <style> #mycanvas { border: 1px solid #9c9898; } body { margin: 0px; padding: 0px; } </style> <script type="text/javascript"> <!-- //draws game window.onload = function() { var canvas = document.getelementbyid("mycanvas"); var context = canvas.getcontext("2d"); //draws rectangles card var cardheight = 125, cardwidth = 80; context.beginpath(); //draws top row of 5 cards for(var x=0;x<5;x++){ context.rect(10+(x*(cardwidth+10)),10,cardwidth,cardheight); } //draws bottom row of 5 cards for(x=0;x<5;x++){ context.rect(10+(x*(cardwidth+10)),150,cardwidth,cardheight); } //draws deck context.rect(10+5*cardwidth+65,(150-10)/2,cardwidth,cardheight); context.fillstyle = 'white'; context.fill(); context.linewidth = 2; context.strokestyle = 'black'; context.stroke(); }; function deck(){ //creates unshuffled deck (loadeddeck) once make shuffling process faster var loadeddeck = new array(), realdeck; this.loadthedeck = function(){ //method for(x=1;x<=13;x++){ this.loadeddeck.push(x+" spades"); //<---issue line (all 4 failing though first) this.loadeddeck.push(x+" clubs"); this.loadeddeck.push(x+" hearts"); this.loadeddeck.push(x+" diamonds"); } document.write(this.loadeddeck); }; this.loadthedeck(); //creates unshuffled deck when deck instantiated //resets deck , randomizes this.shuffle = function(){ //method //creates real deck this.realdeck = this.loadeddeck; //write shuffle function }; this.shuffle(); //shuffles deck when instantiated } document.write("test-1"); var mydeck = new deck(); document.write("test0"); document.write(this.realdeck); document.write("test1"); --> </script> </head> <body> <canvas id="mycanvas" height="300" width="600"></canvas> </body> </html> here demo of code: http://jsfiddle.net/nfmsr/2/
when run line:
this.loadeddeck.push(x+" spades"); you using this.loadeddeck array. have defined part of deck object? nope:
var loadeddeck = new array(), realdeck; change declaration , should work:
this.loadeddeck = []; // i'd use [] instead of new array(). this.realdeck = []; as @j08691 pointed out, need change realdeck this.realdeck because you're calling here:
this.shuffle = function(){ //method //creates real deck this.realdeck = this.loadeddeck; //write shuffle function };
Comments
Post a Comment