Reactjs 当用户在我的刽子手游戏中输入错误的猜测时,无法获得刽子手的不同图片来显示
当用户键入一个错误的字母时,不会弹出正确的图像。我可以得到第一张图片(加洛尔)显示,但除此之外,它不会改变时,有一个错误的字母输入的用户 我尝试了多种方法,如导入图片和要求,但都不起作用。我们也尝试过使函数“updateAttents()”仅用于尝试,同样的情况也在发生Reactjs 当用户在我的刽子手游戏中输入错误的猜测时,无法获得刽子手的不同图片来显示,reactjs,react-native,Reactjs,React Native,当用户键入一个错误的字母时,不会弹出正确的图像。我可以得到第一张图片(加洛尔)显示,但除此之外,它不会改变时,有一个错误的字母输入的用户 我尝试了多种方法,如导入图片和要求,但都不起作用。我们也尝试过使函数“updateAttents()”仅用于尝试,同样的情况也在发生 class HangmanPicture extends React.Component { updatePicture() { const image0 = require('./han
class HangmanPicture extends React.Component {
updatePicture()
{
const image0 = require('./hangman0.png');
const image1 = require('./hangman1.png');
const image2 = require('./hangman2.png');
const image3 = require('./hangman3.png');
const image4 = require('./hangman4.png');
const image5 = require('./hangman5.png');
const image6 = require('./hangman6.png');
let image = image0;
if(this.props.updateAttempts === 5)
image = image1
else if(this.props.updateAttempts === 4)
image = image2
else if(this.props.updateAttempts === 3)
image = image3
else if(this.props.updateAttempts === 2)
image = image4
else if(this.props.updateAttempts === 1)
image = image5
else if(this.props.updateAttempts === 0)
image = image6
return (<img src={image} alt="" />)
}
render(){
return(
<div className="HangmanPicture">
{this.updatePicture()}
</div>
);
}
}
类HangmanPicture扩展React.Component{
updatePicture()
{
const image0=require('./hangman0.png');
const image1=require('./hangman1.png');
const image2=require('./hangman2.png');
const image3=require('./hangman3.png');
const image4=require('./hangman4.png');
const image5=require('./hangman5.png');
const image6=require('./hangman6.png');
设image=image0;
if(this.props.updateAttents==5)
image=image1
else if(this.props.updateAttents==4)
图像=图像2
else if(this.props.updateAttents==3)
图像=图像3
else if(this.props.updateAttents==2)
图像=图像4
else if(this.props.updateAttents==1)
图像=图像5
else if(this.props.updateAttents==0)
图像=图像6
返回()
}
render(){
返回(
{this.updatePicture()}
);
}
}
它编译时没有带警告的错误消息,但除此之外,它在某种程度上可以正常工作。首先,您需要绑定updatePicture或使其成为arrow函数 因此,我们应该:
updatePicture = () => {
...
}
此外,必须以某种方式更新this.props.updateAttemps。如果您不更新任何状态,您认为updateAttemps将如何更改并显示正确的图像?首先,您需要绑定updatePicture或使其成为箭头函数 因此,我们应该:
updatePicture = () => {
...
}
此外,必须以某种方式更新this.props.updateAttemps。如果您不更新任何状态,您认为updateAttemps将如何更改并显示正确的图像?如何递增
UpdateAttents
?如何递增UpdateAttents
?