briarfox56

Codea Sprite Animation

Feb 28th, 2013
153
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. --# Main
  3. -- Animation Test
  4. --Cant find who the original code was from
  5. --Zombie sprite from West
  6. -- Modified by Briarfox
  7.  
  8. --sprite download http://db.tt/eMJa1tsS
  9.  
  10. function setup()
  11. print("Simple Animation")
  12. displayMode(FULLSCREEN)
  13. img = readImage("Dropbox:ZombieSpriteSheet") --set the sprite sheet here
  14. zombie = SimpleSprite(img,5,5) --instance of the class that holds all the animations
  15.  
  16. --loads individual animations loadAnimation(the name,vec2 of position on screen,vec2 of size,speed,column of
  17. --spritesheet, row of spritesheet
  18. zombie:loadAnimation("test",vec2(WIDTH/2,HEIGHT/2),vec2(150,150),5,0,4)
  19. zombie:loadAnimation("test1",vec2(100,600),vec2(50,50),16,0,3)
  20. zombie:loadAnimation("test2",vec2(400,300),vec2(100,100),5,0,2)
  21. zombie:loadAnimation("test3",vec2(800,400),vec2(200,200),3,0,3)
  22. zombie:loadAnimation("test4",vec2(175,175),vec2(175,175),30,0,4)
  23. zombie:loadAnimation("test5",vec2(485,700),vec2(125,125),7,0,4)
  24. zombie:loadAnimation("test6",vec2(950,100),vec2(100,100),10,0,3)
  25.  
  26. end
  27.  
  28. -- This function gets called once every frame
  29. function draw()
  30. -- This sets a dark background color
  31. background(255, 255, 255, 255)
  32.  
  33. zombie:draw()
  34. -- This sets the line thickness
  35. strokeWidth(5)
  36.  
  37. -- Do your drawing here
  38.  
  39. end
  40.  
  41.  
  42. --# SimpleSprite
  43. --This was from someone else's work on the forums, his original code did not have his name and I cant find the post.
  44. --This will load one sprite sheet and allow you to create animations for each row on the sheet
  45.  
  46.  
  47. SimpleSprite = class()
  48.  
  49. function SimpleSprite:init(img,cols,rows) --img of the sprite sheet, cols = columns on sheet, rows = rows on sheeet
  50. -- you can accept and set parameters here
  51. self. m = mesh()
  52. self.m.texture = img --sprite sheet
  53. self.animation = {} --animation table to hold the different animations
  54. self.numacross = rows -- number of sprite rows
  55. self.numdown = cols -- number of sprite columns
  56.  
  57. -- animdelay=0
  58.  
  59. end
  60.  
  61. function SimpleSprite:draw()
  62. -- Codea does not automatically call this method
  63. --background(40, 40, 50)
  64. self.m:clear()
  65. noSmooth()
  66.  
  67. for u,v in pairs(self.animation) do
  68. local idx = self.m:addRect(self.animation[u].coords.x,self.animation[u].coords.y,self.animation[u].size.x,self.animation[u].size.y)
  69. self.m:setRectTex(idx,self.animation[u].xanim*(1/self.numacross), self.animation[u].yanim*(1/self.numdown),(1/self.numacross),(1/self.numdown))
  70. if self.animation[u].animdelay>self.animation[u].animspeed then
  71. self.animation[u].xanim = self.animation[u].xanim + 1
  72. self.animation[u].animdelay=0
  73. end
  74. if self.animation[u].xanim>self.numacross-1 then
  75. self.animation[u].xanim=0
  76. end
  77. self.animation[u].animdelay = self.animation[u].animdelay + 1
  78. self.m:draw()
  79.  
  80. end
  81. end
  82.  
  83. function SimpleSprite:touched(touch)
  84. -- Codea does not automatically call this method
  85. end
  86.  
  87. function SimpleSprite:loadAnimation(name,screenCoords,sSize,speed,row,col)
  88. self.animation[name] = {
  89. animspeed = speed,
  90. xanim = row,
  91. yanim = col,
  92. coords = screenCoords,
  93. size = sSize,
  94. animdelay = 0
  95. }
  96. end
RAW Paste Data