I google a lot about this and i understand that there is ways of copying arrays and use them without affecting the original one but i did have tried a lot of things and nothing seems to work on my specific lines of code.
I'm doing a simple memory game. When the user gets the pair correct, it would appear the image of the matched pair on a right screen showing all the matched images that user got. The image below shows that, but when i'm using a method to resize the image (pairs matched are smaller than the normal game cards), i get the card selected with a undefined cardState
.
Here is a bunch of code that may help you figuring out my problem:
checkCardSelection(_cardSelectionArray,_position){
var _cardArray = this.state.cardArray;
var _matchedcardArray = this.state.matchedcardArray;
console.log("Position:" +_position)
if(_cardSelectionArray[0].cardType === _cardSelectionArray[1].cardType){
var copy = _cardSelectionArray.slice(0,1)
_matchedcardArray.push(copy)
_matchedcardArray[_matchedcardArray.length-1].cardState = this.renderMatchedImage(_matchedcardArray[_matchedcardArray.length-1].cardType)
this.setState({ cardArray: _cardArray, matchedcardArray: _matchedcardArray });
renderMatchedImage(cardType) {
switch(cardType) {
case 'A':
return <img src={imagem1} alt="Raposa" width="49px" height="70px"/>;
case 'B':
return <img src={imagem2} alt="Cobra" width="49px" height="70px"/>;
case 'C':
return <img src={imagem3} alt="Galinha" width="49px" height="70px"/>;
// and so on...
MatchedCardArray
is an empty array that will get cards as soon as they get paired up
CardSelectionArray
is a double value array with the two pairs selected
Each card has a CardType
(A,B,C,D,...) and a CardState
("Invisible" or the returned image). They are randomized with a funcion. For example the CardArray
may be:
0: {cardType: "G", cardState: "INVISIBLE"}
1: {cardType: "B", cardState: "INVISIBLE"}
2: {cardType: "A", cardState: "INVISIBLE"}