Browse Source

conditional scrolling

pull/8/head
Tim Glasgow 2 years ago
parent
commit
14fa12e9ce
  1. 30
      squarenotsquare/src/screens/Game.js

30
squarenotsquare/src/screens/Game.js

@ -14,6 +14,7 @@ function Game(props){
const squareChallenge = useRef(genSquareChallenge()); const squareChallenge = useRef(genSquareChallenge());
const squareMemo = useMemo(renderPairs, [squareChallenge]); const squareMemo = useMemo(renderPairs, [squareChallenge]);
const challengeState = useRef(0);
const [timerState, setTimerState] = useState(0); const [timerState, setTimerState] = useState(0);
const localTimer = useRef(null); const localTimer = useRef(null);
const startTime = useRef(0); const startTime = useRef(0);
@ -68,18 +69,21 @@ function Game(props){
} }
}, [timerState]); }, [timerState]);
function scroll(){ function scroll(pairIndex){
let newOrigin = scrollDestination.current; if (pairIndex === challengeState.current){
let newDestination = scrollDestination.current - Metrics.animated.gameScrollInterval; let newOrigin = scrollDestination.current;
let newDestination = scrollDestination.current - Metrics.animated.gameScrollInterval;
scrollDestination.current = newDestination; scrollDestination.current = newDestination;
setScrollOrigin(newOrigin); challengeState.current = challengeState.current + 1;
setScrollOrigin(newOrigin);
}
} }
function generateSquare(){ function generateSquare(pairIndex){
return ( return (
<TouchableOpacity <TouchableOpacity
onPress={scroll} onPress={() => scroll(pairIndex)}
style={[styles.darkGreen, styles.answerButton, styles.dropShadow, styles.centeredItems, styles.centeredJustify]} style={[styles.darkGreen, styles.answerButton, styles.dropShadow, styles.centeredItems, styles.centeredJustify]}
> >
<MaterialIcon <MaterialIcon
@ -91,10 +95,10 @@ function Game(props){
) )
} }
function generateShape(index){ function generateShape(index, pairIndex){
return ( return (
<TouchableOpacity <TouchableOpacity
onPress={scroll} onPress={() => scroll(pairIndex)}
style={[styles.darkGreen, styles.answerButton, styles.dropShadow, styles.centeredItems, styles.centeredJustify]} style={[styles.darkGreen, styles.answerButton, styles.dropShadow, styles.centeredItems, styles.centeredJustify]}
> >
<MaterialIcon <MaterialIcon
@ -110,15 +114,15 @@ function Game(props){
if (answer === 0) { if (answer === 0) {
return ( return (
<View key={pairIndex} style={[styles.flexRow, styles.spaceEvenly, styles.buttonMargin]}> <View key={pairIndex} style={[styles.flexRow, styles.spaceEvenly, styles.buttonMargin]}>
{generateSquare()} {generateSquare(pairIndex)}
{generateShape(shapeIndex)} {generateShape(shapeIndex, pairIndex)}
</View> </View>
); );
} else { } else {
return ( return (
<View key={pairIndex} style={[styles.flexRow, styles.spaceEvenly, styles.buttonMargin]}> <View key={pairIndex} style={[styles.flexRow, styles.spaceEvenly, styles.buttonMargin]}>
{generateShape(shapeIndex)} {generateShape(shapeIndex, pairIndex)}
{generateSquare()} {generateSquare(pairIndex)}
</View> </View>
); );
} }

Loading…
Cancel
Save