Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="stage">
	<div id="j" class="character"></div>
	<div id="j2" class="character"></div>
</div>
<table>
<tr>
	<td>
		<input type="button" value="Run Left" onclick="j.run_left();" />
	</td>
	<td>
		<input type="button" value="Stop Running" onclick="j.stop_running();" />
	</td>
	<td>
		<input type="button" value="Run Right" onclick="j.run_right();" />
	</td>
	<td>
		<input type="button" value="Jump" onclick="j.jump();" />
	</td>
</tr>
<tr>
	<td>
		<input type="button" value="Run Left" onclick="j2.run_left();" />
	</td>
	<td>
		<input type="button" value="Stop Running" onclick="j2.stop_running();" />
	</td>
	<td>
		<input type="button" value="Run Right" onclick="j2.run_right();" />
	</td>
	<td>
		<input type="button" value="Jump" onclick="j2.jump();" />
	</td>
</tr>
</table>

              
            
!

CSS

              
                #stage {
	background-color:#cccccc;
	border:1px solid black;
	height:170px;
	width:600px;
	position:relative;
	z-index:0;
}

.character {
	height:50px;
	width:40px;
	position:absolute;
	top:120px;
	background-repeat:no-repeat;
	background-position:0 0;
}

#j {
	left:100px;
	background-image: url('http://www.joustmultimedia.com/tutorials/spriting/j.png');
}

#j2 {
	left:200px;
	background-image: url('http://www.joustmultimedia.com/tutorials/spriting/j2.png');
}
              
            
!

JS

              
                var RobotMaker = function(robot, run_speed, jump_height){
	
	var stage = document.getElementById('stage');
	var run_timer, jump_timer;
	var face_right = true;
	
	function run_r(phase, left){
		face_right = true;
		if ((left + (15 * run_speed)) < (stage.offsetWidth - robot.offsetWidth)){
			
			left = left + (15 * run_speed);
			robot.style.left = left+"px";
			switch (phase){
				case 1:
					robot.style.backgroundPosition = "-40px 0px";
					run_timer = setTimeout(function(){run_r(2, left);}, 200);
					break;
				case 2:
					robot.style.backgroundPosition = "-80px 0px";
					run_timer = setTimeout(function(){run_r(3, left);}, 200);
					break;
				case 3:
					robot.style.backgroundPosition = "-120px 0px";
					run_timer = setTimeout(function(){run_r(4, left);}, 200);
					break;
				case 4:
					robot.style.backgroundPosition = "-80px 0px";
					run_timer = setTimeout(function(){run_r(1, left);}, 200);
					break;
			}
		} else {
			robot.style.backgroundPosition = "0px 0px";
		}
	}
	
	function run_l(phase, left){
		face_right = false;
		if (0 < robot.offsetLeft - (15 * run_speed)){
			
			left = left - (15 * run_speed);
			robot.style.left = left+"px";
			switch (phase){
				case 1:
					robot.style.backgroundPosition = "-40px -50px";
					run_timer = setTimeout(function(){run_l(2, left);}, 200);
					break;
				case 2:
					robot.style.backgroundPosition = "-80px -50px";
					run_timer = setTimeout(function(){run_l(3, left);}, 200);
					break;
				case 3:
					robot.style.backgroundPosition = "-120px -50px";
					run_timer = setTimeout(function(){run_l(4, left);}, 200);
					break;
				case 4:
					robot.style.backgroundPosition = "-80px -50px";
					run_timer = setTimeout(function(){run_l(1, left);}, 200);
					break;
			}
		} else {
			robot.style.backgroundPosition = "0px -50px";
		}
	}
	
	function jmp(up, top){
		if (face_right){
			robot.style.backgroundPosition = "-160px 0px";
		} else {
			robot.style.backgroundPosition = "-160px -50px";
		}
		
		if (up && (robot.offsetTop > (20 * (1 / jump_height)))){
			top = top - (top * .1);
			robot.style.top = top+"px";
			jump_timer = setTimeout(function(){jmp(up, top);}, 60);
		} else if (up) {
			up = false;
			jump_timer = setTimeout(function(){jmp(up, top);}, 60);
		} else if (!up && (robot.offsetTop < 115)){
			top = top + (top * .1);
			robot.style.top = top+"px";
			jump_timer = setTimeout(function(){jmp(up, top);}, 60);
		} else {
			robot.style.top = "120px";
			if (face_right){
				robot.style.backgroundPosition = "0px 0px";
			} else {
				robot.style.backgroundPosition = "0px -50px";
			}
			
			jump_timer = false;
		}
		
	}
	
	return {

		run_right : function(){
			if (!jump_timer || jump_timer == undefined){
				clearTimeout(run_timer); // We clear whatever running is going on now ...
				run_r(1, robot.offsetLeft); // And we start them running to the right.
			}
		}, 
		
		run_left : function(){
			if (!jump_timer || jump_timer == undefined){
				clearTimeout(run_timer); // We clear whatever running is going on now ...
				run_l(1, robot.offsetLeft); // and we start them running to the left.
			}
		}, 
		
		stop_running : function(){
			if (!jump_timer || jump_timer == undefined){
				clearTimeout(run_timer);
				if (face_right){
					robot.style.backgroundPosition = "0px 0px";
				} else {
					robot.style.backgroundPosition = "0px -50px";
				}
				
			}
		}, 
		
		jump : function(){
			if (!jump_timer || jump_timer == undefined){
				clearTimeout(run_timer);
				jmp(true, robot.offsetTop);
			}
		}
	};
}

var j = RobotMaker(document.getElementById('j'), 1, 1);
	var j2 = RobotMaker(document.getElementById('j2'), .8, 4);
              
            
!
999px

Console