How do I play a random sound when near an Enemy

0 favourites
  • 6 posts
From the Asset Store
In this template the music plays without looping in your game
  • Hi.

    I would like to "sometimes" play a random enemy sound when I am near an enemy in this 2d platform game. I am the 'playerbox', and the enemy is the 'Pvagt'.

    With sometimes I mean like 50% of the times I am near the enemy, or else it would be too annoying.

    I´ve come to this event setup, but the problem is when I play the empty sound the character´s mouth is moving (the 'Talking' animation).

    So what is a good approach to this behavior?

    Thanks alot!

  • Hello farsen,

    first of all i suggest to use the distance expression for the distance control: distance(player.x player.y, enemy.x, enemy.y) < X

    For your sounds you can use another random number before choosing a random sound. Give your enemy an instance variable.

    Using an instance variable ensures that each enemy acts individually.

    Then instead of choosing a sound, set the instance variable to choose(0, 1).

    Create a sub-event:

    If instance variable = 0 (or 1) -> play sound choose(a, b, c...)

    Edit: To prevent the enemy from yelping again and again, you can give him another instance variable, add this to the condition and change it in an action.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hi kriand

    Thanks alot for your answer!

    I´ve tried to implement your idea which seems to work great! (I´ve sticked with the line of sight though).

    But I seem to have a problem I dont quite understand. See it works just fine if the character should just keep on talking when I´m near it. But there should be some idling time if the choose hits the number 2. So therefore I create an event saying if the talkStatus is 2, I wait a little, set talkStatus to zero and then the main event can trigger again. But this messes up the talking animation which now plays randomly it seems, and I cannot understand why.

    I´ve also tried to play an empty 2 seconds file when the talkstatus is 2, but no luck here either.

    Can you see what I might be doing wrong? Or point me in another direction?

    Thanks

  • Maybe the "talking" animation continues to play from the previous time when talkingStatus was 1? You never reset the animation. Shouldn't you add something like "Set animation to Idle" to event #6 or #7?

    Also note, that if you have multiple pvtagt instances, event #7 will reset talkingStatus for all of them.

    And the "enemyTalking" variable is not needed, you can use Audio -> Is playing "pvagt"

  • Hi dop2000

    Maybe the "talking" animation continues to play from the previous time when talkingStatus was 1? You never reset the animation. Shouldn't you add something like "Set animation to Idle" to event #6 or #7

    - Well I set the talkingStatus to 0, so the event #4 takes care of setting the correct animation. And as said, if I dont care about the "random silence" and only use 0 and 1 in taskStatus it works just fine.

    [quote:1xyq736q]Also note, that if you have multiple pvtagt instances, event #7 will reset talkingStatus for all of them.

    • Good point thanks. (Not that it influcences this specific problem though.

    [quote:1xyq736q]And the "enemyTalking" variable is not needed, you can use Audio -> Is playing "pvagt"

    • I have multiple enemies talking and I dont want them to talk at the same time, so thats why I have this variable

    The problem seems to be the LOS which fires an enormous amount of times and then triggers the audio before the enemyTalking and talkinStatus variables are set.

  • Hi again.

    I solved it but putting a system - every 1 seconds on the event. So now it is only firing once.

    Thanks for your help!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)