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
Related
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
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/
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, })
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.
The code sample below is from a single storyboard scene. A magnifying glass (mg), a green circle (asset1), a return button and an animated ship (ship) are all added to the scene's display group as it unfolds. During this, the ship is also removed again with display.remove(ship).
This all works fine the first time the scene is created, however, when I click the return button, make a scene change, and then return to this scene, the code fails when it attempts to add the ship to the display group: "attempt to call method 'insert' (a nil value)"
What gives?
local storyboard = require ("storyboard")
local widget = require ("widget")
local scene = storyboard.newScene()
storyboard.purgeOnSceneChange = true
function scene:createScene(event)
local group=self.view
local asset1x = 600
local asset1y = 400
-- MAGNIFYING GLASS
local mg=display.newImage( "images/mg.jpg" )
mg.x=600
mg.y=150
mg.myName = "mg"
physics.addBody(mg,"dynamic", { density = 1.0, friction = 0.3, bounce = 0.2, radius = mgRadius})
group:insert(mg)
-- ASSET PLANT
local asset1 = display.newCircle(asset1x,asset1y,30)
asset1:setFillColor(100,200,300)
physics.addBody(asset1,"static")
asset1.myName = "asset1"
asset1.isSensor = true
asset1.isVisible = false
group:insert(mg)
asset1.collision = function(self,event)
if (event.phase == "began" and event.other.myName == "mg") then
print("began")
ship=display.newSprite( shipSheet, shipSeqData )
ship:play()
ship:scale(2,2)
ship.x=asset1x
ship.y=asset1y
ship.myName = "ship"
group:insert(ship)
elseif (event.phase == "ended" and event.other.myName == "mg") then
print("ended")
display.remove(ship)
end
end
asset1:addEventListener("collision")
-- RETURN BUTTON
local function returnWelcome(event)
storyboard.gotoScene("000","fade", 600)
end
local returnButton = widget.newButton
{
left=900,
top=385,
width=300,
height=225,
defaultFile="images/bg000.jpg",
onRelease=returnWelcome,
}
group:insert(returnButton)
end
scene:addEventListener( "createScene", scene )
return scene
It turns out I accidentally added the mg display object to the display group twice and never added asset1 - this somehow caused the bug.