How do I check a value is changing ?

Not favoritedFavorited Favorited 0 favourites
  • 4 posts
From the Asset Store
Simplistic hyper-casual game tapping to match earth, fire, water, and air elements.
  • In my game, I made a custom slider with a sprite vertical bar and a sprite slider.

    SLider moves on the bar through constraint drag behavior with a step of 10 pixels, meaning the dragging is not fluid. I'd like to play a ticking noise when the slider moves step by step. How can I do that ?

    I thought of having a listener listening for the slider Y coordinate to change and then play the sound.

    I tried many different approaches but no success.

    How can I do that ?

  • Since it’s vertical you could give the slider called oldy and do an event like:

    System: pick by comparison: sprite: abs(sprite.y-sprite.oldy)>=10
    — play sound
    — sprite: set oldy to self.y

    Which means every time the difference between the old and current y position is greater than 10 (the step you’re using for snapping) it plays a sound.

    An alternate way to do the condition would be with an “or” with two y checks, however the abs() lets you handle both at the same time.

    Sprite: y>=self.oldy+10
    Or
    Sprite: y<=self.oldy-10
    — play sound
    — sprite: set oldy to self.y

    Also, you’ll want to set oldy to y at the start of the layout to avoid it making noise when the game starts.

    For a more deluxe version you could handle the case where the slider moved more than one step in a frame so you’d get to hear multiple tick sounds. For that you’d want to mess with the audio action to schedule when a sound will play to offset the sounds from each other. I’d think you’d want to space out the sounds playback time over the next frame which may look something like this:

    Variable count=0
    
    For each sprite
    — set count to int(abs(sprite.y-sprite.oldy)/sprite.step times)
    — sprite: oldy to self.y
    — repeat count times
    — audio: schedule play tick sound at self.currentTime+loopindex*dt/count

    You’d have to look at the manual to verify how to use the schedule playback action.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks rojohound !

    do you mean I should give the slider a variable called oldY ? (is the word "variable" missing in your first sentence ?)

  • Yeah I meant to write variable there

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