A Pen by Adam Lefevre on CodePen.
Created
September 8, 2017 01:45
-
-
Save livinlefevreloca/8ce89a3072b0b76aa013d0d6f8bfe1f8 to your computer and use it in GitHub Desktop.
Pixies and Pixels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="reactor"> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hero extends React.Component{ | |
constructor(props){ | |
super(props); | |
this.state = {health: 100, weapon: null, positionX: 300, positionY: 300} | |
} | |
componentWillMount(){ | |
let that = this; | |
document.onkeydown = function(e){ | |
switch(e.keyCode){ | |
case 38: | |
that.up(); | |
break; | |
case 40: | |
that.down(); | |
break; | |
case 37: | |
that.left(); | |
break; | |
case 39: | |
that.right(); | |
break; | |
default: | |
console.log(e.keyCode) | |
} | |
} | |
} | |
/*componentWillReceiveProps(){ | |
let that = this; | |
document.onkeydown = function(e){ | |
switch(e.keyCode){ | |
case 38: | |
that.up(); | |
break; | |
case 40: | |
that.down(); | |
break; | |
case 37: | |
that.left(); | |
break; | |
case 39: | |
that.right(); | |
break; | |
default: | |
console.log(e.keyCode) | |
} | |
} | |
}*/ | |
up(){ | |
let currentPY = this.state.positionY; | |
currentPY = currentPY-5; | |
this.setState({positionY: currentPY}) | |
} | |
down(){ | |
let currentPY = this.state.positionY; | |
currentPY = currentPY+5; | |
this.setState({positionY: currentPY}) | |
} | |
left(){ | |
let currentPX = this.state.positionX; | |
currentPX = currentPX-5; | |
this.setState({positionX: currentPX}) | |
} | |
right(){ | |
let currentPX = this.state.positionX; | |
currentPX = currentPX+5; | |
this.setState({positionX: currentPX}) | |
} | |
render(){ | |
let heroStyle = { height: 60, width: 40, position: "absolute", top: this.state.positionY, left: this.state.positionX} | |
return ( | |
<a href="https://drive.google.com/open?id=0B1ZYNw2fLvjKaWFabW5zSlBtSVk"><img src="https://drive.google.com/open?id=0B1ZYNw2fLvjKaWFabW5zSlBtSVk" style={heroStyle}/></a> | |
) | |
} | |
} | |
ReactDOM.render(<Hero />, document.getElementById("reactor")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://fb.me/react-with-addons-0.14.6.js"></script> | |
<script src="https://fb.me/react-dom-0.14.6.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment