1

我正在尝试将卡片添加到 Deck 对象中的数组中,但由于某种原因 push() 失败。我早些时候有这个工作,但在做了一些改变之后,我实际上把它搞砸了。(“testX”写入用于调试目的)

<!DOCTYPE HTML>
<html>
<head>
<style> 
  #myCanvas {
    border: 1px solid #9C9898;
  }
  body {
    margin: 0px;
    padding: 0px;
  }
</style>
<script type="text/javascript">
<!--
//draws the game
window.onload = function() { 
    var canvas = document.getElementById("myCanvas"); 
    var context = canvas.getContext("2d"); 

    //draws the rectangles that will be the card        
    var cardHeight = 125,
        cardWidth = 80;

    context.beginPath();  

    //draws the top row of 5 cards  
    for(var x=0;x<5;x++){
        context.rect(10+(x*(cardWidth+10)),10,cardWidth,cardHeight);
    }

    //draws the bottom row of 5 cards
    for(x=0;x<5;x++){
        context.rect(10+(x*(cardWidth+10)),150,cardWidth,cardHeight);
    }

    //draws the 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 the unshuffled deck (loadedDeck) once to make the 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 are failing though this is the first)
            this.loadedDeck.push(x+" Clubs");
            this.loadedDeck.push(x+" Hearts");
            this.loadedDeck.push(x+" Diamonds");
        }
        document.write(this.loadedDeck);
    };
    this.loadTheDeck(); //creates the unshuffled deck when the Deck is instantiated


    //resets the deck and randomizes
    this.shuffle = function(){ //method 

        //creates the real deck
        this.realDeck = this.loadedDeck;

        //write shuffle function
    };
    this.shuffle(); //shuffles the 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>

这是代码的演示:http: //jsfiddle.net/NfmsR/2/

4

3 回答 3

4

当您运行此行时:

this.loadedDeck.push(x+" Spades");

您正在使用this.loadedDeck数组。您是否将其定义为Deck对象的一部分?没有:

var loadedDeck = new Array(),
    realDeck;

将声明更改为此,它应该可以工作:

this.loadedDeck = []; // I'd use [] instead of new Array().
this.realDeck = [];

正如@j08691 指出的那样,您也需要更改realDeckthis.realDeck,因为您在此处以类似方式调用它:

this.shuffle = function(){ //method 

    //creates the real deck
    this.realDeck = this.loadedDeck;

    //write shuffle function
};
于 2012-06-04T17:11:04.030 回答
2

您指的是“loadedDeck”,就好像它是“Deck”对象的属性一样。它不是; 它只是构造函数闭包的局部变量。

只需将其称为“loadedDeck”,看看是否有帮助。“realDeck”也是如此。

this.loadTheDeck = function(){ //method
    for(var x=1;x<=13;x++){ 
        loadedDeck.push(x+" Spades"); //<---issue line (all 4 are failing though this is the first)
        loadedDeck.push(x+" Clubs");
        loadedDeck.push(x+" Hearts");
        loadedDeck.push(x+" Diamonds");
    }

摆脱滥用document.write调试的习惯也是一个好主意。也不要忘记用var!!声明“x”之类的东西

于 2012-06-04T17:08:44.267 回答
0

使用 访问的Deck对象this没有“ loadedDeck”-属性。loadedDeck是一个局部变量。只需删除“ this.”。

于 2012-06-04T17:11:23.557 回答