Corona force app to suspend - coronasdk

Is there a way to suspend app from code? Just like pressing Win + arrow down on simulator. Some event maybe? I want to use it for debug only, so it has to work on simulator.

You just can "handling" the system events, but not suspend the app by code.
-- create a function to handle all of the system events
local onSystem = function( event )
if event.type == "applicationStart" then
print("start")
elseif event.type == "applicationExit" then
print("exit")
elseif event.type == "applicationSuspend" then
print("suspend")
elseif event.type == "applicationResume" then
print("resume")
end
end
-- setup a system event listener
Runtime:addEventListener( "system", onSystem )

Related

Music not muting when I press the GUI button but it will print the volume

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
while true do
wait()
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local d = b[c]
d:Play()
wait(d.TimeLength)
mutebutton.MouseButton1Click:Connect(function()
d.Volume = 0
end)
end
If i was to replace d.Volume = 0 with print(d.Volume) or print("testing") then it will work however when I change it to actually mute the audio, it dosent want to work. Anyone know why?
If you want to stop playing a Sound, I would suggest using the :Pause() method, like so:
mutebutton.MouseButton1Click:Connect(function()
d:Pause()
end)
The main problem with your script is that the MouseButton1Click:Connect (listening for clicks) is done only after the wait(d.TimeLength) (after the song has finished playing).
Adding on from ivy's suggestion of using d:Pause(), I would suggest doing something like this:
while true do
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local d = b[math.random(1, #b)]
d:Play()
local connection = mutebutton.MouseButton1Click:Connect(function()
if d.IsPaused then -- Makes the pause button a play button if the song is paused already
d:Resume()
else
d:Pause()
end
end)
d.Ended:Wait() -- Wait until music ends
connection:Disconnect() -- Remove connection (to prevent multiple functions on the same button click)
end

Is there a way to listen for any key press, and mute microphone?

I'm not familiar with LUA or hammerspoon, but I want to listen for any button on the keyboard being pressed.
I believe I can use hs.eventtap.event.newKeyEvent for this, but I'm not sure how to catch any and all key press. I don't care what is pressed, I just want to mute the microphone, and unmute it once there has been x number of seconds of no key being pressed.
Is this possible with hammerspoon? Please point me in the right direction.
I use the following to toggle my audio input devices between muted and unmuted.
function toggleMute()
local mic = hs.audiodevice.defaultInputDevice();
local state = not mic:muted()
hs.fnutils.each(hs.audiodevice.allInputDevices(), function(device)
device:setInputMuted(state)
end)
if mic:muted() then
hs.alert('Muted')
else
hs.alert('Unmuted')
end
end
local hyper = {"⌥", "⌃"}
hs.hotkey.bind(hyper, "m", function() toggleMute() end)
That will toggle between muted and unmuted when you press ⌥+⌃+m.
If you want to automatically mute after X seconds of no keyboard activity, then have a look at the hs.eventtap documentation.
https://www.hammerspoon.org/docs/hs.eventtap.event.html
You could set up a listener for key up (keyUp) or key down (key down) events.
keyboardTracker = hs.eventtap.new({ events.keyDown }, function (e)
...
end
keyboardTracker:start() // start monitoring the keyboard
keyboardTracker:stop() // stop monitoring the keyboard
To accomplish this:
create a timer for X seconds (see hs.timer)
start the timer
setup an event tap for the keyDown event type
start the event tap
every time you detect a key down event reset the timer
when the timer is triggered mute the audio input device and stop the timer
Once the timer has triggered it will mute the audio input devices and stop the timer. After that as soon as you press a key on the keyboard the timer will be restarted.
For example:
local timer = hs.timer.new(5, mute)
function mute()
hs.fnutils.each(hs.audiodevice.allInputDevices(), function(device)
device:setInputMuted(false)
end)
hs.alert('Muted')
timer:stop()
end
timer:start()
local events = hs.eventtap.event.types
keyboardTracker = hs.eventtap.new({ events.keyDown }, function (e)
timer:stop()
timer:start()
end)
keyboardTracker:start()
Reacting to mouse events is similar.

Lua Roblox API: How can I debounce a player/character that runs into a box

Background:
Bear minimal objects. Just a player and a part (rectangular prism) that's floating in the air and anchored.
I honestly don't understand what is happening under the hood so its hard to figure out. Without debounce, upon event firing, the callback function is invoked by connect() or the event handler (not sure), and, without debouncing, the function is invoked multiple times as print statements are repeated on the output box. So, with a variable (of type boolean) that stores a debounce flag, that solves it. I then try to "un-debounce" when the player's model is outside out of the box's model. But, I don't know how to do that properly.
Here's my code attempt:
local box = game.Workspace.BoxModel;
local debounce = false;
local function onTouchedDebounced()
if (debounce == false)
then
debounce = true;
print("Hello! onTouchedDebounced() has run.");
box.Touched:Connect(onTouchedDebounced));
end
end
local function onTouchedUndebounced()
if (debounce == true)
then
print("Hello! onTouchedUndebounced() has run.");
debounce = false;
end
end
box.Touched:Connect(onTouchedDebounced);
box.TouchEnded:Connect(onTouchedUndebounced);
The heart of what you're doing is sound : start blocking after the first event, and unblock some time later. If this were with button presses or mouse-clicks, your solution would work fine. This is complicated by the Touched event, as it fires with any part that touches it, and a player's character could have multiple touch points.
The Touched and TouchEndeded events give you a reference to the instance that has touched the part.
If the goal is to only fire the box event once per player or once while anyone is touching it, you can keep a dictionary of parts that are currently touching the box. When a part touches, you increment a counter. When a part stops, you decrement it. You only remove the debounced flag once all touch points have been removed.
local box = game.Workspace.BoxModel
local playersTouching = {} --<string playerName, int totalParts>
local function onTouchedDebounced(otherPart)
-- check that the thing that touched is a player
local playerModel = otherPart.Parent
if not playerModel:IsA("Model") then
warn(otherPart.Name .. " isn't a child of a character. Exiting")
return
end
-- check whether this player is already touching the box
local playerName = playerModel.Name
local total = playersTouching[playerName]
if total and total > 0 then
warn(playerName .. " is already touching the box")
playersTouching[playerName] = total + 1
return
end
-- handle a new player touching the box
playersTouching[playerName] = 1
-- Do a thing here that you only want to happen once per event...
print(string.format("Hello! onTouchedDebounced() has ran with %s and %s", playerName, otherPart.Name))
end
local function onTouchedUndebounced(otherPart)
-- decrement the counter for this player
local playerName = otherPart.Parent.Name
if playersTouching[playerName] == nil then
return
end
local newTotal = playersTouching[playerName] - 1
playersTouching[playerName] = newTotal
-- if the total is back down to zero, clear the debounce flag
if newTotal == 0 then
playersTouching[playerName] = nil
print(string.format("Hello! onTouchedUndebounced() has ran and %s has no more touching parts", playerName))
end
end
box.Touched:Connect(onTouchedDebounced)
box.TouchEnded:Connect(onTouchedUndebounced)

Vungle ads sometimes not caching - Corona

I've integrated Vungle video ads in my Corona Game it was working perfect but few days ago it stopped caching video ads completely, but after few days it stared it self without changing any thing in code. Now it again stopped caching video ads. I suspect this issue is either from Corona or Vungle side but am not sure.
local vungleInterstitial = "540e9681c7ec2b6d4400000e"
local videoCompletedCallback=nil
videoAddListener=function( event )
if (event.isError ) then
--Cached video ad not available for display
print("problem to show ad")
elseif event.type == "cachedAdAvailable" then
print("cachedAdAvailable")
elseif tostring(event.type) == "adView" then
print("called when add is viewed")
elseif event.type == "adStart" then
print("Corona ads started")
audio.pause()
elseif event.type == "adEnd" then
print("Corona ads ended")
videoCompletedCallback()
audio.resume()
end
end
showVideoAd = function(callbackFunc)
print("showVideoAd function called")
videoCompletedCallback=callbackFunc;
if ( vads.isAdAvailable() ) then
print(" if ( vads.isAdAvailable() ) then, is true")
print("vads.show")
vads.show( "interstitial", { isBackButtonEnabled = false } )
else
local alert = native.showAlert( "Video Ad", "The Video Ad is not available at the moment. Please Try after some time.", { "OK" } )
end
end
vads.init("vungle",vungleInterstitial,videoAddListener)
This might be related to geo or daily view limits.. can you try with vungleTest as your AppID? Also, if you have further questions, feel free to email me at tech-support#vungle.com
Cheers,
Jordyn / Vungle

corona function to show ad when loaded does not work

I am trying to load an interstitial ad before actually showing it so that I can avoid lag. However everything I have tried has failed. With this code no ad ever appears:
local ads = require( "ads" )
ads.init( "admob", "ca-app-pub-2823622942892345/4361536298", adListener )
ads.load( "interstitial", { appId="ca-app-pub-2823622942892345/4361536298",
testMode=false } )
local function adListener( event )
if ( event.isError ) then
--Failed to receive an ad
else
ads.show( "interstitial", { x=0, y=0, appId="ca-app-pub-2823622942892345/4361536298" } )
end
end
With this code the ad loads but with lag...
local ads = require( "ads" )
ads.init( "admob", "ca-app-pub-2823622942892345/4361536298", adListener )
ads.load( "interstitial", { appId="ca-app-pub-2823622942892345/4361536298", testMode=false } )
ads.show( "interstitial", { x=0, y=0, appId="ca-app-pub-2823622942892345/4361536298" } )
Does anyone now why it does not work? If so how can I resolve it?
In your first example, your adListener function won't work. Since it's a local function and declared after you use it the first time, it will be nil on that first use. That function needs to be higher up in your code before it's used the first time.
In your second example, the ads.load() is an asynchronous call, that means we know it can take a while to work, so control returns to your app immediately and then you call ads.show() before the as has loaded and therefore nothing shows.
There is a tutorial that may help you with this:
http://coronalabs.com/blog/2014/07/15/tutorial-implementing-admob-v2/

Resources