selector.test.index

getSelector(id).test.index( element ) (since version 1.0)

or getSelector(id).test.index( element , index ) (since version 1.0)

Tests the index of an element in the selector (starting with 0). If you pass no index, the test will be a success if the element is part of the selector, else a failure.

When testing an element, remember to use a get* command, as simply typing its ID will fail (ie. test getImage("square"), not just "square")

This is particularly useful when you want to test the position of an element after a shuffle.

Example:

[js highlight=”12-15″ try=”true”]
newText(“feedback”)
,
newCanvas(“shapes”, 825, 440)
.settings.add( 0, 0, newImage(“square” , “square.png” ) )
.settings.add( 425, 0, newImage(“triangle”, “triangle.png”) )
.print()
,
newSelector(“choice”)
.settings.add( getImage(“square”) , getImage(“triangle”) )
.shuffle()
,
getSelector(“choice”)
.test.index( getImage(“square”) , 0 )
.success( getText(“feedback”).settings.text(“Square is on the left!”) )
.failure( getText(“feedback”).settings.text(“Square is on the right!”) )
,
getCanvas(“shapes”)
.settings.add( “center at 50%” , “center at 420” , getText(“feedback”) )
[/js]

First draws a square Image element to the left of a triangle Image element onto a Canvas element, then groups the Image elements into a Selector element and uses this to shuffle their positions. Will draw a Text element saying Square is on the left! if the square Image element ended up first in the Selector element after shuffling (index = 0), or Square is on the right! otherwise (i.e., if it ended up second, index = 1).