Im trying to make one of these memorization games, where u need to flip 2 cards over and try to match the images on the other side otherwise they both turn back over.
My code is working fine, except that I need to add a delay/pause before the 2 cards get flipped back over if no match is made. I'm trying to use $timeout
, but i'm getting an error: TypeError: Cannot set property 'isFlipped' of undefined
HTML
<body ng-controller="MainCtrl as main">
<figure ng-class="{true: 'flipped', false: 'not-flipped'}[card.isFlipped]" class="card"
ng-repeat="card in cards" ng-click="flipCard(card.id, card.pair)">
<img ng-src="img/cardback1.png" class="back"></img>
<img ng-src="{{card.img}}" class="front"></img>
</figure>
</body>
MyCtrl
angular.module('CardApp').controller('MainCtrl', ['$scope', '$timeout', function($scope, $timeout) {
$scope.cards = [
{
img: 'img/chantal1.jpg',
id: 1,
isFlipped: false,
pair: 1,
matched: false,
},
{
img: 'img/chantal1.jpg',
id: 2,
isFlipped: false,
pair: 1,
matched: false,
},
{
img: 'img/chantal2.jpg',
id: 3,
isFlipped: false,
pair: 2,
matched: false,
},
{
img: 'img/chantal2.jpg',
id: 4,
isFlipped: false,
pair: 2,
matched: false,
},
{
img: 'img/chantal3.jpg',
id: 5,
isFlipped: false,
pair: 3,
matched: false,
},
{
img: 'img/chantal3.jpg',
id: 6,
isFlipped: false,
pair: 3,
matched: false,
},
];
$scope.unflipCards = function(k, i) {
$scope.cards[k].isFlipped = false;
$scope.cards[i].isFlipped = false;
};
$scope.flipCard = function(id, pair) {
var cards = $scope.cards;
for(var i = 0, j = cards.length; i < j; i++) {
// find card id in cards that is not matched
if(cards[i].id == id && cards[i].matched == false) {
// flip card
cards[i].isFlipped = !cards[i].isFlipped;
// check if any other cards are flipped
for(var k = 0, j; k < j; k++) {
// if we find another card that is flipped and is not yet matched
if(cards[k].isFlipped == true && cards[k].matched == false && cards[k].id != id) {
// check if this card is a pair with the current card
if(cards[k].pair == pair) {
// set matched to true
cards[k].matched = true;
cards[i].matched = true;
} else {
$timeout(function(){$scope.unflipCards(k, i)}, 1000); // undefined error
//$scope.unflipCards(k, i); // works
}
}
}
}
}
};
}]);
I will try to reproduct in JSFiddle now