Javascript 反应本机结束游戏屏幕不显示和崩溃
我的数字游戏有问题 脚本Javascript 反应本机结束游戏屏幕不显示和崩溃,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我的数字游戏有问题 脚本 游戏结束后不在屏幕上显示游戏 找到正确号码后应用程序崩溃 import React,{useState}来自“React”; 从“react native”导入{StyleSheet,View}; 从“./Components/Header”导入标题; 从“/screens/StartGameScreen”导入StartGameScreen; 从“./screens/GameScreen”导入游戏屏幕; 从“./screens/GameOverScreen”导入游戏
- 游戏结束后不在屏幕上显示游戏
- 找到正确号码后应用程序崩溃
import React,{useState}来自“React”;
从“react native”导入{StyleSheet,View};
从“./Components/Header”导入标题;
从“/screens/StartGameScreen”导入StartGameScreen;
从“./screens/GameScreen”导入游戏屏幕;
从“./screens/GameOverScreen”导入游戏屏幕;
导出默认函数App(){
const[userNumber,setUserNumber]=useState();
常量[GEUSSHARDS,SETGEUSSHARDS]=useState(0);
常量startGameHandler=(selectedNumber)=>{
setUserNumber(selectedNumber);
};
const gameOverHandler=numOfRounds=>{
设置轮数(numfounds);
设置轮数(0);
};
让内容=;
if(userNumber&&geusshalls 0){
内容=;
}
返回(
{content}
);
}
const styles=StyleSheet.create({
屏幕:{
弹性:1
}
});
App.js
前端
import React,{useState,useRef,useffect}来自'React';
从“react native”导入{视图、文本、样式表、按钮、警报};
从“../Components/NumberContainer”导入NumberContainer;
从“../Components/Card”导入卡片;
const generateRandomBetween=(最小、最大、排除)=>{
min=数学单元(min);
最大值=数学楼层(最大值);
const rndNum=Math.floor(Math.random()*(max-min))+min;
如果(rndNum==排除){
返回generateRandomBetween(最小、最大、排除);
}否则{
返回rndNum;
}
};
常量游戏屏幕=道具=>{
const[currentGeuss,setCurrentGeuss]=useState(generateRandomBetween(1100,props.userChoice));
const[rounds,setRounds]=useState(0);
const currentLow=useRef(1);
const currentHigh=useRef(100);
const{userChoice,onGameOver}=props;
useffect(()=>{
if(currentGeuss==userChoice){
onGameOver(轮);
}
},[currentGeuss,userChoice,onGameOver]);
const nextgeusHandler=方向=>{
if((方向=='lower'&¤tGeussprops.userChoice)){
Alert.Alert('Don't LIE!!!!!','You known this more…',[{text:'Sorry!',style:'cancel');
返回;
}
如果(方向=='下'){
currentHigh.current=currentGeuss;
}否则{
currentLow.current=currentGeuss;
}
const nextNumber=发电机范围(currentLow.current、currentHigh.current、currentGeuss);
setCurrentGeuss(下一个编号);
设置轮数(curRounds=>curRounds+1);
};
返回(
对手的Geuss
{currentGeuss}
);
};
const styles=StyleSheet.create({
屏幕:{
弹性:1,
填充:10,
对齐项目:“中心”
},
按钮容器:{
flexDirection:'行',
为内容辩护:“周围的空间”,
玛金托普:20,
宽度:300,
最大宽度:“80%”
},
});
导出默认游戏屏幕;
GameScreen.js我想问题出在这个脚本中,但我找不到它,请帮助
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const GameOverScreen = props => {
return (
<View style={styles.screen}>
<Text>The Game is Over!</Text>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
});
export default GameOverScreen;
从“React”导入React;
从“react native”导入{View,Text,StyleSheet};
const GameOverScreen=道具=>{
返回(
游戏结束了!
);
};
const styles=StyleSheet.create({
屏幕:{
弹性:1,
为内容辩护:“中心”,
对齐项目:“中心”
}
});
导出默认游戏机屏幕;
GameOverScreen.js
找到正确的数字后崩溃,并尝试进入下一个很明显,你得到的是一个无限循环。错误消息指出了您的问题:您的
generateRandomBetween()
函数。您应该检查递归并调试输入,以找出忘记了哪些结束循环条件。setgeusshalls(0);放置错误,需要放置在startGameHandler中!
import React, { useState, useRef, useEffect } from 'react';
import { View, Text, StyleSheet, Button, Alert } from 'react-native';
import NumberContainer from '../Components/NumberContainer';
import Card from '../Components/Card';
const generateRandomBetween = (min, max, exclude) => {
min = Math.ceil(min);
max = Math.floor(max);
const rndNum = Math.floor(Math.random() * (max - min)) + min;
if (rndNum === exclude){
return generateRandomBetween(min, max, exclude);
} else{
return rndNum;
}
};
const GameScreen = props => {
const [ currentGeuss, setCurrentGeuss] = useState(generateRandomBetween(1,100, props.userChoice));
const [rounds, setRounds] = useState(0);
const currentLow = useRef(1);
const currentHigh = useRef(100);
const { userChoice, onGameOver } = props;
useEffect(() => {
if (currentGeuss === userChoice) {
onGameOver(rounds);
}
}, [currentGeuss, userChoice, onGameOver]);
const nextGeussHandler = direction => {
if ((direction === 'lower' && currentGeuss < props.userChoice) || (direction === 'greater' && currentGeuss > props.userChoice)){
Alert.alert('Don\'t LIE!!!!!', 'You know this is wrong...', [{ text: 'Sorry!', style: 'cancel'}]);
return;
}
if (direction === 'lower'){
currentHigh.current = currentGeuss;
} else {
currentLow.current = currentGeuss;
}
const nextNumber = generateRandomBetween(currentLow.current, currentHigh.current, currentGeuss);
setCurrentGeuss(nextNumber);
setRounds(curRounds => curRounds + 1);
};
return (
<View style={styles.screen}>
<Text>Opponent's Geuss</Text>
<NumberContainer>{currentGeuss}</NumberContainer>
<Card style={styles.buttonContainer}>
<Button title="LOWER" onPress={nextGeussHandler.bind(this, 'lower')}/>
<Button title="CREATER" onPress={nextGeussHandler.bind(this, 'greater')}/>
</Card>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
padding: 10,
alignItems: 'center'
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-around',
marginTop: 20,
width: 300,
maxWidth: '80%'
},
});
export default GameScreen;
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const GameOverScreen = props => {
return (
<View style={styles.screen}>
<Text>The Game is Over!</Text>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
});
export default GameOverScreen;