Embarking on my journey with React Native (or any JS framework for that matter), I decided to delve into creating a simple tap game. The concept is straightforward: tap on the blue square, watch it randomly relocate, and repeat the process. Here is a snippet of the code I've crafted thus far:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
TouchableOpacity,
View
} from 'react-native';
import styles from './src/styles/style';
export default class App extends Component<{}> {
constructor(props) {
super();
this.state = {
showStartButton: true, // change back to true
score: 0
}
}
startGame() {
this.setState({
showStartButton: !this.state.showStartButton
});
this.renderFirstStage();
}
score() {
this.setState({
showStartButton: !this.state.showStartButton,
score: score+1
});
}
renderStartButton() {
return (
<TouchableOpacity style={styles.startButton} onPress={()=>this.startGame()}>
<Text style={styles.startButtonText}>START</Text>
</TouchableOpacity>
);
}
renderWhiteBox() {
return (
<View style={{flex: 1, backgroundColor: 'white'}} />
);
}
renderBlueBox() {
return (
<View style={{flex: 1, backgroundColor: 'blue'}} />
);
}
renderFirstStage() {
var blueColNum = Math.floor(Math.random() * 6);
var blueRowNum = Math.floor(Math.random() * 4);
var col1 = ['white', 'white', 'white', 'white', 'white', 'white'];
var col2 = ['white', 'white', 'white', 'white', 'white', 'white'];
var col3 = ['white', 'white', 'white', 'white', 'white', 'white'];
var col4 = ['white', 'white', 'white', 'white', 'white', 'white'];
if (blueRowNum == 0)
col1[blueColNum] = 'blue';
else if (blueRowNum == 1)
col2[blueColNum] = 'blue';
else if (blueRowNum == 2)
col3[blueColNum] = 'blue';
else if (blueRowNum == 3)
col4[blueColNum] = 'blue';
return (
<View style={{flex: 1, flexDirection: 'row'}}>
<View style={{flex: 1}}>
<View style={styles.scoreBoard}><Text style={styles.timerText}>Time: {blueRowNum}</Text></View>
{col1.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()} />
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'gray'}} />
{col2.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()}/>
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'gray'}} />
{col3.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()} />
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
<View style={{flex: 1}}>
<View style={styles.scoreBoard}><Text style={styles.timerText}>Score: {this.state.score}</Text></View>
{col4.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()} />
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
</View>
);
}
render() {
return (
<View style={styles.container}>
{this.state.showStartButton ? this.renderStartButton() : null}
{!this.state.showStartButton ? this.renderFirstStage() : null}
</View>
);
}
}
Despite a lengthy chunk of code present, I felt it necessary to provide full context. Moving along, the current roadblock lies within my renderFirstStage() function. Upon closer inspection, notice where an onPress event handler is utilized to call upon the function this.score? When running the program and tapping on those TouchableOpacities, an error surfaces: _this5.score is not a function... _this5.score is undefined. Upon brainstorming, it appears that when calling onPress={()=>this.score} from JavaScript embedded in JSX, accessing methods within the App class poses a challenge. I suspect an issue with the scope, though without certainty. How might one execute the score method inside my TouchableOpacity components successfully? Every attempt I've made yields no positive outcome...
Please note: My setup involves an iPhone 7 emulator on a MacBook Pro.