Corona SDK addEventListener - addeventlistener

Is anyone could help to sort out this Error?
AddEventListener has a problem to called function...
--ship as a custom attribute with the value of false
rects[i][j].ship = false
--Add an event listener to get tappedRect called
rects:addEventListener("tap" ,tappedRect)
end
end
--TappedRect function use when a rectangle gets tapped on
function tappedRect(event)
if event.target.ship then
event.target.ship = true
event.target:setFillColor(0, 255, 0)
else
event.target = false
event.target:setFillColor(255, 0, 0)
end
end

in corona you can only add an event listener that calls a method after that method is defined (you have to call addEventListener after defining tappedRect)

Related

Corona SDK: Tap and Touch + Glitch

local held = false
local function jumperTap ()
jumper:applyForce( 0, 200, jumper.x, jumper.y )
return false
end
Runtime:addEventListener( "tap", jumperTap )
local function holdListener(event)
held = true
jumper:applyForce( 0, 250, jumper.x, jumper.y )
return true
end
local function jumperTouch(event)
if (event.phase == "began") then
display.getCurrentStage():setFocus(jumper)
holdTimer = timer.performWithDelay( 500, holdListener )
elseif (event.phase == "moved") then
timer.cancel(holdTimer)
elseif (event.phase == "ended" or event.phase == "cancelled") then
display.getCurrentStage():setFocus(nil)
timer.cancel(holdTimer)
held = false
end
end
Runtime:addEventListener( "touch", jumperTouch )
I'm trying to have a tap and a touch and hold. When the touch and hold happens, the jumper will have more force applied to him so he can jump higher when the screen is touched and held. When the screen is tapped, he will have a shorter jump.
When I tap, the expected thing happens. When I tap and hold, the expected thing happens. I do have a few glaring issues though due to my novice-ness in Corona. They are...
- When I tap and hold, all goes well, but when I release, it glitches and what is performed is what seems to be a the tap event. Not sure why this is happening
- When I perform the tap event, I am able to perform it again while the object is in the air--this brings him down to the ground and the tap event seems to be performed again, but with less force.
Any and all help is greatly appreciated!
EDIT: I put return true and return false in just to try something different, but it didn't affect anything.
I recommend testing out a debounce in order to prevent the tap/tap+hold events from doubling up. By passing the functions through a global boolean you can make it so they can only tap while no tap events are occuring. Because based on your events it seems they happen regardless if they are currently in 'jump' mode or not.
Example:
debounce = false
function func1()
if debounce == false then
debounce = true
//event script
end
end
function event_ended()
debounce = false
end

If object passes y value then call function

I have an object that is moving down progressively and I want the touch function to be called whenever the object passes a certain Y.
So I have a if condition that checks if square.y is bigger than 150. If that condition is true, I want to be able to call a touch function.
How can i do that?
I have tried doing this, but it's not working.
if (square.y > 150) then
function square:touch (event)
// do something
end
end
You defined the function square:touch, but you didn't call it. You need to call it using square:touch():
if square.y > 150 then
function square:touch (event)
print("calling touch")
end
square:touch()
end
You can also define the function outside the if statement if you want, to make it more clear:
function square:touch (event)
print("calling touch")
end
if square.y > 150 then
square:touch()
end

The image object attached to event listener is nil

--up in the level1.lua
local target
--in the enter frame function of scene
function target:touch(event)
if event.phase=="began" then
local target=display.newImage("target.png",event.x,event.y)
return true
end
end
function target:touch(event)
You have not created target yet. You cannot assign a touch handler to an object that doesn't exist yet.
It sounds like what you need to do is add a touch handler to the stage. I would pre-create the image and just hide it using .isVisible = true. Then in your touch handler, show and hide the object. But regardless you have to put the touch handler on the whole screen and not an individual small image.
Remove the "local" in target:touch: it hides the module local, using a variable local to target:touch(). Also, if you want image to disappear after touch done, use the "ended" and "cancelled" phases of touch event. Finally, I'm assuming that you initialized target to something but if not, you must add that too, otherwise how can you define touch:event (thanks to Rob for noticing this btw):
-- first create the target, but don't show it:
local target = display.newImage("target.png", 0, 0)
target.isVisible = false
--in the enter frame function of scene
function target:touch(event)
if event.phase=="began" then
target.x = event.x
target.y = event.y
return true
else if event.phase == "ended" or event.phase == "cancelled" then
if target ~= nil then
target:removeSelf()
target = nil
end
return true
end
end

LUA/Corona sdk: storyboard.gotoScene() not working

I'm new to Corona and Lua, so pleas be patient.
So i have a main screen, that I go to from my main file, and it works fine. Then i want to go back to that sceen from a diffrent sceen, and nothing happens (but the "win" is spaming my consol window).
The win function:
function win()
print("win!");
storyboard.gotoScene( "MSCREEN.mscreen" );
end
Function where i'm calling it:
function scene:enterFrame(inEvent)
--some other stuff
if (ball.x>display.contentWidth and failFlag==false) then
win();
end
end
and my main sceen:
local scene = storyboard.newScene();
local bg;
local text;
function scene:createScene(inEvent)
bg = display.newImage( "MSCREEN/bg.png");
bg.x = display.contentWidth / 2;
bg.y = display.contentHeight / 2;
text=display.newText( "Touch!", 0,0, fontName, 70);
text.x =display.contentWidth / 2 ;
text.y =display.contentHeight / 2 +230;
self.view:insert(bg);
self.view:insert(text);
end
function scene:enterFrame(inEvent)
tekstBujany(text);
end
function scene:touch(inEvent)
if inEvent.phase == "ended" then
storyboard.gotoScene( "JUMP.GJump" );
end
end
function scene:destroyScene(inEvent)
bg:removeSelf();
bg=nil;
text:removeSelf();
text=nil;
end -- End destroyScene().
scene:addEventListener("createScene", scene);
scene:addEventListener("destroyScene", scene);
Runtime:addEventListener("enterFrame", scene);
Runtime:addEventListener("touch",scene);
return scene;
SOLVED
so adding
storyboard.purgeOnSceneChange = true;
did the trick, not sure why.
Be careful with the win() function, it looks like it gets called a lot of times. Set another variable and change it in the method.
function win()
isNotWin = false -- so this method doesn't get called a lot of times.
print("win!")
storyboard.gotoScene( "MSCREEN.mscreen" )
end
function scene:enterFrame(inEvent)
--some other stuff
if (ball.x>display.contentWidth and failFlag==false and isNotWin == true) then
win();
end
end
The "enterFrame" event gets generated at every frame for the Runtime object, so about 30 to 60 times / sec. So win() will be called at every frame if possible. Since win() causes a transition to a new scene, if storyboard.purgeOnSceneChange = false then enterFrame gets called for each scene, leading to constant stream of win() calls and constant stream of transitions to the same scene "MSCREEN.mscreen" (this can't be good). OTOH if you set storyboard.purgeOnSceneChange = true then the previous scene, here the one with enterFrame that calls win(), is purged so win() will only get called once.
In conclusion, setting storyboard.purgeOnSceneChange = true may work but I think there is a problem with the logic you have there: a scene transition should not be called at every frame! If you are monitoring for some condition to declare a win, you should either remove the enterFrame callback in win() or ensure the condition is false after win() called once:
function scene:enterFrame(inEvent)
if (ball.x > display.contentWidth and failFlag == false) then
win();
Runtime:removeEventListener("enterFrame", scene);
end
end
With the guard variable it looks like this:
local winCalled = false
function win()
winCalled = true
...
end
function scene:enterFrame(inEvent)
if (ball.x > display.contentWidth and failFlag == false) then
win();
end
end

dragging of loqSprite object in corona

i am new to using loqSprite, i am trying to drag a loqSprite sprite object but its not getting done , however it calls its listener only once and then after neither its touch listner is getting called nor even it gives any error, the sprite is playing. Also i thought that might my drag/listener function might be buggy but when i tried the same dragging (movePen() function ) on the inbult corona's Sprite object it works fine. what i am missing i dont know . Can anyone please help me .... below is the code snippet. thanks
local function movePen(event)
local targetObj= event.target;
if event.phase == 'began' then
display.getCurrentStage():setFocus(targetObj);
targetObj.isFocus = true;
targetObj.y = event.y;
elseif event.phase == 'moved' then
targetObj.x = event.x;
targetObj.y = event.y;
elseif event.phase == 'ended' then
display.getCurrentStage():setFocus(nil);
targetObj.isFocus = false;
end
return true;
end --end of touch/move function
local spriteFactoryForPen = loqsprite.newFactory('penAnimation')
local penSpriteAnim = spriteFactoryForPen:newSpriteGroup('pen_write')
penSpriteAnim.x = 100
penSpriteAnim.y = 200
local function spriteEvent (e) --listener to play in loop
if(e.phase == "end") then
penSpriteAnim:play()
end
end -- end of sprit event function
penSpriteAnim:addEventListener("touch", movePen); -- adding listener to move pen object
penSpriteAnim:addEventListener("sprite", spriteEvent) -- adding listener to play in loop
penSpriteAnim:play('pen_write') -- playing pen Sprite
First of all there is no need to call penSpriteAnim:play() in loop. Because it will automatically play in loop untill you don't call the penSpriteAnim:pause() function.
For your touch listener, You should declare all the local variable on top of the page.
I am not sure about this, but hope this will work. Because lua is compiling top to bottom.

Resources