Hi folks,
here is a slightly modified sprite.lua file that will let you set the speed of the animation. To do this, call
the setSpeed method with the speed factor. 1.0 means the animation will be updated every frame, 0.5 means every second frame and so on. I hope it is usefull. I will try to extend this so it becomes framerate independant.
Here is the code, if anyone knows how to preserve the code indention on this forum, then please let me know.
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 | -- -- Abstract: sprite library -- -- Version: 1.0 -- -- Disclaimer: IMPORTANT: This ANSCA software is supplied to you by ANSCA Inc. -- ("ANSCA") in consideration of your agreement to the following terms, and your -- use, installation, modification or redistribution of this ANSCA software -- constitutes acceptance of these terms. If you do not agree with these terms, -- please do not use, install, modify or redistribute this ANSCA software. -- -- In consideration of your agreement to abide by the following terms, and subject -- to these terms, ANSCA grants you a personal, non-exclusive license, under -- ANSCA's copyrights in this original ANSCA software (the "ANSCA Software"), to -- use, reproduce, modify and redistribute the ANSCA Software, with or without -- modifications, in source and/or binary forms; provided that if you redistribute -- the ANSCA Software in its entirety and without modifications, you must retain -- this notice and the following text and disclaimers in all such redistributions -- of the ANSCA Software. -- Neither the name, trademarks, service marks or logos of ANSCA Inc. may be used -- to endorse or promote products derived from the ANSCA Software without specific -- prior written permission from ANSCA. Except as expressly stated in this notice, -- no other rights or licenses, express or implied, are granted by ANSCA herein, -- including but not limited to any patent rights that may be infringed by your -- derivative works or by other works in which the ANSCA Software may be -- incorporated. -- -- The ANSCA Software is provided by ANSCA on an "AS IS" basis. ANSCA MAKES NO -- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED -- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR -- PURPOSE, REGARDING THE ANSCA SOFTWARE OR ITS USE AND OPERATION ALONE OR IN -- COMBINATION WITH YOUR PRODUCTS. -- -- IN NO EVENT SHALL ANSCA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR -- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR -- DISTRIBUTION OF THE ANSCA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF -- CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF -- ANSCA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- -- Copyright (C) 2009 ANSCA Inc. All Rights Reserved. -- -- Modified by Michael Hartlef, April 28th, 2010 -- Added setSpeed method, which sets the speed of animation. -- -- sprite.lua (currently includes Anim class only) module(..., package.seeall) local sprMFloor = math.floor -- added by M.Hartlef April 28th, 2010 local sprMCeil = math.ceil -- added by M.Hartlef April 28th, 2010 function newAnim (imageTable) -- Set up graphics local g = display.newGroup() local animFrames = {} local animLabels = {} 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 animSpeed = 1.0 -- added by M.Hartlef April 28th, 2010 local animTime = 1.0 -- added by M.Hartlef April 28th, 2010 local nextFrame = function() animFrames[currentFrame].isVisible = false animTime = animTime + animSpeed -- added by M.Hartlef April 28th, 2010 --currentFrame = currentFrame + 1 -- removed by M.Hartlef April 28th, 2010 currentFrame = sprMFloor(animTime) -- added by M.Hartlef April 28th, 2010 if (currentFrame > #animFrames) then currentFrame = 1 animTime = 1.0 -- added by M.Hartlef April 28th, 2010 end animFrames[currentFrame].isVisible = true end local prevFrame = function() animFrames[currentFrame].isVisible = false animTime = animTime - animSpeed -- added by M.Hartlef April 28th, 2010 --currentFrame = currentFrame - 1 -- removed by M.Hartlef April 28th, 2010 currentFrame = sprMCeil(animTime) -- added by M.Hartlef April 28th, 2010 if (currentFrame < 1) then currentFrame = #animFrames animTime = #animFrames -- added by M.Hartlef April 28th, 2010 end animFrames[currentFrame].isVisible = true end ------------------------ -- Define public methods local repeatFunction function g:setSpeed(s) -- added by M.Hartlef April 28th, 2010 animSpeed = s end function g:play() Runtime:removeEventListener( "enterFrame", repeatFunction ) repeatFunction = nextFrame Runtime:addEventListener( "enterFrame", repeatFunction ) end function g:reverse() Runtime:removeEventListener( "enterFrame", repeatFunction ) repeatFunction = prevFrame Runtime:addEventListener( "enterFrame", repeatFunction ) end function g:stop() Runtime:removeEventListener( "enterFrame", repeatFunction ) end function g:stopAtFrame(label) -- This works for either numerical indices or optional text labels if (type(label) == "number") then Runtime:removeEventListener( "enterFrame", repeatFunction ) 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", repeatFunction ) animFrames[currentFrame].isVisible = false currentFrame = k animFrames[currentFrame].isVisible = true end 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 |
Mike,
I am getting a load of error with this and the animation will not play, example error from terminal window:-
prite.lua:82: attempt to perform arithmetic on upvalue 'animSpeed' (a nil value)
Mmmh, can you send me the sprite lua file to mike at fantomgl.com ? Maybe you made an error when you copied the code. Here it works fine.
i'm having an odd issue. with beta 1 and beta 2 this worked perfectly fine, but now when i use the same code block (shown below) on beta 3 it seems that the animation progressively speeds up until it becomes like its on crack and lightning fast.
characterEyes:setSpeed(.4);
characterTimer = timer.performWithDelay(3500,
function()
characterEyes:play();
timer.performWithDelay(1200, function() characterEyes:stop(); end, 1);
end, 0);
Your code looks fine to me.
the code is ok, but when i run in corona beta 3 simulator it looks like its on crack and speeds up exponentially (not sure why) but the same code run on beta 2 and beta 1 works fine.
Just checked it with ANSCAs movieclip example. There it shows the same weird behaviour.
Edit: Thsi is already filed as a bug in beta 3.
Mike can you put the speed controls back into the movieclip.lua library as I was just upgrading my project to use the new copy that comes with beta 4 to get the movable and click-able animation and my code failed of course no speed control.
So the sprites are back to acting all crazy as I only have 5 frames but that's all I need.
I really think this is poor that Ansca have not sorted this out at this stage come on guys...I have paid my$99 and there has been three beta's three betas's and still no speed controll this is a really basic thing.
These things should not have to be coded by others.
Hi Richard,
I will do that.
Cheers
Michael
You Hero! ;-)