|
Creating picking with Lingo
To view the Movie script, open the Script window and go to Movie Script 11. You can note the comments in the script as well as review the comments in this article. What follows is the Lingo in the movie script relevant to picking.
The on checkForModels handler, which is called by the mouseUp script on the 3D sprite, does several things. First, it updates, in the spriteToWorld display field, the position vector of where the user clicked.
on checkForModels
tOffset = point(sprite(1).left, sprite(1).top)
spriteToWorld = sprite(1).camera.spriteSpaceToWorldSpace(the
mouseLoc - tOffset) member("spriteToWorld display").text =
"spriteSpaceToWorldSpace: " & spriteToWorld
Second, within the case statement, the code determines what the user clicked and stores the result in the variable gModelsClicked .
The gTestFor variable is set by the script on the Use This Function button, gNumberOfModels is set by the script on the Number of Models button, and gSimpleOrDetailed is set by the script on the Simple or Detailed button. The three types of checks for models that appear under the pointer or along a ray are included in the case statement. The modelUnderLoc function returns a reference to the first model that appears underneath the given 2D location within the sprite's bounding box. The modelsUnderLoc function returns a list of references to the models that appear under the given 2D loc , with the first being the model closest to the camera. The modelsUnderRay function returns a list of references to the models intersected by the ray defined by the given position and direction vectors, with the first being the first model intersected by the ray.
case gTestFor of
"modelUnderLoc":
gModelsClicked = sprite(1).camera.modelUnderLoc((the
mouseLoc - tOffset))
"modelsUnderLoc":
gModelsClicked = sprite(1).camera.modelsUnderLoc((the
mouseLoc - tOffset), gNumberOfModels, gSimpleOrDetailed)
"modelsUnderRay":
gModelsClicked =
member("3d").modelsUnderRay(spriteToWorld, vector(0, 0,
-1), gNumberOfModels, gSimpleOrDetailed)
end case
The third thing the handler does is display, in the scrolling field, the list of models clicked on the Stage according to the value of gModelsClicked .
if gModelsClicked = VOID then
member("which model display").text = "Models picked: " &
RETURN & "<VOID>"
else
member("which model display").text = "Models picked: " &
RETURN & gModelsClicked
end if
end
The following handler positions the overlay according to where the user clicked in the sprite. The handler is called by the frame script on frame 10:
on setOverlayLoc
This code puts the overlay in the upper left corner of the sprite if the user clicks without clicking a model.
if (gModelsClicked = VOID) or (gModelsClicked = []) then
sprite(1).camera.overlay[1].loc = point(30, 30)
If the user does click a model, the value stored in gModelsClicked will be either a model, a linear list of models, or a linear list of property lists, depending on which function is used and how its parameters are set.
else if listP(gModelsClicked) then
The above Lingo checks the first item in the list. If the first item in the list is also a list, gModelsClicked is a linear list of property lists. The #model property from the first property list is the model the user clicked.
The value of the #model property returned by modelsUnderLoc and modelsUnderRay is a reference to a model, not the name of a model. It has the form model("whichModel") but acts as though it had the form member("whichCastmember").model("whichModel") .
if listP(gModelsClicked[1]) then
modelPos = (gModelsClicked[1].model).transform.position
The following code places the overlay on the model the user clicks.
sprite(1).camera.overlay[1].loc =
sprite(1).camera.worldSpaceToSpriteSpace(modelPos)
else
If gModelsClicked is a list and the first item in that list is not itself a list, gModelsClicked is a linear list of references to models. The first item in this list is a reference to the model the user clicked.
modelPos = gModelsClicked[1].transform.position
sprite(1).camera.overlay[1].loc =
sprite(1).camera.worldSpaceToSpriteSpace(modelPos)
end if
else
If the value of gModelsClicked is not a list, then it is a reference to a model.
modelPos = gModelsClicked.transform.position
sprite(1).camera.overlay[1].loc =
sprite(1).camera.worldSpaceToSpriteSpace(modelPos)
end if
end
|