Corona sdk random text - text

I`m have a problem because when i touch my button sometimes not appear my text... (Sorry for my English!)
Code:
function randomText(event)
display.remove(mmDis)
local a = {"Cristiano ronaldo jest najlepszy!","messi jest dobry!","lewandowski jest ok","diego lopez to bramkarz realu"}
com = (a[math.random(1,#a)])
local mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Impact", 30)
mmDis.y=20
mmDis.x=190
mmDis:setFillColor(0, 0, 0, 1)
mmDis.anchorY = 0
end
play:addEventListener ("tap", randomText )
end
end
play:addEventListener( "touch", object )

Make sure you are localizing your variables properly. You need to declare "mmDis" outside your function, and then do not localize it inside the function. Something like this
local mmDis* -- this will ensure code from here on out all use the SAME "mmDis"
function randomText(event)
display.remove(mmDis)
local a = {"Cristiano ronaldo jest najlepszy!","messi jest dobry!","lewandowski jest ok","diego lopez to bramkarz realu"}
com = (a[math.random(1,#a)])
-- NOTICE how I removed the local keyword below.
-- This will ensure your code is talking about the same "mmDis"
-- you declared before your function
mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Impact", 30)
mmDis.y=20
mmDis.x=190
mmDis:setFillColor(0, 0, 0, 1)
mmDis.anchorY = 0
end
play:addEventListener ("tap", randomText )
end
end
play:addEventListener( "touch", object )
Ultimately, the problem you were having is that you were not removing the old text off the screen because mmDis was localized inside the randomText function. Make sure you localize your variables with proper scope. Here's a good read about it http://lua-users.org/wiki/ScopeTutorial

local mmDis
function randomText(event)
if event.phase == "began" then
if mmDis then
display.remove(mmDis)
end
local a = {"Cristiano ronaldo jest najlepszy!","messi jest dobry!","lewandowski jest ok","diego lopez to bramkarz realu"}
local com = a[math.random(1,#a)]
mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Impact", 30)
mmDis.y=20
mmDis.x=190
mmDis:setFillColor(0, 0, 0, 1)
mmDis.anchorY = 0
end
end
play:addEventListener ("tap", randomText )
end
end
play:addEventListener( "touch", object )
Try this and see.

Related

AwesomeWM client created/removed callback

I am using awesome WM and I want to run a lua function after a client is created/deleted. Specifically, I want to change the name of a tag to the name of one of the clients that are on the tag.
I do this with a timer, but I think the best way to do this would be to register a callback function to awesomeWM that it will invoke when a client is created/removed.
Are there some hooks/callbacks that I can implement to tell awesome to do this for me?
---------------------------------------------UPDATE----------------------------------------
I tried using the signals, but i cant find the correct signal that changes calls my function AFTER the window is created and attached to the tag. I tried this with manage/unmanage tagged/untagged, and tag.new, etc, but no one helps.
Any ideas?
here is the code:
override_name_char = "<"
function tag_name_from_client(c)
if string.match(c.name, "Mozilla Firefox") then
return "Firefox"
end
if string.match(c.name, "Sublime Text") then
return "Sublime"
end
if string.match(c.name, "/bin/bash") then
return "Shell"
end
return ""
end
function tag_name_from_tag(tag)
if tag.name:match(override_name_char) then
return tag.name
end
for _, c in pairs(tag:clients()) do
return " "..tostring(awful.tag.getidx(tag)).." "..tag_name_from_client(c)
end
return tostring(awful.tag.getidx(tag))
end
function refresh_tag_name()
for s = 1, screen.count() do
for _,tag in pairs(awful.tag.gettags(s)) do
tag.name = tag_name_from_tag(tag)
end
end
end
client.connect_signal("tagged", refresh_tag_name)
client.connect_signal("untagged", refresh_tag_name)
--tag_timer = timer({timeout = 0.5})
--tag_timer:connect_signal("timeout", function()
--refresh_tag_name()
--end)
--tag_timer:start()
Thanks in advance for any help regarding this.
One of possible ways for v3.5.6, try this in your rc.lua
local naughty = require("naughty")
client.connect_signal("manage", function (c)
--filter client by class name
if c.class:lower() == "gedit" then
-- do things on client start
naughty.notify({text = "Gedit launched!"})
-- add exit signal for this client
c:connect_signal("unmanage", function() naughty.notify({text = "Gedit closed!"}) end)
end
end)
"A new client is created" is the manage signal.
"A new client was destroyed" is the unmanage signal.
So, something like the following (untested):
local function choose_name_for_tag(t)
for _, c in ipairs(t:clients() do
return "has client: " .. tostring(c.name or "unknown")
end
return "has no clients"
end
local function update_state()
for _, t in pairs(root.tags()) do
t.name = choose_name_for_tag(t)
end
end
client.connect_signal("manage", update_state)
client.connect_signal("unmanage", update_state)
tag.connect_signal("tagged", function(t)
t.name = choose_name_for_tag(t)
end)
tag.connect_signal("untagged", function(t)
t.name = choose_name_for_tag(t)
end)

Can I use an ended sound file to transition to a new scene or refresh data in the same scene?

Lua novice asks:
How do I transition from this...
(a simple example to establish a how-to)...
visual = display.newImage( "redCircle.png", 50, 50 )
local music = audio.loadStream("sound1.mp3")
audio.play(music)
audio.stopWithDelay(60000/60)
to this, timed by the first sound file ending?
visual = display.newImage( "blueCircle.png", 50, 50 )
local music = audio.loadStream("sound2.mp3")
audio.play(music)
audio.stopWithDelay(60000/60)
Which api should I be experimenting with? I've looked at https://docs.coronalabs.com/api/index.html
What am I missing?
What you can do is create a function listener for the first audio file you can look more here: https://docs.coronalabs.com/api/library/audio/play.html Below is a sample code I can give you. Note that I did not use audio.stopWithDelay
--DECLARE LOCAL VARIABLES
local visual1
local visual2
local music1
local music2
--LOAD SOUNDS
music1 = audio.loadStream("sound1.mp3")
music2 = audio.loadStream("sound2.mp3")
local function soundIsFinished(event)
if (event.completed) then
--PLAY SECOND AUDIO AND HIDE/REMOVE VISUAL 1
visual1.isVisible = false
visual2.isVisible = true
audio.play(music2, {duration = 1000})
end
end
--DISPLAY VISUAL1 and play MUSIC1
visual1 = display.newImage("redCircle.png", 50,50)
--AUDIO WILL PLAY FOR 1 SECOND (60000/60) is 1 second
audio.play(music1, { duration=1000, onComplete=soundIsFinshed } )
-- HIDE VISUAL2 FIRST
visual2 = display.newImage("blueCircle.png", 50,50)
visual2.isVisible = false
Hope This helps.

Random text view

I'm having a problem because when I touch my button sometimes my text doesn't appear...
function randomText(event)
display.remove(mmDis)
local a = { "Cristiano ronaldo jest najlepszy!",
"messi jest dobry!","lewandowski jest ok", "diego lopez to bramkarz realu"
}
com = (a[math.random(1, #a)])
local mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Impact", 30)
mmDis.y=20
mmDis.x=190
mmDis:setFillColor(0, 0, 0, 1)
mmDis.anchorY = 0
end
play:addEventListener ("tap", randomText )

Corona + objects are not cleared between levels despite removeSelf + set to nil

I have multilevel game and all my objects stay 'empty' in memory between levels
what i mean is that if I set physics to hybrid, when i come again in the level, (or another level) it shows a box but without the picture. (and physics act on these empty boxes)
in the destroyScene I have made sure they are all
myObj:removeSelf()
myObj = nil
and i print a message to prove that it actually does it.
Also in the menu in the enterScene, just in case I do a
local prior_scene = storyboard.getprevious()
storyboard.purgescene( prior_scene )
and also tried a
storyboard.removeAll()
and even a
storyboard.purgeOnSceneChange = true
nothing works when i go to the next level, or into the same level again, all my previous objects are still here, I just don't get it
ok, it's gonna be a bit long but here the entire level. it does go through the destroyscene but somehow the display objects are not removed.
-- scene5
----------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene5 = storyboard.newScene()
function scene5:createScene( event )
local group = self.view
puppetJColCount = 0
print ("createScene5", puppetJColCount)
-----------------------------------------------------------------------------
-- CREATE display objects and add them to 'group' here.
-- Example use-case: Restore 'group' from previously saved state.
local physics = require("physics")
physics.start()
--physics.setScale(50)
puppetT_outside = false
puppetJ_outside = false
end -- scene -------------------------
-- set and release the catapult for puppetT
function arm_puppetT(event)
if event.phase == "began" then
display.getCurrentStage():setFocus(puppetT)
arming_puppetT = true
elseif event.phase == "moved" then
puppetT.x = event.x
puppetT.y = event.y
elseif event.phase == "ended" then
puppetT:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, puppetT.x, puppetT.y)
display.getCurrentStage():setFocus(nil)
arming_puppetT = false
end
end -- arm_puppetT ----------------------------------------------------------------------------
function build_puppetT()
-- setup puppetT
puppetT = display.newImage("assets/t-head-57-78.png")
puppetT.x = 80
puppetT.y = 100
physics.addBody(puppetT,"dynamic",{density=1.0, friction =0.3, bounce=0.2})
-- setup catapult event for arming puppetT
puppetT:addEventListener("touch", arm_puppetT)
end -- build_puppetT
function build_puppetJ_wall()
puppetJColCount = puppetJColCount + 1 -- how many columns of puppetJ
puppetJIJ = {} -- define puppetJ as an array
ij = 0
ipuppetJtot = 0
--puppetJColCount = 1
print ("build_puppetJ_wall puppetJColCount>" , puppetJColCount);
for i=1, 4 do
for j=1, puppetJColCount do
ij = ij + 1 -- # of puppetJs on the screen
ipuppetJtot = ipuppetJtot + 1
puppetJIJ = display.newImageRect("assets/j-head-70-75.png",80,75)
puppetJIJ.x = 600 + j*22
puppetJIJ.y = 100 + (puppetJIJ.height /2 * (i -1))
physics.addBody(puppetJIJ,"dynamic",{density=1.0,friction=0.3,bounce=0.2,isSensor=false,radius = var})
end
end
print ("building puppetJs #:" ,ipuppetJtot)
end -- build_puppetJ_wall -------------------------------------------------------------
function every_frame( event )
end -- every_frame --------------------------------------------------------------------
--reset level
function tap_reset_level(event)
puppetT:applyLinearImpulse(0, 0, 0, 0) -- stop the kick
print "restarting physics?"
puppetT:setLinearVelocity( 0, 0 ) -- stop the speed
puppetT.x = 80
puppetT.y = 100
for ij = 1,ipuppetJtot do
if (puppetJIJ) then
puppetJIJ:removeSelf()
puppetJIJ = nil ------
end
end
puppetT:removeSelf()
puppetT = nil
build_puppetJ_wall()
build_puppetT()
puppetT_outside = false
puppetJ_outside = false
-- physics.addBody(puppetT,"dynamic",{density=1.0, friction =0.3, bounce=0.2})
-- puppetT:addEventListener("touch", arm_puppetT)
--physics.start()
end -- tap_reset_level -------------------------------------------------------------------------
function tap_main_menu(event)
print ("going to main menu")
Runtime:removeEventListener("enterFrame", every_frame)
for ij = 1,ipuppetJtot do
if (puppetJIJ) then
puppetJIJ:removeSelf()
puppetJIJ = nil ------
end
end
if (puppetT) then
puppetT:removeSelf()
puppetT = nil
end
-- scene5:exitScene(scene5)
storyboard.gotoScene( "menu" )
end -- tap_main_menu ---------------------------------------------------------------------------------
-- ======================================================================================
-- Called immediately after scene has moved onscreen:
function scene5:enterScene( event )
local group = self.view
-----------------------------------------------------------------------------
-- INSERT code here (e.g. start timers, load audio, start listeners, etc.)
-- group:insert( reset_btn )
-- load background
print ("enterScene scene5 " , puppetJtot)
background = display.newImage("assets/parliament-1200-800.png",0,0)
--create reset level button
reset_btn = display.newImageRect( "assets/btn_reset_128.png", 50,50)
reset_btn.x = 50
reset_btn.y = 50
--create main menu button
main_menu_btn = display.newImageRect( "assets/btn_home_128.png", 50,50)
main_menu_btn.x = 100
main_menu_btn.y = 50
-- show the level
local myText = display.newText( "Level 5", display.contentWidth - 60, 50, "Helvetica", 24 )
myText:setTextColor(255, 255, 255)
-- insert(floor);
floor = display.newRect(20,display.contentHeight - 40,display.contentWidth - 40 ,10)
physics.addBody(floor,"static",{density=1.0, friction =-0.3,bounce=-0.2,isSensor=false})
-- build puppetT
build_puppetT()
print ("width=" , display.contentWidth , "height=",display.contentHeight)
-- setup puppetJs
build_puppetJ_wall()
-- everything happens in everyframe function
Runtime:addEventListener("enterFrame", every_frame)
--add reset event
reset_btn:addEventListener( "tap", tap_reset_level )
--add mainmenu event
main_menu_btn:addEventListener( "tap", tap_main_menu )
end -- scene:enterScene ----------------------------------------------------------------
-- ======================================================================================
-- Called when scene is about to move offscreen:
function scene5:exitScene( event )
local group = self.view
print "scene:exitScene5"
-----------------------------------------------------------------------------
-- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
end -- exitScene
-- Called prior to the removal of scene's "view" (display group)
function scene5:destroyScene( event )
local group = self.view
Runtime:removeEventListener("enterFrame", every_frame)
puppetJColCount = 0
if (reset_btn) then
print ("destroyScene5 - removing tap_reset_level")
reset_btn:removeEventListener( "tap", tap_reset_level )
reset_btn:removeSelf()
end
reset_btn = nil
if (main_menu_btn) then
main_menu_btn:removeEventListener( "tap", tap_main_menu )
main_menu_btn:removeSelf()
end
main_menu_btn = nil
for ij = 1,ipuppetJtot do
if (puppetJIJ) then
puppetJIJ:removeSelf()
end
puppetJIJ = nil ------
end
if (puppetT) then
puppetT:removeSelf()
end
puppetT = nil
scene5 = nil
end -- destroyScene ------------------------------------------------------------
scene5:addEventListener( "createScene", scene5 )
scene5:addEventListener( "enterScene", scene5 )
scene5:addEventListener( "exitScene", scene5 )
scene5:addEventListener( "destroyScene", scene5 )
---------------------------------------------------------------------------------
return scene5
update:
Hi #DevfaR
Ha, I have restarted from scratch with the corona template and figured out a few things:
1) the exitScene/destroyScene actually does something if the local group is used to group display objects. it removes the display group !
not really obvious as there is only one declaration in it, no code as such
2) the reason why I had so much of removeSelf of objects all over the place is because nothing was working. So I tried pretty much everything under the web.
3) And the reason why it didnt work is because I was creating my display objects into a function. and somehow the display group is not passed there. if I move the code into the createScene function then indeed it is all cleared up when going to the next scene.
problem is .. I really would like to group my puppetCreation code !
e.g.
function scene:createScene( event )
local group = self.view
local physics = require("physics")
physics.start()
background = display.newImage("assets/bckg.png",0,0)
group:insert(background)
createPuppet1(group)
createPuppet2(group)
end
function createPuppet1(group)
puppet1= display.newImage("assets/puppet1.png",0,0)
group:insert(puppet1)
end
function createPuppet2(group)
puppet2= display.newImage("assets/puppet2.png",0,0)
group:insert(puppet2)
end
I passed (group) because the function createPuppet doesnt let me specify
local group = self.view
my email address is edualczatebed#gmail.com
thank you for your help
there's a lot of bug in your code the way you implement it is not good because when you go to a scene and then you switch to another scene your removing all of the object on the destroyScene together with it's listener which is not very efficient i cannot point out what's causing the problem because like i said there is a lot of bug in your code. i think you didn't quite understand how to use a storyboard on Corona. to give you an idea on how will you solve your problem i will implement some code for you to understand that even if you do not remove the object on destroyScene you can remove the object via it's on display groups.
local rectangle
local circle
local function nextScene(event)
storyboard.gotoScene( "scene_2", "fade", "500")
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
rectangle = display.newRect(100,100,100,100)
rectangle:setFillColor(255,0,0)
rectangle.x = 100
rectangle.y = 100
circle = display.newCircle(100,100,50)
circle.x = 250
circle.y = 100
circle:setFillColor(0,255,0)
--inserting all of your object into display group so that if you destroy the scene the display group will be remove
group:insert(rectangle)
group:insert(circle)
-----------------------------------------------------------------------------
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
-----------------------------------------------------------------------------
rectangle:addEventListener("tap", nextScene)
circle:addEventListener("tap", nextScene)
-----------------------------------------------------------------------------
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
local group = self.view
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
end
as you can see i create 2 object the rectangle and circle after i created the objects insert it to group then on enterScene i added a tap listener which will go to the second scene if i tap the object. notice that i didn't bother to remove the object and listener on destroyScene when i go to the scene_2 that is because when i insert the object to the display group whenever i change the scene it will always remove the object. so after moving to the next scene you can either purged the last scene or completely remove it so when you get back to that scene it will recreate the object.
hope this can help you solve your problem

How can i add attribute to an object in Lua?

I am trying to add an attirubute to objects that i created.Here i created bird display objects but i want to add those birds a spesific attiribute like typeOfBird and then i want to reach those attiributes like bird.typeOfBird . How can i do that?
module(...,package.seeall)
function new(params)
local bird=display.newImage(params.img,params.x,params.y)
function bird:touch(event)
local t = event.target
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
elseif t.isFocus then
if "moved" == phase then
t.x = event.x
t.y = event.y
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
It looks like bird objects are already simple lua tables,
so you can just get and set the values as normal. So for example you could add lines like:
if self.typeOfBird == "gull" then ... end
and
self.typeOfBird = "parrot"
to your bird:touch function.
Or
bird.typeOfBird = "gull"
in your new function.

Resources