Hi to all, I say immediately that I'm Italian and so I apologize for my English.
I'm using this code for generating an animation but it repeat infinity times:
Frames = gideros.class(Sprite)
function Frames:init(frameList)
self.frames = {}
for i = 1, #frameList do
self.frames[i] = Bitmap.new(TextureRegion.new(Texture.new(frameList[i])))
end
self.nframes = #frameList
-- to find the bounding box we add all childs and then remove them
for i = 1, #self.frames do
self:addChild(self.frames[i])
end
local width = self:getWidth()
local height = self:getHeight()
for i = 1, #self.frames do
self:removeChild(self.frames[i])
end
for i = 1, #self.frames do
self.frames[i]:setX(-width/2)
self.frames[i]:setY(-height/2)
end
self.frame = 1
self:addChild(self.frames[1])
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
function Frames:onTimer()
self:removeChild(self.frames[math.floor(self.frame)])
self.frame = self.frame + 0.1
if self.frame >= self.nframes + 1 then
self.frame = 1
end
self:addChild(self.frames[math.floor(self.frame)])
end
end
function Frames:onEnterFrame()
self:removeChild(self.frames[math.floor(self.frame)])
self.frame = self.frame + 0.1
if self.frame >= self.nframes + 1 then
self.frame = 1
end
self:addChild(self.frames[math.floor(self.frame)])
local x = self:getX()
local y = self:getY()
x = application:getContentWidth() / 2
if (y > 165) then y = y else if (self.frame < 15) then y = y else y = y + 6 end end
self.timer = Timer.new(590, 0)
self.timer:addEventListener(Event.TIMER, self.onTimer, self)
self.timer:start()
self:setX(x)
self:setY(y)
end
How i can repeat it only one time??
Comments
I would try this code:
there is a difference between the = and the ==.
The single = is used for assignment, like
Likes: QuasarCreator
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
if you want that the init function is run only once, then you can have a simple thing like
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
It looks like the line " self.frames = Frames.new(frameList)" is actually called inside the init function (which is called from Frames.new) so your actually re-calling the init function again from within the init function and you've got a recursive infinite loop which will hang the player until you finally run out of memory and crash.
Likewise your onTimer function is declared within the init function and whilst this would still work as your effectively adding a function to the Frames table, it's very bad practice and should be moved OUT of the function and placed at the same level as the other Frame: functions.
Functions declared within functions should be considered local to the scope of the function they were declared in - unless using them as closures (but I'd stay away from that area for a while if I were you)
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Lastly I'm not 100% sure what your trying to achieve here, but you might want to search the forums for examples of how to use the MovieClip library - also if you want to progress - start simple, test your code and make sure you fully understand what's going on before trying to progress too far, then make a small addition, test, understand, repeat...
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
frames.lua:8: attempt to get length of local 'frameList' (a nil value)
stack traceback:
frames.lua:8: in function 'init'
[string "property.lua"]:151: in function 'new'
frames.lua:66: in main chunk
Obviously it has to be a table of some sort - if you've not defined the "frameList" value that I posted AS AN EXAMPLE to show you how to call the code then it will be nil.
The clue was in the "Outside of the class do something LIKE this to start the ball rolling..." comment line.
At the end of the day - it's your code, if you don't understand how to call it (or what to call it with) then I suggest you read (and re-read) the final paragraph of my previous post.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
[rant]
A sure fire way of getting members to ignore your questions would be to jump right in and say please fix my code or please write my code for me and then expect someone to do the work for you.
If someone tries to help by giving you some pointers and example code it is really impolite to then tell them that their code doesn't work. Members help each other, we don't write for you, we may contribute code to the whole community but as you have seen from the above examples, the onus is on you to understand your own code or at least what you want to achieve.[/rant]
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
The code you actually want to fix for this logic is in the onEnterFrame Function or the onTimer function as you appear to have two copies of it.
So first off get rid of that timer--you are repeating the code that is already handled inside the Frames:onEnterFrame function. Then make a change to the code below to stop the event from firing once the animation is finished.
As shown here--you will need to edit you file's function to look like this below:
Eli
I Haven't tested this code but reading through and commenting it makes it more understandable and you can see any obvious errors. There don't appear to be any
Hope this helps.
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
@techdojo makes a very important point as most of the time the device is using 60 fps, which makes it very cool to actually know the speed in which your animations will play on most devices--which when I first started playing with Gideros confused me a lot actually heh