Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Need help: How to check a name on audio?
Started by d.mach Jun 29 2018 12:09 AM

4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

d.mach

[GLOBAL: userInfoPane.html]
d.mach
  • Contributor

  • 633 posts
  • Corona SDK

I wonder how I can use this kind approach without an error:

 

Example:

local sfx={one}
sfx.one = audio.loadSound( "sfx/mp3_music/mysound.mp3")
sfx.one.name="soundfx"

The last line with the .name is generating this error:

attempt to index field 'one' (a userdata value)

 

How can I add some info to my soundfiles, so I can later check in an if clause what file is getting called? I need some kind of ID for each sound (like the name) to check in my module what sound was called.

 

How can I do this?

 



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 325 posts
  • Corona SDK

May be something like that?

 

local sfx={ 
    one = { 
        sound = audio.loadSound( "coin.mp3"), 
        name  = 'soundfx' 
    } 
}


[TOPIC: post.html]
#3

d.mach

[GLOBAL: userInfoPane.html]
d.mach
  • Contributor

  • 633 posts
  • Corona SDK

Thank you very much for your feast feedback!

Is this then still working when calling audio.play (sfx.one) ?

Because I need a solution I don't have to change all the calls and just add a "fix" to the audio module wthout going to all the code!



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,458 posts
  • Corona SDK

Maybe a better way to do it would be:

 

 
local sfx={ 
   coinSound = audio.loadSound("coin.mp3"),
   gameOverMusic = audio.loadSound("gameOver.mp3")
}
 
local function playSound(name)
 
  if (sfx[name] ~= nil)
    audio.play(sfx[name])
    print ("Sound being played is: " .. name)
  end
 
end
 
 
playSound("coinSound")
playSound("gameOverMusic")
 
 


[TOPIC: post.html]
#5

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 325 posts
  • Corona SDK

Is this then still working when calling audio.play (sfx.one) ?

No. @nick_sherman way doing this is better :)




[topic_controls]
[/topic_controls]