1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | -- movieclip.lua (a convenience library for assembling animated sprites from separate images) module(..., package.seeall) function newAnim (imageTable) -- Set up graphics local g = display.newGroup() local animFrames = {} local animLabels = {} local limitX, limitY, transpose local i = 1 while imageTable[i] do animFrames[i] = display.newImage(imageTable[i]); g:insert(animFrames[i], true) animLabels[i] = i -- default frame label is frame number animFrames[i].isVisible = false i = i + 1 end -- show first frame by default animFrames[1].isVisible = true ------------------------- -- Define private methods local currentFrame = 1 local totalFrames = #animFrames local startFrame = 1 local endFrame = #animFrames local loop = 0 local loopCount = 0 local remove = false local dragBounds = nil local dragLeft, dragTop, dragWidth, dragHeight -- flag to distinguish initial default case (where no sequence parameters are submitted) local inSequence = false local function resetDefaults() currentFrame = 1 startFrame = 1 endFrame = #animFrames loop = 0 loopCount = 0 remove = false end local function resetReverseDefaults() currentFrame = #animFrames startFrame = #animFrames endFrame = 1 loop = 0 loopCount = 0 remove = false end local function nextFrame( self, event ) animFrames[currentFrame].isVisible = false currentFrame = currentFrame + 1 if (currentFrame == endFrame + 1) then if (loop > 0) then loopCount = loopCount + 1 if (loopCount == loop) then -- stop looping currentFrame = currentFrame - 1 animFrames[currentFrame].isVisible = true Runtime:removeEventListener( "enterFrame", self ) if (remove) then -- delete self (only gets garbage collected if there are no other references) self.parent:remove(self) end else currentFrame = startFrame animFrames[currentFrame].isVisible = true end else currentFrame = startFrame animFrames[currentFrame].isVisible = true end elseif (currentFrame > #animFrames) then currentFrame = 1 animFrames[currentFrame].isVisible = true else animFrames[currentFrame].isVisible = true end end local function prevFrame( self, event ) animFrames[currentFrame].isVisible = false currentFrame = currentFrame - 1 if (currentFrame == endFrame - 1) then if (loop > 0) then loopCount = loopCount + 1 if (loopCount == loop) then -- stop looping currentFrame = currentFrame + 1 animFrames[currentFrame].isVisible = true Runtime:removeEventListener( "enterFrame", self ) if (remove) then -- delete self self.parent:remove(self) end else currentFrame = startFrame animFrames[currentFrame].isVisible = true end else currentFrame = startFrame animFrames[currentFrame].isVisible = true end elseif (currentFrame < 1) then currentFrame = #animFrames animFrames[currentFrame].isVisible = true else animFrames[currentFrame].isVisible = true end end local function dragMe(self, event) local onPress = self._onPress local onDrag = self._onDrag local onRelease = self._onRelease if event.phase == "began" then display.getCurrentStage():setFocus( self ) g._startX = event.x - g.x g._startY = event.y - g.y if onPress then result = onPress( event ) end elseif event.phase == "moved" then if transpose == true then -- Note: "transpose" is deprecated now that Corona supports native landscape mode -- dragBounds is omitted in transposed mode, but feel free to implement it if limitX ~= true then g.x = g._startX - (event.yStart - event.y) end if limitY ~= true then g.y = g._startY + (event.xStart - event.x) end else if limitX ~= true then g.x = event.x - g._startX if (dragBounds) then if (g.x < dragLeft) then g.x = dragLeft end if (g.x > dragLeft + dragWidth) then g.x = dragLeft + dragWidth end end end if limitY ~= true then g.y = event.y - g._startY if (dragBounds) then if (g.y < dragTop) then g.y = dragTop end if (g.y > dragTop + dragHeight) then g.y = dragTop + dragHeight end end end print("g" , g.x, g.y, event.x, event.y) end if onDrag then result = onDrag( event ) end elseif event.phase == "ended" then display.getCurrentStage():setFocus( nil ) if onRelease then result = onRelease( event ) end end -- stop touch from falling through to objects underneath return true end ------------------------ -- Define public methods function g:enterFrame( event ) self:repeatFunction( event ) end function g:play( params ) Runtime:removeEventListener( "enterFrame", self ) if ( params ) then -- if any parameters are submitted, assume this is a new sequence and reset all default values animFrames[currentFrame].isVisible = false resetDefaults() inSequence = true -- apply optional parameters (with some boundary and type checking) if ( params.startFrame and type(params.startFrame) == "number" ) then startFrame=params.startFrame end if ( startFrame > #animFrames or startFrame < 1 ) then startFrame = 1 end if ( params.endFrame and type(params.endFrame) == "number" ) then endFrame=params.endFrame end if ( endFrame > #animFrames or endFrame < 1 ) then endFrame = #animFrames end if ( params.loop and type(params.loop) == "number" ) then loop=params.loop end if ( loop < 0 ) then loop = 0 end if ( params.remove and type(params.remove) == "boolean" ) then remove=params.remove end loopCount = 0 else if (not inSequence) then -- use default values startFrame = 1 endFrame = #animFrames loop = 0 loopCount = 0 remove = false end end currentFrame = startFrame animFrames[startFrame].isVisible = true self.repeatFunction = nextFrame Runtime:addEventListener( "enterFrame", self ) end function g:reverse( params ) Runtime:removeEventListener( "enterFrame", self ) if ( params ) then -- if any parameters are submitted, assume this is a new sequence and reset all default values animFrames[currentFrame].isVisible = false resetReverseDefaults() inSequence = true -- apply optional parameters (with some boundary and type checking) if ( params.startFrame and type(params.startFrame) == "number" ) then startFrame=params.startFrame end if ( startFrame > #animFrames or startFrame < 1 ) then startFrame = #animFrames end if ( params.endFrame and type(params.endFrame) == "number" ) then endFrame=params.endFrame end if ( endFrame > #animFrames or endFrame < 1 ) then endFrame = 1 end if ( params.loop and type(params.loop) == "number" ) then loop=params.loop end if ( loop < 0 ) then loop = 0 end if ( params.remove and type(params.remove) == "boolean" ) then remove=params.remove end else if (not inSequence) then -- use default values startFrame = #animFrames endFrame = 1 loop = 0 loopCount = 0 remove = false end end currentFrame = startFrame animFrames[startFrame].isVisible = true self.repeatFunction = prevFrame Runtime:addEventListener( "enterFrame", self ) end function g:nextFrame() -- stop current sequence, if any, and reset to defaults Runtime:removeEventListener( "enterFrame", self ) inSequence = false animFrames[currentFrame].isVisible = false currentFrame = currentFrame + 1 if ( currentFrame > #animFrames ) then currentFrame = 1 end animFrames[currentFrame].isVisible = true end function g:previousFrame() -- stop current sequence, if any, and reset to defaults Runtime:removeEventListener( "enterFrame", self ) inSequence = false animFrames[currentFrame].isVisible = false currentFrame = currentFrame - 1 if ( currentFrame < 1 ) then currentFrame = #animFrames end animFrames[currentFrame].isVisible = true end function g:currentFrame() return currentFrame end function g:totalFrames() return totalFrames end function g:stop() Runtime:removeEventListener( "enterFrame", self ) end function g:stopAtFrame(label) -- This works for either numerical indices or optional text labels if (type(label) == "number") then Runtime:removeEventListener( "enterFrame", self ) animFrames[currentFrame].isVisible = false currentFrame = label animFrames[currentFrame].isVisible = true elseif (type(label) == "string") then for k, v in next, animLabels do if (v == label) then Runtime:removeEventListener( "enterFrame", self ) animFrames[currentFrame].isVisible = false currentFrame = k animFrames[currentFrame].isVisible = true end end end end function g:playAtFrame(label) -- This works for either numerical indices or optional text labels if (type(label) == "number") then Runtime:removeEventListener( "enterFrame", self ) animFrames[currentFrame].isVisible = false currentFrame = label animFrames[currentFrame].isVisible = true elseif (type(label) == "string") then for k, v in next, animLabels do if (v == label) then Runtime:removeEventListener( "enterFrame", self ) animFrames[currentFrame].isVisible = false currentFrame = k animFrames[currentFrame].isVisible = true end end end self.repeatFunction = nextFrame Runtime:addEventListener( "enterFrame", self ) end function g:setDrag( params ) if ( params ) then if params.drag == true then limitX = (params.limitX == true) limitY = (params.limitY == true) transpose = (params.transpose == true) dragBounds = nil if ( params.onPress and ( type(params.onPress) == "function" ) ) then g._onPress = params.onPress end if ( params.onDrag and ( type(params.onDrag) == "function" ) ) then g._onDrag = params.onDrag end if ( params.onRelease and ( type(params.onRelease) == "function" ) ) then g._onRelease = params.onRelease end if ( params.bounds and ( type(params.bounds) == "table" ) ) then dragBounds = params.bounds dragLeft = dragBounds[1] dragTop = dragBounds[2] dragWidth = dragBounds[3] dragHeight = dragBounds[4] end g.touch = dragMe g:addEventListener( "touch", g ) else g:removeEventListener( "touch", g ) dragBounds = nil end end end -- Optional function to assign text labels to frames function g:setLabels(labelTable) for k, v in next, labelTable do if (type(k) == "string") then animLabels[v] = k end end end -- Return instance of anim return g end |