If object passes y value then call function - lua

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

Related

How do I run a function once when it is being called continuously

Pretty much, I have a joystick controlling movement of my character, it works fine for left and right being called continuously but when I try and run the function for jumping, it will continously run and keep calling the function
https://imgur.com/a/nwCYUyz
Here is an image of what the level looks like, and the joystick
I've tried have an if jumping is false
```THIS IS WHERE THE FUNCTION IS CALLED
if (nob.y > event.y+40) then
jump()
print("up")
```Function
function jump()
if(player.isJumping ~= true ) then
print("jumpin")
player:applyLinearImpulse( 0, -15 *player.mass)
player.isJumping = true
player:setSequence("jump")
player:play()
print("Jumped # ", system.getTimer())
end
end
What I want to happen is for the character to jump only when it lands and not continously

Love2d isDown equivalent for touch control?

I have this code for mouse:
if love.mouse.isDown(1) then
self.player:fire()
end
So this fires the gun as long as the mouse button is held down. I have onscreen buttons for touch which I can capture individual touches with love.touchpressed but this fires the gun one at a time.
I want it so that the player can touch the onscreen button and as long as it is held down the gun fires continuously.
Use:
if next(love.touch.getTouches()) ~= nil then
self.player:fire()
end
or define function is_empty or any:
function is_empty(t)
return next(t) == nil
end
if not is_empty(love.touch.getTouches()) then
self.player:fire()
end
function any(t)
return next(t) ~= nil
end
if any(love.touch.getTouches()) then
self.player:fire()
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.

Getting a button reference?

I'm working on an app in Lua + Corona. As a complete beginner, I've managed to hack together a little script for a carousel, but now I've got a question.
function forwardButtonPress()
if carousel.getCurImage() < #myImages then
carousel.slideToImage(carousel.getCurImage() + 1)
end
end
function backButtonPress()
if carousel.getCurImage() > 1 then
carousel.slideToImage(carousel.getCurImage() - 1)
end
end
--Here's where we do the actual initilization of the page.
local fwbutton = display.newImage("buttonArrow.png")
fwbutton.x = 260
fwbutton.y = 120
fwbutton:addEventListener("tap", forwardButtonPress )
local bkbutton = display.newImage("buttonBackArrow.png")
bkbutton.x = 60
bkbutton.y = 120
bkbutton:addEventListener("tap", backButtonPress )
If you look at the code, you'll see that I have two buttons, a back button and a forward one. Those are for sliding the images. So, say you get to the end of the carousel. The script takes care of making sure that it doesn't go past the end already, but how do I access the button to set the alpha to zero or fade it? It's linear, so I can't just put the button above its event function, so that the event function can reference the button... is there a way to pass the event function a reference to the button?
You can forward declare the event handler functions like this at the top of the file:
local forwardButtonPress
local backButtonPress
Then create your buttons and attach the event handlers (This is your code copied & pasted):
local fwbutton = display.newImage("buttonArrow.png")
fwbutton.x = 260
fwbutton.y = 120
fwbutton:addEventListener("tap", forwardButtonPress )
local bkbutton = display.newImage("buttonBackArrow.png")
bkbutton.x = 60
bkbutton.y = 120
bkbutton:addEventListener("tap", backButtonPress )
Add a function to manage setting the appearance of the buttons when either button is clicked:
local function setButtons()
if carosel.getCurImage() < #myImages then
fwbutton.alpha = 1.0
else
fwbutton.alpha = 0.5
end
if carosel.getCurImage() > 1 then
bkbutton.alpha = 1.0
else
bkbutton.alpha = 0.5
end
end
Now, you can write the function implementations, which will be able to deal with the buttons via the setButtons function:
forwardButtonPressed = function()
if carousel.getCurImage() < #myImages then
carousel.slideToImage(carousel.getCurImage() + 1)
end
setButtons()
end
backButtonPress = function()
if carousel.getCurImage() > 1 then
carousel.slideToImage(carousel.getCurImage() - 1)
end
setButtons()
end
Disclaimer: I'm not able to test this now, so there might be a syntax error somewhere, but organizing the code this way will work for what you're doing.
You can create/define the buttons above the function, and attach the EventListener below, no? If not, I don't really understand the problem.

Resources