简体   繁体   English

反应本地的touchableopacity onpress不起作用

[英]React Native touchableopacity onpress not working

I'm new to react native (or any JS framework for that matter), and I'm trying to make a simple tap game where once you tap on the blue square it randomly renders in a different location, then you tap again, and so on. 我是本机(或与此相关的任何JS框架)做出反应的新手,我正在尝试制作一个简单的点击游戏,其中,一旦您点击蓝色方块,它会随机渲染到其他位置,然后再次点击,以此类推。 Here's my code so 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>
        );
    }
}

So that's a lot of code but I figured to include it all for context's sake. 因此,这是很多代码,但为了上下文,我想将其全部包括在内。 Anyways, the issue I'm having is with my renderFirstStage() function. 无论如何,我遇到的问题是我的renderFirstStage()函数。 See in the return statement where I'm calling an onPress event handler to run the function this.score? 在return语句中看到我正在调用onPress事件处理程序以运行功能this.score的地方吗? Ok, so when I run the program and I tap on one of those TouchableOpacities I get an error reading: _this5.score is not a function... _this5.score is undefined. 好的,所以当我运行程序并点击其中一个TouchableOpacities时,我看到一个错误消息:_this5.score不是函数... _this5.score是未定义的。 My best guess is that since I'm calling the onPress={()=>this.score} from within JS code that is within JSX, and I somehow don't have access to methods in the App class. 我最好的猜测是,由于我是从JSX内的JS代码中调用onPress = {{()=> this.score}的,因此我无法以某种方式访问​​App类中的方法。 So I'm thinking something is wrong with the scope, or maybe not, I don't know. 因此,我认为范围存在问题,或者可能不是,我不知道。 Even if my hunch is correct, I have no idea how to correct it. 即使我的直觉是正确的,我也不知道如何纠正它。 How can I call the score method from inside my TouchableOpacity components? 如何从TouchableOpacity组件内部调用score方法? Nothing I've tried seems to work for me... 我尝试过的所有方法似乎对我都不起作用...

Note: I'm running on an iPhone 7 emulator on a MacBook pro. 注意:我在MacBook Pro的iPhone 7模拟器上运行。

Yes it has the scope problem, you need to bind the function with this context in your constructor 是的,它存在范围问题,您需要在constructor functionthis functionthis上下文bind

constructor(props) {
    super();
    this.state = {
        showStartButton: true, // change back to true
        score: 0
    }
    this.renderFirstStage = this.renderFirstStage.bind(this);
}

Or you may simply use call to invoke the function immediately and bind it with this 或者您可以简单地使用call来立即调用该function并将其与this绑定

{!this.state.showStartButton ? this.renderFirstStage.call(this) : null}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM