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).
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;
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.