Play sound based on list version 3.0

drago76

  • Posts: 7
I have two lists, enemy animations and sounds (based on the animation).  Whenever I try to play a sound based on the list, I get:

TypeError: Error #1034: Type Coercion failed: cannot convert "Sound Name" to com.stencyl.models.Sound.

My lists look something like this:

0 - Bad Guy Annimation 1
1 - Bad Guy Annimation 2
2 - Bad Guy Annimation 3

0 - Angry Bad Guy
1 - Really Angry Bad Guy
2 - Super Angry Bad Guy

Can I reference a sound from a list?  Any help/insight appreciated.

SanjayPatel

  • Posts: 18
So what you're trying to do is store the names of the sounds you want to trigger in a list as text?  That's a cool idea, but I don't think it will work directly because the block that triggers a sound doesn't accept text as it's input like, say, the switch animation blocks. 

I think its possible, however, to create a list that references the actual sound objects, and not just the text name of the sound.  The only way i could figure out how to do this, however, was to make an attribute of type "Sound" for each of the sounds you want included in the list.  Then set the default value of the these sound attributes to each of the sounds you've imported into stencyl.  Then you can add those sounds to the list and reference them directly.

Either way, it seems kind of clunky.  Its probably easier just to reference the sounds directly.

drago76

  • Posts: 7
I see where you are going with that, that's helpful.  I'm still tinkering with it but your idea gives me something to fall back on.

Thanks.

sdieters

  • Posts: 2068
It is possible to call a sound by its name, but it requires a little bit of custom coding.
When in home I will show you =)
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

drago76

  • Posts: 7
That would be awesome.  I've been playing with a custom block but can't seem to get things right....probably because it's beyond my skills right now.

sdieters

  • Posts: 2068
okay so it was a bigger pain in the ass as i thought, BUT... (oh the irony)

i managed to do some research in the costum code part and i came up with the following theory.
since you cant make a list with sounds the "normal" way, we should create a list by custom code. we need to tell stencyl when it is created, to create a new list (an array) and fill it with all the objects that are sounds ( i believe it should be something like : getResourcesOfType(SoundClip) ). when we have our list, we should be able to tell stencyl, to get the soundclip that is stored on index #0, or any index you like, and play it.

sadly enough i havent done enough programming for stencyl, and thus i cant program it out for you. i hope another more programming experianced user can help you out further.
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

drago76

  • Posts: 7
I don't make anything easy.  I am definitely a learner and followed what you were saying but couldn't figure it out coding-wise in Stencyl.  So to get by, I thought I would just change directions for now and identify each sound by ID (manually),  Did that, put in the list but something is not right. 

I've tried things like the attachment and the compile will never complete.

sdieters

  • Posts: 2068
if i was you i should be using the CODE block if you dont know programming, since you need to know the language ;)

the best thing you can do is making an attribute for every sound you have, and assign the soundclips in the inspector ( the behaviour tab). hen you need to make something like this:

*whenever your enemy needs to play the angry sound*
set (randomNumber) to pick any number between 0 and 2,
IF (randomNumber) = 0,
~play (angryBadGuy)
OTHERWISE IS (randomNumber) = 1,
~play (reallyAngryBadGuy)
OTHERWISE IF (randomNumber) = 2,
~play (superAngryBadGuy)

a little tip about sounds. of you have allot of sounds to play, you should work with channels. i always have 3 channels.
channel 0 = background music
channel 1 = player sounds
channel 2 = enemy sounds

this way you prevent cut-off from other sounds when you play a new one.
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

drago76

  • Posts: 7
After some trial and error (and previewing the generated code), I removed the "Play Sound" and changed the code to "playSound(getSound(Math.floor(_SoundID)));"  So the sounds play for each animation based on the sound (resource) ID from a list which I got from data.xml under the game resources.

If anyone has any idea how I could code getting the resource ID for a sound based on the name, that would make things work as I wanted in the beginning.

Thanks for the tip on the sound channels.