It looks like you're new here. If you want to get involved, click one of these buttons!
--[[ Linear move function ]]--
local function move(sprite, vx, vy)
local x, y = sprite:getPosition()
local dx = (vx or 0) * (1 / 60)
local dy = (vy or 0) * (1 / 60)
local function action()
x = x + dx
y = y + dy
sprite:setPosition(x, y)
end
sprite:addEventListener(Event.ENTER_FRAME, action)
return action
end
--[[ Start move action ]]--
-- Make a sprite
local theSprite = Sprite.new()
-- Make varaible and set it's value to the move function. The move
-- function will return the reference to the listener function so
-- we can stop the action when needed.
-- Supply a sprite along with velocity x and y (pixels per second)
local theAction = move(theSprite, 100, 0)
--[[ End move action ]]--
-- Remove the event listener
theSprite:removeEventListener(Event.ENTER_FRAME, theAction)
Pretty simple approach. We call the move function that contains an enter frame listener that executes the move chunk. The move function returns the reference to the listener so we can then destroy it when desired.
--[[ The Enter Frame listener ]]--
local actions = {}
local function onEnterFrame(event)
for someAction in pairs(actions) do
someAction()
end
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
--[[ Linear move function ]]--
local function move(sprite, vx, vy)
local x, y = sprite:getPosition()
local dx = (vx or 0) * (1 / 60)
local dy = (vy or 0) * (1 / 60)
local function action()
x = x + dx
y = y + dy
sprite:setPosition(x, y)
end
actions[action] = true
return action
end
--[[ Start move action (same as before) ]]--
local theSprite = Sprite.new()
local theAction = move(theSprite, 100, 0)
--[[ End move action ]]--
-- Remove action from actions table
actions[theAction] = nil
The move part is the same, but now we have an external table to store actions, along with a listener function to execute those actions. So we call the move function like before, then the move function ads the action as a key to the actions table and returns the reference back to you. The single listener calls every key in the actions table as a function. We can then destroy the action with a nil at the key equal to the reference.Likes: ArtLeeApps, incipient, Harrison, ar2rsawseen
Comments
http://artleeapps.com/
Bubble Adventure - Colors
What I do is have one main thread and process everything in one go - using only one listener (the 60hz event).
https://deluxepixel.com
I apologize if it came across as if I was suggesting that. My goal was to explain how we could take advantage of locals (upvalues) to make the move of a sprite more efficient.
As to how one goes about moving many sprites, I think it really depends on the design of the game.
Sorry for the confusion
https://deluxepixel.com
https://deluxepixel.com