Skip to content

Instantly share code, notes, and snippets.

@livinlefevreloca
Created September 8, 2017 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save livinlefevreloca/8ce89a3072b0b76aa013d0d6f8bfe1f8 to your computer and use it in GitHub Desktop.
Save livinlefevreloca/8ce89a3072b0b76aa013d0d6f8bfe1f8 to your computer and use it in GitHub Desktop.
Pixies and Pixels
<div id="reactor">
</div>
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"));
<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