JQuery Game – King of Fighters – lesson 1
JQuery – Interactive Sprite Sheet Animation

the-king-of-fighters

Sprite Sheet is a larger image for combining multiple individual images into a single, efficiently laid out image.

The Original Sprite Sheets:

stand.png

punch-right.png

The idea is to change div class using JQuery. Ultra Easy!

The Source Code:

<!DOCTYPE html>
<html>
<!-- Don't Break My b***s - Gimme Code! Project                                              -->
<!-- Author: Andrea Tonin - http://blog.lucedigitale.com                                     -->
<!-- This code come with absolutely no warranty                                              -->
<!-- If you use this code in your project please give a link to http://blog.lucedigitale.com -->
<!-- Thanks in advance                                                                       -->
<head>
<title>JQuery - Interactive Sprite Sheet Animation</title>

<style type="text/css" media="screen">
/* Anim Stand START */
@-webkit-keyframes stand {
    from { background-position: 0px; }
    to { background-position: -1536px; } /* The width of the sprite sheet is 1536px, the sprite sheet moves from left to right  */
}

@-moz-keyframes stand {
    from { background-position: 0px; }
    to { background-position: -1536px; }
}

@keyframes stand {
    from { background-position: 0px; }
    to { background-position: -1536px; }
}
/* Anim Stand END */

/* Anim Punch START */
@-webkit-keyframes punch {
    from { background-position: 0px; }
    to { background-position: -1280px; } /* The width of the sprite sheet is 1280px, the sprite sheet moves from left to right  */
}

@-moz-keyframes puch {
    from { background-position: 0px; }
    to { background-position: -1280px; }
}

@keyframes punch {
    from { background-position: 0px; }
    to { background-position: -1280px; }
}
/* Anim Punch END */

/* STAND ANIMATION */
#terrybogard .stand {
 width: 256px; /* Animation Container - width and height of a single frame */
 height: 256px;
 background: url(css/images-gimme-code-2013-0016/stand.png) no-repeat 0 0; /* A sprite as background image */
 /* Animation START */
    -webkit-animation: stand .9s steps(6, end) infinite;  /* 0.9 seconds delay time, 6 frames in the sprite sheet, infinite loop */
    -moz-animation: stand .9s steps(6, end) infinite;
    animation: stand .9s steps(6, end) infinite;
 /* Animation END */
}

/* PUNCH ANIMATION */
#terrybogard .punch {
 width: 256px; /* Animation Container - width and height of a single frame */
 height: 256px;
 background: url(css/images-gimme-code-2013-0016/punch-right.png) no-repeat 0 0; /* A sprite as background image */
 /* Animation START */
    -webkit-animation: punch .9s steps(5, end) infinite;  /* 0.9 seconds delay time, 5 frames in the sprite sheet, infinite loop */
    -moz-animation: punch .9s steps(5, end) infinite;
    animation: punch .9s steps(5, end) infinite;
 /* Animation END */
}
</style>

<script type="text/javascript" src="js/jquery-2.0.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){

  $("#buttonpunch").click(function(){
  $("div").removeClass("stand");
  $("div").addClass("punch");
  });
  
  $("#buttonstand").click(function(){
  $("div").removeClass("punch");
  $("div").addClass("stand");
  });
  
});
</script>
 
</head>
 
<body>
<p>JQuery - Interactive Sprite Sheet Animation - <button id="buttonstand">STAND</button> or <button id="buttonpunch">P U N C H !!!</button></p>
<div id="terrybogard">
<div class="stand">&nbsp;</div>
</div>
</body>
 
</html>
DEMO

 

GO TO LESSON 2

 

My official WebSite >