Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. //Changing the sprites' appearance
  2. //press the mouse and move the cursor to control the animations
  3.  
  4. var boxSprite;
  5. var imageSprite;
  6. var animatedSprite;
  7. var anotherAnimatedSprite;
  8.  
  9. function setup() {
  10. createCanvas(800,300);
  11.  
  12. //assets should be preloaded in the preload() function
  13. //to avoid delays in the visualization
  14. //but they can be loaded in setup() and draw() as well
  15. var img = loadImage("assets/asterisk.png");
  16. var animation = loadAnimation("assets/ghost_standing0001.png", "assets/ghost_standing0007.png");
  17.  
  18. //create a sprite with a placeholder rectangle as visual component
  19. boxSprite = createSprite(100, 150, 50, 100);
  20. //change the color of the placeholder
  21. boxSprite.shapeColor = color(222, 125, 2);
  22.  
  23. //create a sprite and associate an existing image as visual component
  24. //it is not necessary to specify the dimensions
  25. imageSprite = createSprite(300, 150);
  26. imageSprite.addImage(img);
  27.  
  28. //create a sprite and associate an existing animation as visual component
  29. //since a sprite can have multiple images and animations
  30. //the first parameter must be a label identifying the animation
  31. animatedSprite = createSprite(500, 150, 50, 100);
  32. animatedSprite.addAnimation("floating", animation);
  33.  
  34. //alternative usage:
  35. //create a sprite and associate a non existing animation to it
  36. //the first parameter must be a label
  37. anotherAnimatedSprite = createSprite(700, 150, 50, 100);
  38. anotherAnimatedSprite.addAnimation("breathing", "assets/cloud_breathing0001.png", "assets/cloud_breathing0005.png");
  39.  
  40. }
  41.  
  42. function draw() {
  43. background(255,255,255);
  44.  
  45. //all the methods and properties of the current animation will be
  46. //accessible from the .animation property of the sprite
  47.  
  48. //stop/play a sprite animation
  49. if(mouseIsPressed)
  50. animatedSprite.animation.stop();
  51. else
  52. animatedSprite.animation.play();
  53.  
  54. //change the frame in relation to the mouse x position
  55. var frame = round(map(mouseX, 0, width, 0, anotherAnimatedSprite.animation.getLastFrame()));
  56. //note: frames must be integer numbers so I have to round the result of map
  57.  
  58. anotherAnimatedSprite.animation.changeFrame(frame);
  59.  
  60. //draw all the sprites
  61. drawSprites();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement