3

I am fairly new to AS3 so any help would be appriciated.

Basically I am trying to make something similar to the Hazard Perception test, where you click and it records weather you clicked at the right time or not.

What I have so far is this:

import flash.events.Event;

videoOverlay.addEventListener(MouseEvent.CLICK,doClick)
function doClick(e:Event):void
{
    trace(myVideo.playheadTime)
}

I have managed to make a clickable area and then display click times, what I now need to do is to be able to tell if a click was in a certain time frame then add 1 point, and then at the end of the video clip I want to display a score.

I am not after just code, if anyone could maybe suggest a way of doing this it would be appriciated.

jjhilly
  • 31
  • 3

2 Answers2

2

You can store the 'right moments' in an array, xml, whatever. Let's say something like this:

var moments:Array = [{start: "1:01", end: "1:16"}, {start: "1:25", end: "1:26"}, {start: "1:39", end: "1:51"}];
//time is in minutes, so you need to convert it to seconds
function doClick(e:Event):void
{
    for (var i:int = 0; i < moments.lenght; i++)
    {
        var moment:Object = moments[i];
        if (myVideo.playheadTime => toSeconds(moment.start) && myVideo.playheadTime <= toSeconds(moment.end))
        {
            trace("that's the right moment");
            break; //we do not need to check further
        }
    }
}
strah
  • 6,702
  • 4
  • 33
  • 45
  • it would be perfect if there was no conversion done from string to number. in other words the start, end should store numeric values of seconds. – Lukasz 'Severiaan' Grela Mar 12 '13 at 13:11
  • @Lukasz'Severiaan'Grela That's the 'design' decision: do you want a computer friendly value (number, in seconds) or human friendly (in format such as '1:05'). – strah Mar 12 '13 at 13:14
  • human readable can only be e.g. in xml and one time converted to number, or it can be converted when user needs to view it - but not when you do comparison in a loop. – Lukasz 'Severiaan' Grela Mar 12 '13 at 15:44
  • Agreed, but a human has to enter these values manually, be it into an array or XML or whatever data collection they want. Once the data is there it can be converted into 'computer friendly' format. The conversion can be done at the very beginning or at the time the numbers are needed. The code above is just for illustration, it's not a final, optimized version. It was easier to show the idea that way. Pozdrawiam :-) – strah Mar 12 '13 at 15:56
0

The best way I can think of to do this is a timer. Setup a timer class and on the click event display rather or not the click is within a certain range of time - if so then success if not so then failure.

Try something like this:

counter:Number = 0;
videoOverlay.addEventListener(MouseEvent.CLICK,doClick)
public function doClick(e:Event):void
{
if (counter < 10)
trace("Success, your quick!");
else
  {
   trace("Failure!");
  }
}
addEventListener(Event.ENTER_FRAME, timed_event);

public function timed_event(event:Event) : void
{
counter++;
}

Of course, the time rate depends upon the frame rate of the application as well.