Mouse press bug in love2d lua - lua

The below is a self-explanatory love2d program sample of mine. The variable 'state' determines the state of the game, i.e, 'play' & 'menu'. The initial state is 'menu' and what is expected to happen here is when first right-clicked, the state changes to 'play', and on further second right-click the variable printMsg is set to true as a result of which a message is printed inside function love.draw().
function love.load()
state = 'menu'
end
function love.draw()
if printMsg == true then
love.graphics.print('mousepressed')
end
end
function love.mousepressed(x, y, button)
if state == 'menu' and button == 1 then
state = 'play'
end
if state == 'play' and button == 1 then
printMsg = true
end
end
I have 2 issues here:
On the first click itself the message is printed because the program tends to think that the first click is also the second click.
Without having to create a variable printMsg to actually print the message, I want to print the message at the instance the button is pressed. What I mean is:
function love.load()
state = 'menu'
end
function love.draw()
end
function love.mousepressed(x, y, button)
if state == 'menu' and button == 1 then
state = 'play'
end
if state == 'play' and button == 1 then
love.graphics.print('mousepressed')
end
end
but unfortunately, this prints nothing.

You enter the second if statement because you assign 'play' to state. hence the condition of the second if statement is true.
If only one of those things should happen:
if state == 'menu' and button == 1 then
state = 'play'
elseif state == 'play' and button == 1 then
love.graphics.print('mousepressed')
end
or
if button == 1 then
if state == 'menu' then
state = 'play'
elseif state == 'play' then
love.graphics.print('mousepressed')
end
end
or if you can only have those two options you can omit one of the conditions:
if button == 1 then
if state == 'menu' then
state = 'play'
else
love.graphics.print('mousepressed')
end
end
Note that this print will not result in any output. By default Love2d will clear the screen befor invoking love.draw. So anything you print outside love.draw is not taken into account.
Either exclusively draw in love.draw or avoid clearing the frame buffer by implementing your own love.run.

The standard color for text is: Black
And the standard color for background is: Black
I guess therefore it is printed but you cant see it.
Try love.graphics.setColor(1, 1, 1, 1) before love.graphics.print() in love.draw().
( Before each drawable )
Look: https://love2d.org/wiki/love.graphics.setColor

Related

You were kicked from the experience nil Roblox

It kicks me with coding this script.
function attach()
-- Will be called when a backdoor is found
notify("Backdoor Found")
settings.Found = true
STATUS.Text = "Attached: "..settings.BackdoorRemote:GetFullName()
for _, button in pairs(SIDEBAR:GetChildren()) do
if button.ClassName == "ImageButton" and button ~= uiIndex.CurrentButton then
button.ImageColor3 = Color3.fromRGB(255, 255, 255)
end
end end
How?
I tried to attach it with my own gui button, when i press that button. it doesnt work, It kicks me with "nil" value.
Do not tell me ImageColor3, ImageButton, Color3, etc is not displayed
I want to exploit into my game. or Inactive Backdoored Games.
Like is there no argument or something?

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

Global back button

I'm working on a game in which I have a bunch of overlays and one scene which is called game.lua. I wanted to make it, when I press back button (hardware button on Android device), game would, if there's an overlay, close the overlay and if there's no overlay (just game.lua scene), it would show exitmenu.lua(simple pop-up menu).
function onKeyEvent( event )
if(event.keyName == "back") then
local CurrentScene = composer.getSceneName("current")
local CurrentOverlay = composer.getSceneName("overlay")
if CurrentScene == "Scenes.game" and CurrentOverlay == nil then
composer.showOverlay("Scenes.exitmenu", {isModal = true})
return true
elseif CurrentOverlay ~= nil and CurrentOverlay ~= "Scenes.exitmenu" then
composer.hideOverlay("fade", 500)
return true
end
end
end
Runtime:addEventListener("key", onKeyEvent)
What happens is, when I press back button while overlay is on, function hides the overlay and also shows exitmenu.lua overlay. I have no idea what is wrong with my code, any advice is highly appreciated.
EDIT: I've fixed it! I needed to add and event.phase == "down", which makes my button press do function only once. That was the fix.

stopping multiple collision which causes multiple events

I am making a game and I am having problems where, when the character jumps on an object, a series of events happen. But, because it collides with the object multiple times, the events happen multiple times, which is what I do not want. What I want is for the event to happen once after the character jumps on the object and stays on top of the object. THEN, let the event happen again ONCE when the character jumps on another object.... etc.
Here is part of my code that is relevant:
function playerCollision( self, event )
--if hit bottom column, u get points
if event.target.type == "player" and event.other.type == "startColumn2" then
if event.phase == "began" then
print ("collided")
addColumns()
timer.performWithDelay(5000, addBody)
startColumn2: translate(-4, 0)
startcolumn2hit = true
end
end
if event.target.type == "player" and event.other.type == "bottomColumn" then
print ("hit column")
onPlatform = true
end
end
How would I make it so I can prevent multiple collisions?
On the first collision you can set a boolean flag on the object the player collided with. Here I'm adding alreadyCollided and check if we already have collided with it:
function playerCollision( self, event )
--if hit bottom column, u get points
if event.target.type == "player" and event.other.type == "startColumn2" then
if event.phase == "began" then
addColumns()
timer.performWithDelay(5000, addBody)
startColumn2: translate(-4, 0)
startcolumn2hit = true
end
end
if event.target.type == "player" and event.other.type == "bottomColumn" and not event.other.alreadyCollided then
event.other.alreadyCollided = true
-- Increase score etc...
end
end

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

Resources