Question about Sound Effects SOLVED

Calicara

  • Posts: 10
I am designing a platformer game with farm animals. When my player walks by each of the animals, I want the animals to emit a sound effect. I have tried making a code, and reading through stencylpedia, but for some reason my code does not work, and I was wondering if someone would help me figure out why.

I am new to Stencyl, and coding in general, but I am trying my best to learn!

Here is my current code:

« Last Edit: March 14, 2019, 03:29:49 pm by Calicara »

Luyren

  • *
  • Posts: 1665
On your green block, were it says "Actor", you have to select an actor. I'm assuming that should be your player actor, so you have to find a way to get the player actor, and that depends on your setup.
-If your player is not placed on the scene: I think the most common way is to set your player to an exclusive actor group, then loop through all actors of that group in your behavior and set an attribute of type "actor" to the value of the actor found on the loop.
-If your player actor is placed directly on the scene: Create a non-hidden attribute of type "actor", then set that attribute to your player actor, by double clicking the animal after it's placed on the scene.

One more thing: your code would play the noise 100 times per second while your actor is within range. So you should have a boolean to do it only once:

Code: [Select]
If <not (Boolean)> and <distance check>
-Set Boolean to true
-Play sound
Otherwise
-Set Boolean to false
Something like this should work.
Working.

Calicara

  • Posts: 10
Thanks for the response! I think I understand the first part about the actor. I'm having trouble with the code portion.

If <not (Boolean)> and <distance check>
-Set Boolean to true
-Play sound
Otherwise
-Set Boolean to false

I think I can sort of understand what you're saying, I just can't figure out how to construct it using stencyl blocks. I am trying now, still having a hard time.

corbanwolf

  • Posts: 160
If distance is smaller than 20
   If "nearby" is false
      set "nearby" to true
      play sound
Otherwise
   set "nearby" to false

nearby is boolean attribute
You already constructed code with blocks. Just add attribute to your code

« Last Edit: March 14, 2019, 10:44:35 am by corbanwolf »

Calicara

  • Posts: 10
I'm sorry I'm still not sure I get it :(

corbanwolf

  • Posts: 160
Your code checks if player is 20px away from some actor 100times per second. If the player is exactly 20px away, the sound will be played 100 times per second. We want the sound to be played only once when the player approaches the animal. To do that we need a boolean attribute that will store information whether the player is close to animal and make the sound play only once.
Use IF block.. First condition will check if player is close enough to sound to be played. IF DISTANCE BETWEEN PLAYER AND ACTOR IS LESS THAN 20. Now we need another IF. It will be second condition that we need to pass to the sound be played.  Let's create an ATTRIBUTE of name "nearby".  And condition IF "nearby" IS FALSE. Now let's use PLAY SOUND block. If we leave it like. The sound will still be played 100 times per second. And we need only one when player is near. To do that, we need to change ATTRIBUTE value from FALSE to TRUE. When it's TRUE our IF statement will not let us play that sound again. Let's use SET "nearby" TO TRUE. Now when we approach an animal the sound will be player once, only once. When we go far from animal and get near again the sound will not play because an ATTRIBUTE "nearby" is still TRUE which prevents IF block to let us play the sound. We need the animal to be ready to play sound again when we get far. We need OTHERWISE block that will turn "nearby" ATTRIBUTE TO FALSE. As name suggests "nearby" ATTRIBUTE is TRUE when player is nearby and FALSE when player is far. So let's put OTHERWISE block to the statement IF DISTANCE BETWEEN and let's put SET "nearby" TO FALSE. This way when we get away from out little sheep we will change the ATTRIBUTE value to FALSE which will let us play sound again when we get close to animal.

Calicara

  • Posts: 10
This is what I have now.

It's still not working though. I am not sure I made the nearby attribute correctly. I have it so the value it's looking for is number.

Then I made the custom block "set", which does nothing, but allows me to place the variable in it. I appreciate you spelling it out for me. I think I am almost there :)

corbanwolf

  • Posts: 160
Attach OTHERWISE to the other IF
Don't make another CUSTOM BLOCK
On your right you have list of available blocks. Go to ATTRIBUTES and find SETTERS.
"nearby" should be boolean type.

Calicara

  • Posts: 10
I am almost there! Thank you so much for the help! Here is where I'm at now.

I am still getting an error when testing the game though, it says:

Noise

Missing return: Float
from scripts.Design_44_44_Noise
line: 72
columns: 2-27

I have tried looking at that line in the code, it looks like it has something to do with the actors in my custom distance block

corbanwolf

  • Posts: 160
Looks like it. Can you post your code for that custom block?

Calicara

  • Posts: 10

corbanwolf

  • Posts: 160
1. The custom block is empty. It will not give you the right return value.
2. In custom blocks those types are actor group types, and you attach actor types.

Please run crash course ;d

« Last Edit: March 14, 2019, 02:23:20 pm by corbanwolf »

Calicara

  • Posts: 10
Hello! I haven't got it completely working yet, but I think I will soon. I've got it to run without errors at least.

This was the initial article I was using: http://www.stencyl.com/help/view/creating-custom-blocks/

Sorry it was unclear to me you had to put additional content in the wrapper.

I just found this article: http://community.stencyl.com/index.php?topic=4608.0

Which I am finding much more helpful. So between you, and that article, I should have it working soon. Thank you for being patient with me. I have not had too much trouble with any other program. I try to use stencylpedia when I can.

Calicara

  • Posts: 10
It finally works!!! Thank you!!! :D