Timing of display of a bitmap image

I’m trying to make a bitmap image appear at a specific time during a program.

I’d like to play a beep, present my image 1 second later, and play a final beep after another 2 seconds.

Currently, the code plays the first beep, then presents the image and the final beep at the same time.

(Note: I’m using a loop as I’ll want to check for keyboard state changes between the two beeps - this is the beginning of a program for a psychology experiment).

Any help greatly appreciated!

package;

import openfl.display.Bitmap;
import openfl.display.Sprite;
import openfl.media.Sound;
import openfl.Assets;
import openfl.Lib.getTimer;

class Main extends Sprite
{
public function new()
{
super();

  var beep:Sound = Assets.getSound("audio/beep.wav");
  beep.play();
  
  var startTime = Date.now().getTime();
  var currentTime = Date.now().getTime();
  var stimFlag = 0;
  while ((currentTime - startTime) < 3000)
  {
  	if ((currentTime - startTime) > 1000 && stimFlag == 0)
  	{
  		presentStim();
  		stimFlag = 1;
  	}
  	currentTime = Date.now().getTime();
  }
  
  beep.play();

}

function presentStim()
{
//present a stimulus
var bmp1 = openfl.Assets.getBitmapData(“img/1.png”);
var stim1 = new Bitmap (bmp1);
addChild (stim1);
}
}

Please don’t busy-wait. It wasn’t necessary in whatever language you started out in, and it’s even less necessary in Haxe.

Instead, how about using Timer?

private var beep:Sound;

public function new() {
    beep = Assets.getSound("audio/beep.wav");
    beep.play();
    
    //queue the next step
    Timer.delay(presentStim, 1000);
}

private function presentStim():Void {
    //present a stimulus
    var bmp1 = openfl.Assets.getBitmapData("img/1.png");
    var stim1 = new Bitmap (bmp1);
    addChild (stim1);
    
    //queue another beep
    Timer.delay(playBeep, 1000);
}

private function playBeep():Void {
    beep.play();
}

Thanks for your response - however, the second beep doesn’t play - I think you’ve missed passing the beep variable to the presentStim function.

On this note, is there a way to make the variable global, so all functions have access to it without it having to be specifically passed to them? That way I could use a global timer event to get all the relevant reaction times I need from my participants.

There might be an error with Timer.delay(beep.play, 1000). I’ve updated my code above; try that version instead.

You can already access it from anywhere in the Main class. Just make sure your new() function does not contain the word “var”.