[Solved] Having Problems Porting A Simple Gamesalad Project To Corona. Any Help Is Appreciated!

Hi all,

For the past few days I’ve been trying to rebuild what I did in GameSalad in Corona. At the very beginning what seemed to be an easy project proved to be way harder than I thought. Here is what I did in GS;

http://www.youtube.com/watch?v=rQ3oiYnr8Jo

And here is what I did in Corona;

http://www.youtube.com/watch?v=_qg_zUpb1g8

It is pretty close but I can’t get it working while mouse (touch) is moving and pressed. I tried to use timelimit (system.gettimer()) and few other methods but I failed everytime.

Here is my full code;

\_H = display.contentHeight \_W = display.contentWidth local Y2 = 630 local X2 = \_W/2 circle = display.newCircle( X2, Y2, 10 ) markTime = os.time() print ( markTime ) function SpawnBall() --print ( "spawnball" ) circle = display.newCircle( X2, Y2, 10 ) end function MoveBall(event) print ( event.x ) print ( event.y ) local X1 = event.x local Y1 = event.y local m1 = (Y2 - Y1)/(X2 - X1) --The Slope local TargetX = (-2000 - Y2 + ( m1 \* X2 ) ) / m1 --Calculate the Target X coordinate according to Y=2000 coordinate local ballTravelDistance = math.sqrt( ((-2000 - Y2) \* (-2000 - Y2)) + ((TargetX - X2) \* (TargetX - X2)) ) --The distance? transition.to( circle, { time=ballTravelDistance\*3, x=TargetX, y=-2000} ) timer.performWithDelay( 100, SpawnBall) end local amItouching = false local function onScreenTouch(event) markTime = system.getTimer() doit = function() MoveBall(event) end if (event.phase ~= "moved") then if (event.phase == "ended") then amItouching = false print ( amItouching ) elseif (event.phase == "began") then amItouching = true print ( amItouching ) end if (amItouching==true) then cycle = timer.performWithDelay(90, doit, 0) else timer.cancel ( cycle ) end end end --function Runtime:addEventListener( "touch", onScreenTouch )

I know it is a lot to ask to re-write or even to ask to study the code and tell what I’m doing wrong but I just can’t do it and I need help. I already had some awesome people helping me with this code. Maybe I just need to read a lot more tutorials before I do something like this…

I think this will work, I will try it asap when I het home;

If(event.phase == moved) then
TimePassed = os.now - markTime

if(TimePassed > 100) then
MoveBall(event)
markTime = os.now()
end
end

BUT, the problem with this is I can obly track time in Seconds, how can I do it in miliseconds?

Hi @cbt,

I think a better method would be a repeating timer (which can be controlled at the millisecond level, of course). When the user touches the screen, the timer begins, and on each iteration you fire a bullet in the direction of the touch point. When the user lifts the touch off, the timer stops (or rather, pauses, since you’ll be using it again). Will that accomplish what you need?

Best regards,

Brent

I did it! Here is the working code!

local function trackTime( event ) --Gets called by "onScreenTouch" function in an enterFrame eventlistener. Every 'timeThreshold' microseconds, does a job (function) -- print("I'm in da function bitch!") local secondsPassed = (event.time - markTime) --print("X = " .. mouseX .. " Y = " .. mouseY) if secondsPassed \>= timeThreshold then print( "You held down the button for at least " .. timeThreshold .. " miliseconds." ) MoveBall(event) markTime = system.getTimer() end end local function onScreenTouch(event) -- Marks the time when touch is pressed and adds an enterFrame eventlistener calling "trackTime" function. Removes eventlistener when touch is removed. if event.phase == "began" then markTime = system.getTimer() mouseX = event.x mouseY = event.y Runtime:addEventListener( "enterFrame", trackTime ) print("began") elseif event.phase == "ended" or event.phase == "cancelled" then -- stop tracking time Runtime:removeEventListener( "enterFrame", trackTime ) print("removed") end if event.phase == "moved" then mouseX = event.x mouseY = event.y end end --function

Thank you everyone! Now that is 0.05% of the project. And I asked only 10 questions! :smiley:

I think this will work, I will try it asap when I het home;

If(event.phase == moved) then
TimePassed = os.now - markTime

if(TimePassed > 100) then
MoveBall(event)
markTime = os.now()
end
end

BUT, the problem with this is I can obly track time in Seconds, how can I do it in miliseconds?

Hi @cbt,

I think a better method would be a repeating timer (which can be controlled at the millisecond level, of course). When the user touches the screen, the timer begins, and on each iteration you fire a bullet in the direction of the touch point. When the user lifts the touch off, the timer stops (or rather, pauses, since you’ll be using it again). Will that accomplish what you need?

Best regards,

Brent

I did it! Here is the working code!

local function trackTime( event ) --Gets called by "onScreenTouch" function in an enterFrame eventlistener. Every 'timeThreshold' microseconds, does a job (function) -- print("I'm in da function bitch!") local secondsPassed = (event.time - markTime) --print("X = " .. mouseX .. " Y = " .. mouseY) if secondsPassed \>= timeThreshold then print( "You held down the button for at least " .. timeThreshold .. " miliseconds." ) MoveBall(event) markTime = system.getTimer() end end local function onScreenTouch(event) -- Marks the time when touch is pressed and adds an enterFrame eventlistener calling "trackTime" function. Removes eventlistener when touch is removed. if event.phase == "began" then markTime = system.getTimer() mouseX = event.x mouseY = event.y Runtime:addEventListener( "enterFrame", trackTime ) print("began") elseif event.phase == "ended" or event.phase == "cancelled" then -- stop tracking time Runtime:removeEventListener( "enterFrame", trackTime ) print("removed") end if event.phase == "moved" then mouseX = event.x mouseY = event.y end end --function

Thank you everyone! Now that is 0.05% of the project. And I asked only 10 questions! :smiley: