-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.tsx
More file actions
132 lines (123 loc) · 3.41 KB
/
Copy pathApp.tsx
File metadata and controls
132 lines (123 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useRef, useState, useEffect } from 'react';
import {
View,
Button,
Text,
Switch,
TextInput,
StyleSheet,
SafeAreaView,
} from 'react-native';
import { Sprites, type SpritesMethods } from 'react-native-sprites';
const App = () => {
const spriteRef = useRef<SpritesMethods>(null);
const [loop, setLoop] = useState<boolean>(true);
const [fps, setFPS] = useState<number>(15);
const [inputFPS, setInputFPS] = useState<string>('15');
const [currentAnimation, setCurrentAnimation] = useState<string>('down');
// useEffect to handle changes in fps or loop and update the current animation
useEffect(() => {
if (currentAnimation) {
spriteRef.current?.play({
type: currentAnimation,
fps,
loop,
});
}
}, [fps, loop, currentAnimation]);
const handlePlay = (type: string) => {
setCurrentAnimation(type);
spriteRef.current?.play({
type,
fps,
loop,
});
console.log(`Playing animation: ${type}`);
};
const handleStop = () => {
spriteRef.current?.stop(() => console.log('Animation stopped'));
};
const handleFPS = (text: string) => {
setInputFPS(text); // Update raw input value first
const parsedFPS = Number(text);
if (!isNaN(parsedFPS) && parsedFPS > 0) {
setFPS(parsedFPS); // Only update FPS if valid
spriteRef.current?.play({
type: currentAnimation,
fps: parsedFPS,
loop,
});
} else {
spriteRef.current?.stop(() => {
console.log('Animation stopped due to invalid FPS input');
});
}
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.sprite}>
<Sprites
ref={spriteRef}
source={require('../assets/goat.png')}
columns={3}
rows={4}
animations={{
down: { row: 0, startFrame: 0, endFrame: 2 },
left: { row: 1, startFrame: 0, endFrame: 2 },
right: { row: 2, startFrame: 0, endFrame: 2 },
up: { row: 3, startFrame: 0, endFrame: 2 },
}}
/>
</View>
<View style={styles.controls}>
<View style={styles.controlRow}>
<Text>Loop: </Text>
<Switch value={loop} onValueChange={(value) => setLoop(value)} />
</View>
<View style={styles.controlRow}>
<Text>FPS: </Text>
<TextInput
value={inputFPS}
onChangeText={handleFPS}
keyboardType="numeric"
/>
</View>
<View>
<Text>Animation:</Text>
<View style={styles.buttons}>
<Button title="Down" onPress={() => handlePlay('down')} />
<Button title="Left" onPress={() => handlePlay('left')} />
<Button title="Right" onPress={() => handlePlay('right')} />
<Button title="Up" onPress={() => handlePlay('up')} />
<Button title="Stop" onPress={handleStop} />
</View>
</View>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
sprite: {
marginBottom: 20,
},
controls: {
width: '80%',
},
controlRow: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 10,
},
buttons: {
flexDirection: 'row',
justifyContent: 'space-between',
marginVertical: 10,
},
});
export default App;