Advertisement
Guest User

super-mario

a guest
Jun 6th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <reference path="_references.js" />
  2.  
  3. window.onload = function () {
  4.     "use strict";
  5.  
  6.  
  7.     var stage = new Kinetic.Stage({
  8.         container: 'container',
  9.         width: 1440,
  10.         height: 960
  11.     });
  12.  
  13.     var layer = new Kinetic.Layer();
  14.  
  15.     var imageObj = new Image();
  16.  
  17.     imageObj.onload = function () {
  18.         var superMario = new Kinetic.Sprite({
  19.             x: 450,
  20.             y: 600,
  21.             image: imageObj,
  22.             animation: 'idle',
  23.             animations: {
  24.                 idle: [
  25.                   // x, y, width, height (4 frames)
  26.                   340, 980, 170, 250,
  27.                   1130, 1040, 180, 200
  28.                 ],
  29.                 move: [
  30.                   // x, y, width, height (3 frames)
  31.                   560, 985, 240, 260,
  32.                   25, 970, 235, 265,
  33.                   860, 660, 200, 220
  34.                 ]
  35.             },
  36.             frameRate: 2,
  37.             frameIndex: 0
  38.         });
  39.  
  40.         // add the shape to the layer
  41.         layer.add(superMario);
  42.  
  43.         // add the layer to the stage
  44.         stage.add(layer);
  45.  
  46.         // start sprite animation
  47.         superMario.start();
  48.  
  49.         var frameCount = 0;
  50.  
  51.         superMario.on('frameIndexChange', function (evt) {
  52.             if (superMario.animation() === 'move' && ++frameCount > 3) {
  53.                 superMario.animation('idle'); // restore original animation
  54.                 superMario.scaleX(1); // restore original animation
  55.                 frameCount = 0;
  56.             }
  57.         });
  58.  
  59.         function onKeyDown(evt) {
  60.             switch (evt.keyCode) {
  61.                 case 37:  // left arrow
  62.                     superMario.setX(superMario.attrs.x -= 50);
  63.                     superMario.attrs.animation = "move";
  64.                     break;
  65.                 case 39:  // right arrow
  66.                     superMario.setX(superMario.attrs.x += 50);
  67.                     superMario.scaleX(-1); // this scale reverses the mario
  68.                     superMario.attrs.animation = "move";
  69.                     break;
  70.             }
  71.         }
  72.  
  73.         window.addEventListener('keydown', onKeyDown);
  74.     };
  75.  
  76.     imageObj.src = "images/super-mario-sprite.png";
  77.  
  78.     var paper = new Raphael(0, 0, 1440, 960);
  79.  
  80.     paper.image("images/super-mario-background.png", 0, 0, 1440, 960);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement