-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
119 lines (102 loc) · 2.67 KB
/
App.js
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
import { StatusBar } from 'expo-status-bar';
import React, { Component } from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
import ConfettiCannon from 'react-native-confetti-cannon';
import { Audio } from 'expo-av';
import LottieView from 'lottie-react-native';
const soundObject = new Audio.Sound();
export default class App extends Component {
constructor(props) {
super(props);
this.animations = [this.popCat];
for (let i = 0; i < 4; i++) {
this.animations.push(this.explodeConfetti);
}
this.explosion;
this.loadSounds();
this.state = {
catIsPopping: false,
};
}
loadSounds = async () => {
try {
await soundObject.loadAsync(require('./assets/sounds/ding-sound-effect_2.mp3'));
} catch (error) {
// An error occurred!
}
}
explodeConfetti = () => {
this.explosion && this.explosion.start();
}
popCat = () => {
this.setState({
catIsPopping: !this.state.catIsPopping,
});
this.cat && this.cat.play();
}
renderResults = async () =>{
try {
await soundObject.replayAsync();
// Your sound is playing!
} catch (error) {
// An error occurred!
}
let choice = Math.floor(Math.random() * 10) % this.animations.length;
this.animations[choice]();
}
render() {
return (
<View style={styles.container}>
{this.state.catIsPopping?<LottieView
source={require('./assets/cat_pop.json')}
ref={cat => {
this.cat = cat;
}}
loop={false}
onAnimationFinish={() => this.setState({ catIsPopping: false })}
/>:null}
<StatusBar style="auto" />
{!this.state.catIsPopping?<TouchableOpacity
style={styles.dingButton}
onPress={this.renderResults}
underlayColor='#fff'>
<Text style={styles.dingText}>Ding!</Text>
</TouchableOpacity>:null}
<ConfettiCannon
count={70}
origin={{x: -10, y: 0}}
explosionSpeed={400}
fallSpeed={3000}
autoStart={false}
fadeOut={true}
ref={ref => (this.explosion = ref)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
dingButton:{
marginRight:40,
marginLeft:40,
marginTop:10,
padding: 20,
backgroundColor:'#33FF99',
borderRadius:20,
borderWidth: 1,
borderColor: '#fff'
},
dingText:{
color:'#fff',
textAlign:'center',
paddingLeft : 10,
paddingRight : 10,
fontSize: 50,
},
});