Articles Archive
Articles Search
Director Wiki

Director 8.5: Moving Primitives Around

June 14, 2001
by Gary Rosenzweig

In my last two columns I showed you how to use the new Director 8.5 Shockwave3D cast member to create simple objects called primitives. This week, we'll create a primitive and learn how to move it around in the 3D world. Start off by creating a new Shockwave3D cast member and attaching a behavior to it in the Score. The behavior will start by resetting the world and then it will create a box and a plane.


property pScene, pBox, pPlane

on beginSprite me

 pScene = sprite (me.spriteNum).member

  -- reset the world when done
  pScene.resetWorld ()

  -- create box and plane
  createBox (me)
  createPlane (me)


The box is created just like last time except that it is a 10x10x10 box and is positioned 35 units below the center of the model.

on createBox me

 -- create a box resource
  boxResource = pScene.newModelResource ("BoxResource", #box)
  boxResource.width = 10
  boxResource.height = 10
  boxResource.length = 10

  -- create new model
  pBox = pScene.newModel ("Box", boxResource)

  -- assign a texture to all 6 faces
  pScene.newtexture ("box texture", #fromCastMember,member ("texture-blue"))
  pScene.newshader ("box shader", #standard)
  pScene.shader ("box shader").texture = pScene.texture ("box texture")
  repeat with i = 1 to pBox.shaderList.count
    pBox.shaderList[i] = pScene.shader("box shader")
  end repeat

  -- move into position
  pBox.translate (0, -35, 0)


The plane is created simply as a visual reference. The box will seem to slide along it. The plane is 40 units below the center of the world. This makes the box 5 units higher, and since the box is 10 units along a side, it places the box perfectly on top of the plane.

on createPlane me

-- create a plane resource
  planeResource = pScene.newModelResource ("PlaneResource", #plane)
  planeResource.length = 100
  planeResource.width = 100

  -- create new model
  pPlane = pScene.newModel ("Plane", planeResource)

  -- assign a texture to the sphere
  pScene.newtexture ("plane texture", #fromCastMember, member ("texture-ground"))
  pScene.newshader ("plane shader", #standard)
  pScene.shader ("plane shader").texture = pScene.texture ("plane texture")
  pPlane.shaderList[1] = pScene.shader ("plane shader")

  -- move plane into position
  pPlane.translate (0, -40, 0)

  -- rotate plane so it is parallel to ground
  pPlane.rotate (90, 0, 0)


To move the box, we'll use keyPressed with the codes for the arrow keys. If an arrow key is pressed, then the translate function will move it along the x- or z-axis.

on exitFrame me

 -- move
  if keyPressed (123) then pBox.translate (-1, 0, 0)
  if keyPressed (124) then pBox.translate (1, 0, 0)
  if keyPressed (125) then pBox.translate (0, 0, 1)
  if keyPressed (126) then pBox.translate (0, 0, -1)

This alone will allow you to move the box around in the world. But wouldn't it be great if you could rotate the box to face another direction? This can be accomplished by using the rotate function. We'll use the , and . keys to rotate the box along the y-axis.

-- rotate
  if keyPressed (",") then pBox.rotate (0, 1, 0)
  if keyPressed (".") then pBox.rotate (0, -1, 0)


The great thing about the translate function is that it will move the box relative to its current rotation. So if you use just the up arrow and down arrow keys and the , and . keys, you'll be able to move the box pretty naturally. As a matter of fact, you may want to remove the left and right movement (key codes 123 and 124) and use those keys for the rotation instead of comma and period. This makes the movement very natural.

Here is the finished product. You'll need Shockwave 8.5 to see it in your browser. Try it with just the arrow keys first. Then, try it with just the up, down, comma and period keys.

The source Director 8.5 movie is available for download in Mac or Windows format.

Next week we'll learn about cameras and how to view the action from different points in the world.

Gary Rosenzweig is the author of six books on Macromedia Director and Flash, including his latest, Flash 5 ActionScript for Fun & Games. He also publishes an email newsletter for Flash and Director developers called the Developer Dispatch. You can subscribe to this newsletter at

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2019, Director Online. Article content copyright by respective authors.