07.12.2012 Views

Adobe Director Basics

Adobe Director Basics

Adobe Director Basics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ADOBE DIRECTOR BASICS<br />

3D: Controlling action<br />

property pPuck -- model("Puck")<br />

property pAcceleration -- -0.001<br />

property plPositions -- [: , ...]<br />

property pThrowTime -- milliseconds when puck was released<br />

property pThrowSpot -- world position where puck was released<br />

property pSpeed -- speed of puck when it was released<br />

property pDirection -- direction of travel of puck on release<br />

property pTimeToStop -- milliseconds until puck stops moving<br />

on mouseDown(me)<br />

pPuck = member("3D").model("Puck")<br />

pAcceleration = -0.001<br />

plPositions = [:]<br />

plPositions.sort() -- enables list.findPosNear()<br />

(the actorList).append(me)<br />

end mouseDown<br />

on stepFrame(me)<br />

-- ... other code to set the position of the puck<br />

plPositions.addProp(the milliseconds, pPuck.worldPosition)<br />

end stepFrame<br />

When you release the mouse button, the mThrow() handler is called. This works out where the puck was about 200<br />

milliseconds before it was released, and where it was on the frame immediately before it was released. Below is a<br />

simplified version of the code.<br />

vCount = plPositions.count()<br />

vLast = the milliseconds - 200 -- approximate time<br />

vIndex = plPositions.findPosNear(vLast)<br />

pThrowTime = plPositions.getPropAt(vCount)<br />

vLast = plPositions.getPropAt(vIndex) -- recorded time<br />

vElapsed = pThrowTime - vLast<br />

vThen = plPositions[vIndex]<br />

pThrowSpot = plPositions.getLast()<br />

The variable vThen now contains the puck's position about 200 milliseconds ago, pThrowSpot contains the position<br />

where it was on the last frame, and vElapsed contains the time between these two positions. Subtracting one position<br />

from the other gives the displacement between the two positions.<br />

The mThrow() handler calculates the speed and direction of the movement in two separate property variables, and also<br />

how long it will take the puck to come to a halt due to the effect of friction.<br />

pDirection = pThrowSpot - vThen<br />

pSpeed = pDirection.magnitude / vElapsed<br />

pDirection.normalize() -- unit vector<br />

pTimeToStop = -pSpeed / pAcceleration<br />

Calculating the current position of the puck<br />

After you release the puck, on every frame, the mSlide() handler calculates how much time has elapsed since the puck<br />

was released. It uses that information to determine where the puck is at the current point in time. Here is a simplified<br />

version of the handler. It incorporates the getDistance() handler from the Linear Motion script:<br />

Last updated 8/26/2011<br />

263

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!