Commit cc8603af authored by Malsha Jayakody's avatar Malsha Jayakody

fix message errors

parent 5229c7c7
......@@ -150,50 +150,45 @@ const Game02Level01 = ({navigation, route}) => {
if (budget - item.cost >= 0) {
setBudget(budget - item.cost);
setCollectedItems([...collectedItems, item]);
if (item.type === 'healthy') {
setHealthyCost(prevCost => prevCost + item.cost);
} else {
setUnhealthyCost(prevCost => prevCost + item.cost);
}
// Use a setTimeout to ensure state updates have been applied before check
setTimeout(() => {
if (unhealthyCost > 2000) {
if (unhealthyCost + item.cost > 2000) { // ensure the current cost is added to total for the check
Alert.alert('Game lost!', 'Choose healthier foods. 🍇🍌🍉🥬🥕🍒',
[
{
text: 'OK',
onPress: () => {
navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS);
},
onPress: () => navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS),
},
]);
} else if (healthyCost > 2000) {
Alert.alert('Game won!',
} else if (healthyCost + item.cost > 2000) { // ensure the current cost is added to total for the check
Alert.alert('Game won!', 'Congratulations! You have made healthy choices.',
[
{
text: 'OK',
onPress: () => {
navigation.navigate(ROUTES.Game02ProgressLevel03);
},
onPress: () => navigation.navigate(ROUTES.Game02ProgressLevel03),
},
]);
backgroundMusic.current.setVolume(0.0);
}
}, 0);
} else {
Alert.alert('Not enough budget',
Alert.alert('Not enough budget', 'Please try to manage your budget better.',
[
{
text: 'OK',
onPress: () => {
navigation.navigate(ROUTES.CHOOSE_GAME);
},
onPress: () => navigation.navigate(ROUTES.CHOOSE_GAME),
},
]);
}
};
return (
<ScrollView style={styles.container}>
......
......@@ -81,44 +81,39 @@ const CravingsPuzzleGame02Level02 = ({navigation}) => {
};
const purchaseItem = item => {
// Check if the budget allows for the purchase
if (budget - item.cost >= 0) {
// Update the budget and purchased items
setBudget(budget - item.cost);
setPurchasedItems([...purchasedItems, item]);
// Update the cost tracking based on whether the item is essential
if (item.essential === true) {
setHealthyCost(prevCost => prevCost + item.cost);
} else {
setUnhealthyCost(prevCost => prevCost + item.cost);
}
setTimeout(() => {
if (unhealthyCost > 2000) {
Alert.alert('Game lost!', 'Choose essintial items!', [
{
text: 'OK',
onPress: () => {
navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS);
},
},
]);
} else if (healthyCost > 2000) {
Alert.alert('Game won!', [
{
text: 'OK',
onPress: () => {
navigation.navigate(ROUTES.Game02ProgressLevel06);
},
},
]);
backgroundMusic.current.setVolume(0.0);
}
}, 0);
// Delay is unnecessary, so directly execute the checks
if (unhealthyCost + item.cost > 2000) {
Alert.alert('Game lost!', 'Choose essential items!', [
{
text: 'OK',
onPress: () => navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS),
},
]);
} else if (healthyCost + item.cost > 2000) {
Alert.alert('Game won!', 'Congratulations!', [
{
text: 'OK',
onPress: () => navigation.navigate(ROUTES.Game02ProgressLevel06),
},
]);
backgroundMusic.current.setVolume(0.0);
}
} else {
Alert.alert('Not enough budget');
}
setBudget(currentBudget => currentBudget - item.cost);
setPurchasedItems(currentItems => [...currentItems, item]);
};
return (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment