initial position and placement zone error #3451
Unanswered
KaraElBatson
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to code a puzzle game with the React Native expo gesture handler library. Above is the world map, below are the continent pieces that will be placed on the world map, but I'm having trouble with the target location and the location of the continents when the game is first opened.
My code :
import React, { useState, useEffect, useRef } from 'react';
import {View,Text,StyleSheet,Dimensions,Image,TouchableOpacity,ImageBackground,Platform} from 'react-native';
import { Audio } from 'expo-av';
import FlashMessage, { showMessage } from 'react-native-flash-message';
import Menu from '../../components/menu';
import * as Speech from 'expo-speech';
import FontAwesome from '@expo/vector-icons/FontAwesome';
import { GestureHandlerRootView, GestureDetector, Gesture } from 'react-native-gesture-handler';
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
runOnJS
} from 'react-native-reanimated';
import * as Haptics from 'expo-haptics';
// Styles
import mainStyle from '../../styles/main.style';
const GAME_CONSTANTS = {
INITIAL_LIVES: 3,
POINTS_PER_CORRECT: 10,
AUDIO_DELAY: 1000,
NEW_ROUND_DELAY: 2000,
};
// Kıta bilgileri - yüzdelik değerlerle tanımlanmış - mavi ekrandaki etiketlere göre düzenlendi
const CONTINENTS = [
{
id: 1,
name: 'Kuzey Amerika',
image: require('../../assets/cont/namerıca.png'),
targetPosition: { xPercent: 0.18, yPercent: 0.3 }, // Sol üst etiket
widthPercent: 0.15,
heightPercent: 0.2
},
{
id: 2,
name: 'Güney Amerika',
image: require('../../assets/cont/samerıca.png'),
targetPosition: { xPercent: 0.18, yPercent: 0.7 }, // Sol alt etiket
widthPercent: 0.10,
heightPercent: 0.2
},
{
id: 3,
name: 'Avrupa',
image: require('../../assets/cont/eu.png'),
targetPosition: { xPercent: 0.35, yPercent: 0.3 }, // Orta üst etiket
widthPercent: 0.08,
heightPercent: 0.12
},
{
id: 4,
name: 'Afrika',
image: require('../../assets/cont/afrıca.png'),
targetPosition: { xPercent: 0.35, yPercent: 0.7 }, // Orta alt etiket
widthPercent: 0.12,
heightPercent: 0.2
},
{
id: 5,
name: 'Asya',
image: require('../../assets/cont/asıa.png'),
targetPosition: { xPercent: 0.52, yPercent: 0.3 }, // Sağ üst etiket
widthPercent: 0.16,
heightPercent: 0.2
},
{
id: 6,
name: 'Avustralya',
image: require('../../assets/cont/aus.png'),
targetPosition: { xPercent: 0.52, yPercent: 0.7 }, // Sağ alt etiket
widthPercent: 0.12,
heightPercent: 0.12
},
];
export default function ContinentPuzzleGame() {
const [score, setScore] = useState(0);
const [lives, setLives] = useState(GAME_CONSTANTS.INITIAL_LIVES);
const [gameOver, setGameOver] = useState(false);
const [sound, setSound] = useState(false);
const [placedContinents, setPlacedContinents] = useState([]);
const [continents, setContinents] = useState([]);
// Her kıta için pozisyon ve animasyon değerleri
const positions = useRef(
CONTINENTS.map(() => ({
x: useSharedValue(0),
y: useSharedValue(0),
isPlaced: useSharedValue(false),
scale: useSharedValue(1), // Animasyon için ölçek
zIndex: useSharedValue(1),
startX: useSharedValue(0),
startY: useSharedValue(0),
}))
).current;
const playSound = async (soundFile) => {
try {
const { sound } = await Audio.Sound.createAsync(soundFile);
await sound.playAsync();
sound.setOnPlaybackStatusUpdate((status) => {
if (status.didJustFinish) {
sound.unloadAsync();
}
});
setSound(true);
setTimeout(() => setSound(false), 1500);
} catch (error) {
console.error('Ses efekti hatası:', error);
}
};
const playAudio = async (type, message = '') => {
try {
await Speech.stop();
};
// Yüzdelikleri piksel değerlerine dönüştürme
const getPixelValues = (continent) => {
const width = GAME_AREA_WIDTH * continent.widthPercent;
const height = GAME_AREA_HEIGHT * 0.6 * continent.heightPercent;
const x = GAME_AREA_WIDTH * continent.targetPosition.xPercent;
const y = (GAME_AREA_HEIGHT * 0.6) * continent.targetPosition.yPercent;
};
// Doğru yerleştirme geri bildirimi
const handleCorrectPlacement = (continentId, continentName, index) => {
if (!placedContinents.includes(continentId)) {
setPlacedContinents(prev => [...prev, continentId]);
const newScore = score + GAME_CONSTANTS.POINTS_PER_CORRECT;
setScore(newScore);
};
// Yanlış yerleştirme geri bildirimi
const handleWrongPlacement = (continentName, targetContinentName, index) => {
if (lives > 0) {
setLives(prev => prev - 1);
}
};
const createPanGesture = (index) => {
return Gesture.Pan()
.onStart(() => {
console.log(
Sürükleme başladı - Kıta: ${CONTINENTS[index].name}
);positions[index].startX.value = positions[index].x.value;
positions[index].startY.value = positions[index].y.value;
})
.onUpdate((event) => {
if (positions[index].isPlaced.value && !gameOver) {
console.log(
${CONTINENTS[index].name} zaten yerleştirilmiş, hareket etmiyor
);return;
}
};
// Animasyon stilleri
const getAnimatedStyles = (index) => {
return useAnimatedStyle(() => {
return {
transform: [
{ translateX: positions[index].x.value },
{ translateY: positions[index].y.value },
{ scale: positions[index].scale.value },
],
opacity: positions[index].isPlaced.value ? 0.7 : 1,
zIndex: positions[index].zIndex.value,
};
});
};
const startGame = async () => {
setScore(0);
setLives(GAME_CONSTANTS.INITIAL_LIVES);
setGameOver(false);
setPlacedContinents([]);
const shuffledContinents = [...CONTINENTS].sort(() => Math.random() - 0.5);
setContinents(shuffledContinents);
};
useEffect(() => {
const initGame = async () => {
await startGame();
};
initGame();
return () => {
Speech.stop();
};
}, []);
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<ImageBackground
source={require('../../assets/background.jpg')}
style={styles.background}
>
Kıta Eşleştirme
);
}
const { width, height } = Dimensions.get('window');
const GAME_AREA_WIDTH = width > 600 ? width * 0.8 : width * 0.95;
const GAME_AREA_HEIGHT = height * 0.6;
const styles = StyleSheet.create({


background: {
flex: 1,
width: '100%',
height: '100%',
},
title: {
fontSize: 24,
fontFamily: 'Iceberg_400Regular',
textAlign: 'center',
marginBottom: 20,
color: '#333',
paddingTop: 50,
},
statsContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: 20,
backgroundColor: 'rgba(255, 255, 255, 0.8)',
padding: 15,
borderRadius: 15,
marginHorizontal: 20,
},
score: {
fontSize: 20,
fontFamily: 'Iceberg_400Regular',
color: '#333',
paddingHorizontal: 20,
},
lives: {
fontSize: 20,
fontFamily: 'Iceberg_400Regular',
color: '#333',
paddingHorizontal: 20,
},
gameContainer: {
width: GAME_AREA_WIDTH,
height: GAME_AREA_HEIGHT,
alignSelf: 'center',
position: 'relative',
},
mapContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: GAME_AREA_HEIGHT * 0.6,
backgroundColor: '#87CEEB',
borderWidth: 5,
borderColor: '#8B4513',
borderRadius: 10,
overflow: 'hidden',
justifyContent: 'center',
alignItems: 'center',
},
worldMap: {
width: '70%',
height: '90%',
opacity: 0.7,
},
targetAreasContainer: {
position: 'absolute',
top: 0,
left: 0,
width: GAME_AREA_WIDTH,
height: GAME_AREA_HEIGHT * 0.6,
zIndex: 2,
},
targetArea: {
position: 'absolute',
borderWidth: 3,
borderRadius: 10,
borderStyle: 'dashed',
justifyContent: 'center',
alignItems: 'center',
zIndex: 3,
borderColor: 'rgba(255, 99, 71, 0.8)',
},
targetLabel: {
color: '#333',
fontSize: 10,
fontWeight: 'bold',
textAlign: 'center',
backgroundColor: 'rgba(255, 255, 255, 0.7)',
padding: 2,
borderRadius: 5,
position: 'absolute',
bottom: 5,
},
draggableContainer: {
position: 'absolute',
top: GAME_AREA_HEIGHT * 0.65,
left: 0,
width: GAME_AREA_WIDTH,
height: GAME_AREA_HEIGHT * 0.35,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-around',
alignItems: 'center',
backgroundColor: '#8B4513',
borderWidth: 5,
borderColor: '#8B4513',
borderRadius: 10,
padding: 10,
},
draggableContinent: {
justifyContent: 'center',
alignItems: 'center',
borderRadius: 10,
margin: 5,
elevation: 5,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.25,
shadowRadius: 3.84,
},
continentImage: {
width: '100%',
height: '100%',
},
gameOverContainer: {
position: 'absolute',
top: '40%',
left: '10%',
right: '10%',
backgroundColor: 'rgba(255, 255, 255, 0.95)',
padding: 20,
borderRadius: 15,
alignItems: 'center',
zIndex: 10,
},
gameOverText: {
fontSize: 24,
fontFamily: 'Iceberg_400Regular',
color: '#333',
marginBottom: 10,
},
finalScore: {
fontSize: 20,
fontFamily: 'Iceberg_400Regular',
color: '#333',
marginBottom: 20,
},
restartButton: {
backgroundColor: '#4CAF50',
padding: 15,
borderRadius: 10,
width: '100%',
alignItems: 'center',
},
restartText: {
color: 'white',
fontSize: 18,
fontFamily: 'Iceberg_400Regular',
},
});
Error target place loc:
Error start loc cont puzzle piece
Beta Was this translation helpful? Give feedback.
All reactions