Remove items lua/Corona - lua

Very quick, and probably easy question... but its left me stumped for the last 30 minutes... But how can I remove items from the canvas/screen?
I know this works with removeSelf(), but I'm not quite sure how to use that in my example:
display.setStatusBar( display.HiddenStatusBar )
function cat1()
displayCategory(1)
print( "clicked 1" )
end
function cat2()
displayCategory(2)
print( "clicked 2" )
end
function cat3()
displayCategory(3)
print( "clicked 3" )
end
--category display
function displayCategory(cat)
if (cat == nil) then
cat = 1
end
print( cat )
if (cat == 1) then
local item1 = display.newRect(100,100,100,100)
item1:setFillColor(255,255,0)
elseif (cat == 2) then
local item2 = display.newRect(200,100,100,100)
item2:setFillColor(255,0,255)
elseif (cat == 3) then
local item3 = display.newRect(300,100,100,100)
item3:setFillColor(0,255,255)
end
end
--category buttons
local catBtn1 = display.newRect(0,0,50,50)
catBtn1:setFillColor( 255,0,0 )
catBtn1:addEventListener( "tap", cat1 )
local catBtn2 = display.newRect(60,0,50,50)
catBtn2:setFillColor( 0,255,0 )
catBtn2:addEventListener( "tap", cat2 )
local catBtn3 = display.newRect(120,0,50,50)
catBtn3:setFillColor( 0,0,255 )
catBtn3:addEventListener( "tap", cat3 )
Right now, every time I click on of my buttons, the corresponding item pops up, but doesnt disappear when I click on any of the other buttons. How would I go about that?

First, create those itens on start, before the displayCategory function.
Then you set them to not visible, like this:
local item1 = display.newRect(100,100,100,100)
item1:setFillColor(255,255,0)
item1.isVisible = false
Later you set the proper itens to visible or not as appropriate, in case of item 2 it would be
item1.isVisible = false
item2.isVisible = true
item3.isVisible = false

Related

how can i use " id = tostring(i) " in Corona and Lua

i recently saw something like this
if(ebasRating_Arr[i] == 0) then
radioButton_0 = widget.newSwitch {
left = 565,
style = "radio",
initialSwitchState = true,
id = tostring(i),
width = 60,
height = 60,
onPress = setEBASRating0
}
radioGroup:insert( radioButton_0 )
radioButton_1 = widget.newSwitch {
style = "radio",
id = tostring(i),
initialSwitchState = false,
width = 60,
height = 60,
onPress = setEBASRating1
}
radioGroup:insert( radioButton_1 )
notice the "id = tostring(i)" in line 6 and 13. So i am experimenting this id = tostring(i) wanting to find out what it means. Can someone tell me what it really means? SO the second part of my question how can i use something like id = tostring(i) in my code here
local madrs = display.newGroup()
local function textListener( event )
if ( event.phase == "began" ) then
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
-- Output resulting text from "defaultField"
print( event.target.text )
elseif ( event.phase == "editing" ) then
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
end
end
The i is a number in the for loop you used.The id of the button should be in string format so that it will be identified by corona sdk as a name for things like scenes.Example
composer.gotoScene("level"..tostring(event.target.id),{effect="fade"});
The above line goes to a scene named level1,if the id of the button is 1.It should not be an integer or float.It should be a string so that corona sdk will treat it as a name of a scene
Bro i don't understand you question or the second code(since i havenot learnt about radio buttons),But let me share what i know....Say that you want to know the id of the radioButton_0 and convert it into number.The code will be like below
print(radioButton_0.id); --> prints a string (0) --
print(tonumber(radioButton_0.id));--> prints a number (0)
I dont know about radio buttons.If you tell me your motive(application) of your above code,I can help

Refresh menu scene after data change (Corona SDK)

I have just implemented adbuddiz reward video in my app and it works as expected but I have a slight challenge.
After watching the video, it triggers the "didComplete" event for adbuddiz where it adds the needed value into a json file.
The challenge now is the value does not automatically reflect on the screen except I go out of the scene and go back before the new value shows.
Is there a way to refresh the screen so the new value appears immediately?
Note: This all happens on a button click
local savecount = Lib.getSaveValue("savecount")
-- Events (rewarded video)
local function listenerRewardedVideo( event )
if event.value == "didFetch" then
print( "didFetch" )
end
if event.value == "didComplete" then
print( "didComplete" )
-- Reward user here
print ("savecount")
savecount = savecount + 1
Lib.setSaveValue("savecount", savecount, true);
local alert = native.showAlert("Successful","Count saved successfully!", {"OK"})
end
if event.value == "didNotComplete" then
print( "didNotComplete" )
end
if event.value == "didFail" then
print( "didFail - " .. event.detail )
end
end
Runtime:addEventListener( "AdBuddizRewardedVideoEvent", listenerRewardedVideo )
This displays the value on the menu scene
label_saveme = display.newText({parent=uiGroup, text=savecount, x=0, y=0, font=native.systemFont, fontSize=24})
label_saveme.xScale = 0.92
label_saveme.x = button_buysaveme.x + 22
label_saveme.y = button_buysaveme.y + 6
label_saveme:setFillColor(255/255, 255/255, 255/255)
label_saveme.alpha = 0.4
uiGroup:insert(label_saveme)
Thanks
So you want to put savecount value on label_saveme?
if your local label_save me on the same file as your button click code, change the "didComplete"
if event.value == "didComplete" then
print( "didComplete" )
-- Reward user here
print ("savecount")
savecount = savecount + 1
label_saveme.text = savecount
Lib.setSaveValue("savecount", savecount, true);
local alert = native.showAlert("Successful","Count saved successfully!", {"OK"})
end

Corona interstitial tap count

i have a question with corona (LUA), at the moment im showing interstitial ad when tap button occurs. I have this and its working (sometimes takes like 5-10-15 seconds to load and ad, i dont know why:
local ads = require("ads")
local interstitialAppID = "ca-app-pub-xxxxxxxxxx/xxxxxxxx21"
local testMode = true
local adProvider = "admob"
local function adListener( event )
if ( event.isError ) then
print ( "Error en el anuncio!", msg )
elseif ( event.phase == "loaded") then
print ( "Anuncio cargado!", msg )
elseif ( event.phase == "shown") then
print ( "Cargando nuevo anuncio!", msg )
ads.load ("interstitial", { appId = interstitialAppID, testMode = isTestMode } )
end
end
ads.init("admob", interstitialAppID, adListener )
ads.load ("interstitial", { appId = interstitialAppID, testMode = isTestMode } )
-- INTERSTITIAL AD
local function Adinterstatial( self, event )
ads.show( "interstitial", { appId = interstitialAppID, testMode = isTestMode
} )
end
local test = display.newImageRect( "Lore/0.png", 50, 50 )
test.x = 150
test.y = 150
test.tap = Adinterstatial
test:addEventListener( "tap" )
I want to do this for example: every 20 taps (on all the app) shows an interistitial ad.
Is this possible?
How i can do it?
Thanks.
You can add a Runtime tap event. This will get all taps on the screen, it does not matter where or what object was tapped. For example:
local numTaps = 0
local function countTaps()
numTaps = numTaps + 1
if numTaps % 20 == 0 then
-- Show the add here.
end
end
Runtime:addEventListener("tap", countTaps)
The % gets the remaindeer of the division between numTaps and 20. This means that it will be 0 every 20 taps.

Images doesn´t appear in storyboard Corona SDK

I am doing a game in corona SDK, but I have this little problem.
I have a menu with a button. If I press it, it sends me to the first level of my game.
When I pass the final level, the game return me to the menu. Bur, if I start playing the first again, my images doesn´t appear.
The images are balls, and to pass the level, you have to eliminate all the balls. To do this, I use:
ball:removeSlef()
ball = nil
But, I don´t think that this is the problem, because I eliminate this lines, and it doesn´t work.
The images are create in scene:createScene function, and insert in the Group.
I short the code of the first level to be understood.
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local physics = require "physics"
physics.start(); physics.pause()
physics.setGravity( 0, 0 )
local cont = 0
local bur = {}
function eliminar1( event )
if (cont == 0) and (event.phase == "began") then
event.target:removeSelf()
bur[1] = nil
cont = cont + 1
end
end
function eliminar2( event )
if (cont == 1) and (event.phase == "began") then
bur[2]:removeSelf()
bur[2] = nil
cont = cont + 1
end
end
function eliminar3( event )
if (cont == 2) and (event.phase == "began") then
bur[3]:removeSelf()
bur[3] = nil
storyboard.gotoScene( "levels.1.level2" )
end
end
function scene:createScene ( event )
local screenGroup = self.view
for i = 1,3 do
bur[i] = display.newImage("irudiak/"..i..".png")
bur[i]:translate(math.random(0,280), math.random(0,400) )
physics.addBody( bur[i], {bounce = 0.3 } )
bur[i]:setLinearVelocity(math.random(-50,50), math.random(-50,50) )
screenGroup:insert(bur[i])
end
bur[1]:addEventListener("touch", eliminar1)
bur[2]:addEventListener("touch", eliminar2)
bur[3]:addEventListener("touch", eliminar3)
end
function scene:enterScene( event )
local screenGroup = self.view
physics.start()
end
function scene:exitScene( event )
local screenGroup = self.view
physics.stop()
end
function scene:destroyScene( event )
local screenGroup = self.view
package.loaded[physics] = nil
physics = nil
end
return scene
createScene is ran only first time when you gotoScene. Every next time only willEnterScene and enterScene are played. To play createScene again you have to remove it (storyboard.removeScene() I guess). Or you can move some stuff you need to willEnterScene. For more detailed info you can watch this: http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

trigger to hide and show table

I am trying to trigger a hide and a show on my table local starTable = {}
local starTable = {} -- Set up star table
local function showStarTable()
-- some trigger to show the star table
end
timer.performWithDelay( 500, showStarTable, 1 )
local function hideStarTable()
-- some trigger to hide the star table
end
timer.performWithDelay( 1000, hideStarTable, 1 )
Is it possible to achieve this
To go along with the first answer here is an example:
local starTable = {}
local star1 = <display object>
starTable:insert(star1)
local function showStarTable()
starTable.alpha = 1
end
timer.performWithDelay( 500, showStarTable, 1 )
local function hideStarTable()
starTable.alpha = 0
end
timer.performWithDelay( 1000, hideStarTable, 1 )
Or of you wanted to stick with an actual table. and without seeing what actually is inserted into your starTable you could try:
local starTable = {}
local star = <display object>
starTable[1] = star
star = <display object>
starTable[2] = star
star = <display object>
starTable[3] = star
local function showStarTable()
for i=1, #starTable do
starTable[i].star.alpha = 1
end
end
timer.performWithDelay( 500, showStarTable, 1 )
local function hideStarTable()
for i=1, #starTable do
starTable[i].star.alpha = 0
end
end
timer.performWithDelay( 1000, hideStarTable, 1 )
However, the first option is better, if it works with your program.
Your code will execute the function showStarTable() after 1/2 second for 1 time. Then in another 1/2 second, it will execute hideStarTable() once.
Display objects like display.newImageRect() are tables, so if that's the table you are referring to, you can show/hide them by changing either the .alpha property of the object or it's visibility (.isVisible = true or .isVisible = false). However tables by themselves are just containers of information and a generic table isn't displayable. It could contain a single display object ore multiple.
It would be your responsibility in your show/hide functions to show/hide the contents of the table, if the table has displayable content.

Resources