How do I stop Music from playing with my stop button? - lua

I am creating two buttons, using Corona SDK, one for playing music and the other for stopping. The program works fine up until i create the stop button and nothing works. There is no audio. Can someone help me resolve this please?
local widget = require("widget")
display.setStatusBar(display.HiddenStatusBar)
centerX = display.contentWidth * .5
centerY = display.contentHeight * .5
-- Background
local bg = display.newImageRect("bg_iPhone.png", 1100, 3200)
bg.x = centerX
bg.y = centerY
local isAudioOn = true
local soundJump = audio.loadSound("boing.mp3") --[[loadSound is for animations]]--
local soundMusic = audio.loadStream("HappyPants.wav") --[[loadStream is for background music]]--
--Sound function to play music
local function playSound()
if isAudioOn then
audio.play(soundMusic)
print("Boing!")
end
end
-- Button Function that controls what happens after button is pressed
local function buttonHit(action)
if action == "play" then
playSound()
elseif action == "stop" then
audio.stop(playSound)
end
end
-- Play Button
local playButton = widget.newButton{
label = "Play",
id = "play",
x = 330,
y = 500,
labelColor = { default={ 0, 19, 189 }, over={ 0, 19, 189, 1 } },
onPress = buttonHit
}
-- Stop Button
local stopButton = widget.newButton{
label = "Stop",
id = "stop",
x = 330,
y= 550,
labelColor = { default={ 0, 19, 189 }, over={ 0, 19, 189, 1 } },
onPress = buttonHit
}

Your buttonHit function is wrong.
You pass in the argument action, however, since you are using widget library, the only thing that gets passed into the function is event. Also, you've given the buttons an id, not an action. This id then belongs to the event target, i.e. the button that is being pressed.
What you want is something along the lines of:
local function buttonHit( event )
if event.target.id == "play" then
playSound()
else
audio.stop(playSound)
end
end

Related

How do I set the visibility of a widget to false?

So, I have this function:
local function addMainMenu()
local widget = require( "widget" )
-- Function to handle button events
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
scene = "GAME"
end
end
-- Create the widget
local button1 = widget.newButton(
{
label = "button",
onEvent = handleButtonEvent,
emboss = false,
-- Properties for a rounded rectangle button
shape = "roundedRect",
width = 200,
height = 40,
cornerRadius = 2,
fillColor = { default={0.9,0.9,0.9,1}, over={1,0.1,0.7,0.4} },
strokeColor = { default={0,0,0,1}, over={0.8,0.8,1,1} },
strokeWidth = 5
}
)
-- Center the button
button1.x = display.contentCenterX
button1.y = display.contentCenterY
-- Change the button's label text
button1:setLabel( "Start Game" )
end
That adds the button to start the game, and then I have:
local function enterFrame()
local dt = getDeltaTime()
if (scene == "MAIN_MENU") then
addMainMenu()
elseif (scene == "GAME") then
if (running == false) then
startGame()
else
moveBg(dt)
moveEnemy(enemy)
updateScore()
end
elseif (scene == "GAME_OVER") then
local gameOverLabel = display.newText( "Game Over!", 50, 20, native.systemFont, 16)
gameOverLabel:setFillColor(1, 1, 1)
end
end
As you can see, once I click on the button to start, the scene changes to "GAME", and the button should go away. The thing is: It stays there. And reading the docs, I can't find a way to set its visibility to false. How can I cease displaying a widget?
To make it not visible use .isVisible:
button1.isVisible = false
To hide it use .alpha
button1.alpha = 0.00
-- or hide just a little by 50%
button1.alpha = 0.50
To remove it:
display.remove( button1)
button1= nil
A ButtoWidget inherits from GroupObject which inherits from DisplayObject which provides the property isVisible
Overview
Controls whether the object is visible on the screen. The
property is also readable. The default is true.
Example
local rect1 = display.newRect( 100, 100, 50, 50 )
rect1:setFillColor( 0.7 )
local rect2 = display.newRect( 150, 100, 50, 50 )
rect2:setFillColor( 1, 0, 0, 0.6 )
rect2.isVisible = false
button1.isVisible = false will hide the button. It will disappear with the next screen update.
In case you don't need the button anymore you can as well just remove it by calling button1:removeSelf() or by removing it from it's parent group

Creating clones/spawns/unique duplicates in Corona SDK

I'm an extreme beginner to Corona SDK, and I am currently attempting to make a sound board, where the screen displays multiple buttons, and each button you tap makes a different sound. I am using a process of duplicating an image and having each duplicate play a sound, but I ran into some problems.
Is there a way that I can create "clones" of display objects? What I mean, is that I want to spawn multiple images on the screen, each having some sort of unique value so when one of them is clicked, I will be able to recognize which one.
Try this out:
local function onClickButton( event )
local button = event.target
if event.phase == "ended" then
audio.stop() -- Stop ALL current channels
audio.play( button.stream )
end
end
local function createButton( params )
local x = params.x or 0
local y = params.y or 0
local audio_location = params.audio or "my_sound.mp3"
local button = display.newRect( x, y, 50, 50 )
button.stream = audio.loadStream( audio_location )
button:addEventListener( "touch", onClickButton )
end
createButton( { x = 100, y = 100, audio = "my_sound.mp3" } )
createButton( { x = 200, y = 100, audio = "my_sound_2.mp3" } )
createButton( { x = 100, y = 200, audio = "my_sound_3.mp3" } )
createButton( { x = 200, y = 200, audio = "my_sound_4.mp3" } )
Windows Phone doesn't support MP3's so have that in mind if you're planning on target that market as well:
https://docs.coronalabs.com/guide/media/audioSystem/index.html
You can also use modular classes with metatables but I don't think it's necessary in this case but here is more information about that:
https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/

Corona: go to scene when event loop if else statement variable matches

ok so I have this.
local quotetap = 30
function changet()
if tImage then tImage:removeSelf() end
if counterBlock then counterBlock:removeSelf() end
tImage = display.newImage( "images/tFaces_"..math.random(6)..".jpg", 264, 280 )
tImage.x = display.contentWidth * 0.5
tImage.y = display.contentHeight * 0.5
counter = counter + 1
counterBlock = display.newText(counter, 30, 30, native.systemFont, 25)
counterBlock.x = display.contentWidth /7 *6
counterBlock.y = display.contentHeight /10
tImage:addEventListener("tap", changet)
end
I want to add.
if (counter == quotetap) then
composer.gotoScene( "scenes.nextlevel", "fade", 500 )
end
mind you, the counter works perfectly. When add this however, i notice the friction when the counter hits 30. But it doesn't change scene like it should. After a slight pause, it just continues counting.
ah I've figured out what's happening. Sorry for the meaningless question. Seems it actually is changing scenes. The scene i switch to is currently blank. So it actually is switching, and not clearing the old objects. So it appears that it is staying on the same scene, when it actually is not.
You need to include the parameters (time, effect) in a params table. Look here https://docs.coronalabs.com/api/library/composer/gotoScene.html
Change
composer.gotoScene( "scenes.nextlevel", "fade", 500 )
To
local options = {
effect = "fade",
time = 500,
}
composer.gotoScene( "scenes.nextlevel", options )
Alternatively:
composer.gotoScene("scenes.nextlevel", { effect = "fade", time = 500, })

MapView to background - Corona SDK

I need some help with the MapView in Corona SDK. I want to put the map as background (but with all functions) and, in this case, a button to the front. Here is my code:
function scene:createScene( event )
local group = self.view
if ( system.getInfo( "environment" ) == "simulator" ) then
local simulatorMessage = "Maps not supported in Corona Simulator.\nYou must build for iOS or Android to test native.newMapView() support."
local label = display.newText( simulatorMessage, 36, 40, 270, 0, native.systemFont, 12 )
label.anchorX = 0
label.anchorY = 0
end
myMap = native.newMapView( 20, 20, display.contentWidth, display.contentHeight )
if ( myMap ) then
myMap.mapType = "normal"
myMap.x = display.contentCenterX
myMap.y = display.contentCenterY
myMap:setCenter( currentLatitude, currentLongitude )
myMap:addEventListener("mapLocation", mapLocationListener)
end
backBtn = widget.newButton
{
id="backButton",
label="Back",
fontSize="30",
labelColor={ default={1, 1, 1}, over={0, 0, 0, 0.5} },
defaultFile="images/button.png",
width = dispWidth * 0.25, height = dispHeight * 0.1,
onRelease = onBackBtnRelease
}
backBtn.anchorX = 0.5
backBtn.anchorY = 0.5
backBtn.x = display.contentWidth * 0.8
backBtn.y = dispHeight * 0.8
backBtn:toFront()
group:insert( backBtn )
group:toFront() --second try to put that button to front
end
Since the MapView is a native object I can't add it to any group and 'mapView:toBack()' doesn't work as well ('attempt to index upvalue 'myMap' (a nil value)'). In Corona SDK the button appears without the map, as expected. On device I get the map without my button, not as expected.
Is it any possible to put a native object to the back, or to force something to be in the front?
What you are trying to do with the map and the button is not currently possible with CoronaSDK.
Indeed the MapView is a native object. According to Corona's documentation, you cannot place other objects on top of a MapView.
The documentation says:
Map objects, like other native display objects, cannot be inserted into groups and are always displayed on top of regular Display Objects (groups, vector, images, and text).
See documentation here
Good luck coding.
First Check the Following :
If button doesn't appear on device most common problem will be , you might misspell the Image names(Check for Caps) or check the path of the file is proper or not.
To know in detail about the mapView : http://docs.coronalabs.com/api/library/native/newMapView.html
I know you might have visited already, its better you look again.
Thanks,

Lua - Corona SDK - Text not appearing inside a display Object

I am trying to move a display object that is hiding over the right side of the screen, into the scene. It works wonderfully with images (i.e. the background), but not with texts (the coords seem correct from debugging them with print(), but they never display, I already tried the obj:toFront).
I thought that they may work inside display objects, so I put everything in a display Object: Nothing. Just the text? Neither. Anyone knows why/how to override this?
function tscreen:init()
local textGroup = display.newGroup()
local menuBackground = self:getBtn("src/bgMenu.png")
menuBackground.isVisible = false
menuBackground.anchorX = 0.5
menuBackground.anchorY = 0.5
self.menuBackground = menuBackground
local optionsText = {
parent = textGroup,
text = "Hello World",
x = centerX,
y = centerY,
width = 128,
font = native.systemFontBold,
fontSize = 14,
align = "center"
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 0, 0 )
setPos(textGroup, W, 0)
--setPos() is a custom function that assigns x and y coords
textGroup.isVisible = false
self.textGroup = textGroup
end
function tscreen:show()
local menuBackground = self.menuBackground
local textGroup = self.textGroup
local inTime = 1200
setPos(menuBackground, 2*W + centerX, centerY)
menuBackground.isVisible = true
setPos(textGroup, W, 0)
textGroup.isVisible = true
self:cancelTween(menuBackground)
self:cancelTween(textGroup)
menuBackground.tween = transition.to(menuBackground, {time = inTime, transition = easing.outExpo, x = centerX,
onComplete = function()
tscreen:cancelTween(menuBackground)
end
})
textGroup.tween = transition.to(textGroup, {time = inTime, transition = easing.outExpo, x = 0,
onComplete = function()
tscreen:cancelTween(textGroup)
print(getPos(textGroup), textGroup.width, textGroup.height)
end
})
end
I have the starters edition of Corona, so I don't have the recently implemented Composer API.
Maybe this isn't the most appropriate site to post this query since there already is a Corona SDK forum, but I'm trying anyway.
I'm not seeing anything wrong, but a group should not be necessary. Verify that the text can be seen ever: in the init(), do
local optionsText = {
text = "Hello World",
x = 0,
y = 100,
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 1, 1 )
If you can't see the text then something else is going on, maybe your init() is not being called or such. Once you see it, change the parameters to what you want (fill color etc), and test. If still works, add a transition, right after, in the init():
local easeXto100 = {
time = 2000,
transition = easing.outExpo,
x = 100,
onComplete = function() print('did you see text move from x=0 to 100?') end
}
transition.to(workText, easeXto100)
If you see it move, then move the relevant parts of code to your show(), if now it disappears this will give you clue.

Resources