From a5ab349f9c71335e0daa076b8c160fe57b55eb7f Mon Sep 17 00:00:00 2001 From: Sigrid Benezra Date: Fri, 30 Nov 2018 10:22:36 -0800 Subject: [PATCH] Completed callback functions. Working on checks and x showing on clicks. --- src/App.css | 2 +- src/App.js | 26 ++++++++++++++++++++------ src/components/GameItem.js | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/App.css b/src/App.css index 441dacf..eea9963 100644 --- a/src/App.css +++ b/src/App.css @@ -30,7 +30,7 @@ body { margin: 5px; cursor: pointer; - animation-duration: 4s; + animation-duration: 4s; /* orig 4s */ animation-name: item-move; animation-timing-function: linear; diff --git a/src/App.js b/src/App.js index 1ca7b71..a9e342b 100644 --- a/src/App.js +++ b/src/App.js @@ -25,32 +25,46 @@ class App extends Component { super(); this.state = { - items: [], + items: [], /* item array keeps track of items and if they've been spotted */ points: 0, }; // Uncomment this to spawn a single test item - //const testItem = this.spawnItem(Date.now()); - //this.state.items.push(testItem); + // const testItem = this.spawnItem(Date.now()); + // this.state.items.push(testItem); // Uncomment this to automatically spawn new items this.enableSpawner(); - console.log(this.state); + // console.log(this.state); } - onItemClicked = () => { + onItemClicked = (index) => { // Fill this in! + // This click handler is passed via props to GameItem + console.log("Item was clicked"); + // identify item clicked on + console.log(index); + // change it's state to spotted + let updatedItemData = this.state.items; + updatedItemData[index].wasSpotted = true; + this.setState({items: updatedItemData}); + console.log(this.state.items[index]); } render() { + // mapping over item array from state to create array of GameItems + // array of GameItems to be returned in return statement const items = this.state.items.map((item, i) => { return ; }); diff --git a/src/components/GameItem.js b/src/components/GameItem.js index 673cee9..f4ea3fe 100644 --- a/src/components/GameItem.js +++ b/src/components/GameItem.js @@ -5,6 +5,11 @@ import PropTypes from 'prop-types'; class GameItem extends Component { + clickCallback = (e) => { + console.log("An item was clicked!"); + console.log(this.props.index); + this.props.onClickCallback(this.props.index); + } render() { const itemStyle = { @@ -13,11 +18,35 @@ class GameItem extends Component { }; // Update this to select the correct icon for each item - const icon = ItemIcons.rock; + let icon; + let className = "game-item"; + + switch(this.props.type) { + case 'litter': + icon = ItemIcons.litter; + break; + case 'rock': + icon = ItemIcons.rock; + break; + case 'bush': + icon = ItemIcons.bush; + break; + case 'flower': + icon = ItemIcons.flower; + break; + default: + icon = ItemIcons.mushroom; + }; + + if (this.props.type === "litter" && this.props.wasSpotted) { + className = "game-item spotted-litter" + } else if (this.props.wasSpotted) { + className = "game-item spotted-nature"; + } return ( -
- Item +
+ Item
); } @@ -27,6 +56,7 @@ class GameItem extends Component { GameItem.propTypes = { height: PropTypes.number.isRequired, layer: PropTypes.number.isRequired, + type: PropTypes.string, } export default GameItem;