Question

local W = application:getLogicalWidth() 
local H = application:getLogicalHeight()
local xD, yD, speed = 1, 1, 10 
local img = newImage("myImage2.png", 0, 0) 
local wd, ht = img:getWidth(), img:getHeight()

function onEnterFrame(event) 
    local xP, yP = img:getPosition()
    xP = xP + xD*speed yP = yP + yD*speed 

    if xP >= W-wd or xP == 0 then
        xD = −xD
    end

    if yP >= H-ht or yP == 0 then
        yD = −yD
    end 

    img:setPosition(xP, yP)
end

img:addEventListener(Event.ENTER_FRAME, onEnterFrame)

I have above piece of code and my question is why Event.ENTER_FRAME here? All this code does is move the ball across screen, we are not changing any frame,then why do I need this? What is the difference if I use it without Event.ENTER_FRAME ?

Was it helpful?

Solution

Event.ENTER_FRAME is called once for every frame Gideros runs. If you were to run the above code without it, it would just run through the code once, the ball will move ,and then the ball will cease to move. a example of enterframe usage (see #6)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top