Chris is the MVP.
Code:
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
#container {
background-image: url(scene-02.jpg);
display: inline-block;
width: 500px;
height: 500px;
}
</style>
</head>
<body>
<div id=”container”></div>
<!– KineticJS library –>
<script src=”http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.0.min.js”></script>
<!– script will not run until after the page has loaded –>
<script>
// function drawStick(imageObj) {
// //setting the stage
// var stage = new Kinetic.Stage({
// container: “container”,
// width: 1000,
// height: 1000
// });
// var layer = new Kinetic.Layer();
// background image
// var backgroundImage = new Kinetic.Image({
// image: scene.jpg,
// width: 1000,
// height: 1000,
// draggable: false
// });
// }
var walker;
var x = 6;
var y = 6;
window.onload = function() {
var stage = new Kinetic.Stage({
container: “container”,
width: 500,
height: 500
});
// var backgroundImage = new Kinetic.Image({
// image: scene.jpg,
// width: 1000,
// height: 1000,
// draggable: false
// });
// backgroundImage.src = “scene.jpg”;
// }
var backgroundImage = new Image();
backgroundImage.onload = function() {
backgroundImage = new Kinetic.Image({
x: 0,
y: 0,
image: backgroundImage,
// animation: ‘standright’,
// animations: animations,
});
}
backgroundImage.src = “scene-02.jpg”;
var layer = new Kinetic.Layer();
var animations = {
walkfront: [{
x: 64,
y: 0,
width: 31,
height: 60
}, {
x: 95,
y: 0,
width: 31,
height: 60
}, {
x: 126,
y: 0,
width: 31,
height: 60
}, {
x: 157,
y: 0,
width: 31,
height: 60
}],
standfront: [{ x: 31, y: 0, width: 31, height: 60}],
walkleft: [{
x: 64,
y: 62,
width: 31,
height: 60
}, {
x: 95,
y: 62,
width: 31,
height: 60
}, {
x: 126,
y: 62,
width: 31,
height: 60
}, {
x: 157,
y: 62,
width: 31,
height: 60
}],
standleft: [{ x: 30, y: 63, width: 31, height: 60}],
walkright: [{
x: 64,
y: 192,
width: 31,
height: 60
}, {
x: 95,
y: 192,
width: 31,
height: 60
}, {
x: 126,
y: 192,
width: 31,
height: 60
}, {
x: 157,
y: 192,
width: 31,
height: 60
}],
standright: [{ x: 31, y: 192, width: 31, height: 60}],
walkback: [{
x: 64,
y: 126,
width: 31,
height: 60
}, {
x: 95,
y: 126,
width: 31,
height: 60
}, {
x: 126,
y: 126,
width: 31,
height: 60
}, {
x: 157,
y: 126,
width: 31,
height: 60
}],
standback: [{ x: 31, y: 126, width: 31, height: 60}]
};
var imageObj = new Image();
imageObj.onload = function() {
walker = new Kinetic.Sprite({
x: 30,
y: 400,
image: imageObj,
animation: ‘standright’,
animations: animations,
frameRate: 7
});
layer.add(walker);
stage.add(layer);
walker.start();
}
imageObj.src = “spriteremix.png”;
};
function Walk(fx) {
switch(fx) {
case ‘front’:
if(walker.attrs.animation != ‘walkfront’) {
walker.setAnimation(‘walkfront’);
}
walker.setY(walker.attrs.y + y);
break;
case ‘back’:
if(walker.attrs.animation != ‘walkback’) {
walker.setAnimation(‘walkback’);
}
walker.setY(walker.attrs.y – y);
break;
case ‘right’:
if(walker.attrs.animation != ‘walkright’) {
walker.setAnimation(‘walkright’);
}
walker.setX(walker.attrs.x + x);
break;
case ‘left’:
if(walker.attrs.animation != ‘walkleft’) {
walker.setAnimation(‘walkleft’);
}
walker.setX(walker.attrs.x – x);
break;
}
}
function Stand(fx) {
switch(fx) {
case ‘Back’:
walker.setAnimation(‘standback’);
break;
case ‘Front’:
walker.setAnimation(‘standfront’);
break;
case ‘Left’:
walker.setAnimation(‘standleft’);
break;
case ‘Right’:
walker.setAnimation(‘standright’);
break;
}
}
function doKeyDown(evt) {
switch(evt.keyCode) {
case 38: /* Up arrow was pressed */
Walk(‘back’);
break;
case 40: /* Down arrow was pressed */
Walk(‘front’);
break;
case 37: /* Left arrow was pressed */
Walk(‘left’);
break;
case 39: /* Right arrow was pressed */
Walk(‘right’);
break;
}
}
function doKeyUp(evt) {
switch(evt.keyCode) {
case 38: /* Up arrow was pressed */
Stand(‘Back’);
break;
case 40: /* Down arrow was pressed */
Stand(‘Front’);
break;
case 37: /* Left arrow was pressed */
Stand(‘Left’);
break;
case 39: /* Right arrow was pressed */
Stand(‘Right’);
break;
}
}
window.addEventListener(‘keydown’, doKeyDown, true);
window.addEventListener(‘keyup’, doKeyUp, true);
</script>
</body>
</html>