I'm trying to write a function that only updates the score when the item being called for and the one being hit with the shopping cart are the same.
Post any code you think might be relevant (one fenced block per file)
const detectHit = () => {
for (let i = 0; i < fruitCollection.length; i++)
// console.log(fruitCollection[i])
if (
cart.x < fruitCollection[i].x + fruitCollection[i].width &&
cart.x + cart.width > fruitCollection[i].x &&
cart.y < fruitCollection[i].y + fruitCollection[i].height &&
cart.y + cart.height >= fruitCollection[i].y
){
fruitCollection[i].alive = false
pointsUpdate.innerText = `${score+=10}`
fruitCollection.splice(i, 1, i) //removes items that have been collided with from canvas
console.log("collision detected")
console.log(fruitCollection[i])
}
}
const matchingScore = () => {
if(currentItemIndex === fruitCollection[i]){
console.log("matchy matchy!")
}
}
matchingScore()
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
unexpected behavior: no "matchy matchy!" console log when the random item matches the item hit with the cart
What is your best guess as to the source of the problem?
fruitCollection.[i] probably has to be called in a different way in the matchingScore function.
What things have you already tried to solve the problem?
calling the matchingScore function in the game loop.
Setting fruitCollection[i] to a variable in detectHit then calling that variable in matchingScore.
What's the problem you're trying to solve?
I'm trying to write a function that only updates the score when the item being called for and the one being hit with the shopping cart are the same.
Post any code you think might be relevant (one fenced block per file)
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
unexpected behavior: no "matchy matchy!" console log when the random item matches the item hit with the cart
What is your best guess as to the source of the problem?
fruitCollection.[i] probably has to be called in a different way in the matchingScore function.
What things have you already tried to solve the problem?
calling the matchingScore function in the game loop. Setting fruitCollection[i] to a variable in detectHit then calling that variable in matchingScore.