jmtaylor
01-19-2012, 11:07 PM
Hey, I've been making a pairs game in javascript with jquery and I've been fiddling about with it but now suddenly the cards have disappeared.
My code is below. I'm pretty stumped, can someone let me know what's going on please?
Thanks
// JavaScript Document
// Pairs game
$(document).ready(function() {
// Sets up variables, sets up image location, size of each card and arrays for each card's picture and position
var imageFiles = 'images/';
var cards = ['card1.jpg', 'card2.jpg', 'card3.jpg', 'card4.jpg', 'card5.jpg', 'card6.jpg'];
var cardSize = 100;
var rows = 3;
var cols = 4;
var posns = [];
var rowsByCols = rows*cols;
// Function that checks for num in posns array
function checkArray(array, num) {
for(i = 0; i < array.length; i++) {
if(array[i] == num) return true;
}
return false;
}
// For loop that creates random unique positions for the cards and then stores them within the posns array
for(i = 0; i < rowsByCols; i++) {
while(true) {
var ranNum = Math.floor(Math.random() * (rowsByCols));
if(!checkArray(posns, ranNum)) {
posns[i] = ranNum;
break;
}
}
// For loop that makes sure then card is the right size then adds a picture to the card at the position specified in the posns array
}
for(i = 0; i < posns.length; i++) {
if(posns[i] >= cards.length) {
posns[i] = posns[i] - cards.length;
}
$('#cards').append('<div class="picture"><img src="' + imageFiles + cards[posns[i]] + '" alt="" /></div>');
}
$('.picture').hover(function() { //apply the mouse over
$(this).css('background-position', '0 -100px');
}, function() { //mouse out
$(this).css('background-position', '0 0');
});
// Setup variable for how many cards have been selected. Function for fading in selected card.
var selected = 1;
$('.picture').click(function() {
if($(this).children().css('display') != 'none') return false;
var step = parseInt($('#information strong').text()) + 1; //add a step
$('#information strong').text(step); //update at informations
$('#steps').text(step); //update at the sidebar
if(selected != 2) {
$(this).children().fadeIn('fast');
selected++;
}
// If 2 cards are selected, stores urls of the picture of each card selected.
else if(selected == 2) {
$(this).children().fadeIn('fast', function() {
var card1; var card2;
$('.picture img').not('.found').each(function() {
if($(this).css('display') != 'none') {
if(!card1) card1 = $(this).attr('src');
else card2 = $(this).attr('src');
}
});
// Compare urls of the two selected cards and if they match adds a class so that the pair is 'out'
if(card1 == card2) {
$(".picture img:visible").each(function() {
$(this).addClass('found');
});
}
// If pair doesn't match then they fade out
else {
$('.picture img').not('.found').fadeOut('slow');
}
// If no hidden pairs are left the game ends
if($('.picture img:hidden').css('display') != 'none') {
$('#cards').fadeOut('slow');
}
selected = 1;
});
}
});
});
My code is below. I'm pretty stumped, can someone let me know what's going on please?
Thanks
// JavaScript Document
// Pairs game
$(document).ready(function() {
// Sets up variables, sets up image location, size of each card and arrays for each card's picture and position
var imageFiles = 'images/';
var cards = ['card1.jpg', 'card2.jpg', 'card3.jpg', 'card4.jpg', 'card5.jpg', 'card6.jpg'];
var cardSize = 100;
var rows = 3;
var cols = 4;
var posns = [];
var rowsByCols = rows*cols;
// Function that checks for num in posns array
function checkArray(array, num) {
for(i = 0; i < array.length; i++) {
if(array[i] == num) return true;
}
return false;
}
// For loop that creates random unique positions for the cards and then stores them within the posns array
for(i = 0; i < rowsByCols; i++) {
while(true) {
var ranNum = Math.floor(Math.random() * (rowsByCols));
if(!checkArray(posns, ranNum)) {
posns[i] = ranNum;
break;
}
}
// For loop that makes sure then card is the right size then adds a picture to the card at the position specified in the posns array
}
for(i = 0; i < posns.length; i++) {
if(posns[i] >= cards.length) {
posns[i] = posns[i] - cards.length;
}
$('#cards').append('<div class="picture"><img src="' + imageFiles + cards[posns[i]] + '" alt="" /></div>');
}
$('.picture').hover(function() { //apply the mouse over
$(this).css('background-position', '0 -100px');
}, function() { //mouse out
$(this).css('background-position', '0 0');
});
// Setup variable for how many cards have been selected. Function for fading in selected card.
var selected = 1;
$('.picture').click(function() {
if($(this).children().css('display') != 'none') return false;
var step = parseInt($('#information strong').text()) + 1; //add a step
$('#information strong').text(step); //update at informations
$('#steps').text(step); //update at the sidebar
if(selected != 2) {
$(this).children().fadeIn('fast');
selected++;
}
// If 2 cards are selected, stores urls of the picture of each card selected.
else if(selected == 2) {
$(this).children().fadeIn('fast', function() {
var card1; var card2;
$('.picture img').not('.found').each(function() {
if($(this).css('display') != 'none') {
if(!card1) card1 = $(this).attr('src');
else card2 = $(this).attr('src');
}
});
// Compare urls of the two selected cards and if they match adds a class so that the pair is 'out'
if(card1 == card2) {
$(".picture img:visible").each(function() {
$(this).addClass('found');
});
}
// If pair doesn't match then they fade out
else {
$('.picture img').not('.found').fadeOut('slow');
}
// If no hidden pairs are left the game ends
if($('.picture img:hidden').css('display') != 'none') {
$('#cards').fadeOut('slow');
}
selected = 1;
});
}
});
});