web scripting for designers - CGR C/092 - fall 2003
Philip van Allen - v a n a l l e n @ a r t c e n t e r . e d u

room 133, Monday 4:00pm-7:00pm
room 133, Monday 7:00pm-10:00pm
all materials on this web site © copyright 2003, Philip van Allen
 
week 12b - director audio, globals/properties, duplicate sprites, collision detection

globals, properties : 


use variables to communicate between code

By default, variables used in sprite behaviors are local to that sprite, and persist only during the execution of the code. For example, in the following behavior code the variable count would equal 1 every time the sprite is clicked.

on mouseUp me
    count = count + 1
    put count
end

The count variable can never increase because it is recreated each time the code runs. In addition, the value of this variable is not available to any other code in the Director application. There are two approaches to working with variables that you want to survive longer. The first is property variables, which allows a sprite behavior to retain the value of a property variable over time. The second is global variables, which makes the variable accessible to any code running in the application.



properties : 

each sprite instance gets a personal copy of the variable

Properties of behaviors are specific to each sprite the behavior is applied to, and persist as long as the sprite is on the stage (like any variable in Flash on a movieClip). For example, if two sprites have the following behavior, they will maintain their own counts, independent of each other. This would be useful if each sprite were maintaining its own score, hit count, on/off state, etc.

property pCount

on mouseUp me
    pCount = pCount + 1
    put pCount
end

A sprite property can also be modified by other code. For example, if the above code was on sprite 2, then the following code would change the value of that property:

sprite(2).pCount = 100

 

 
globals : 

make a variable accessible to all code

Global variables have one value for the entire Director application, regardless of where it is accessed (similar to the _root.variableName technique we used in Flash). For example, if two sprites have the following behavior, they will update the same count variable. This is useful if each sprite is updating a system-wide variable, for example the total score for a game.

global gCount

on mouseUp me
    gCount = gCount + 1
    put gCount
end

Any code that declares a variable as global (using the above syntax) can access the global variable. Simply put "global variableName" at the top of the code.

 

 
global, property names : 

use a standard naming convention for your global and property variables

It is a good idea to use a standard method for naming global and property variables. This way it is clear how a variable is being used in the code. A suggested naming convention is as follows:

global gCount -- use a lowercase "g" as a prefix for all global variables

property pCount -- use a lowercase "p" for all property variables

 

 

duplicating sprites : 


creating sprites on the fly

Sample Director movie: view duplicateSprite.dcr, download duplicateSprite.dir

As with Flash, it is possible to create new sprites in the Director score while an application is running. The approach is a little different than the one we used in Flash, but the effect is the same. Here are steps needed to create a new sprite:

  1. find an open channel for the new sprite
  2. "puppet" the channel to make it possible to add a sprite to it
  3. put a cast member into the available channel
  4. set any properties of the new sprite
  5. attach a behavior to the new sprite
  6. set any properties associated with that behavior

 


the code : 

from duplicateSprite.dir

on getFreeChannel startChannel
    -- this handler finds the next available open
    -- channel starting at the number in startChannel
    repeat with i = startChannel to 999 -- loop through all the channels
        temp = string(sprite(i).member) -- get name of cast member in the channel
        if (temp = "(member 0 of castLib 0)") then
            -- if the channel is empty, there won't be a cast member
            return i -- we found an empty channel, return it to who called us
            exit repeat -- bail out of the loop since we found an empty channel
        end if
    end repeat
end

on mouseUp me
    freeChannel = getFreeChannel(10) -- start looking for channels after channel 10
    sprite(freeChannel).puppet = TRUE -- make new channel available to add a sprite

    -- put the "seed" member into the sprite
    sprite(freeChannel).member = member("seed")
    sprite(freeChannel).locH = random(640) -- set the X location
    sprite(freeChannel).locV = random(480) -- set the Y location
    sprite(freeChannel).ink = 36 -- make background transparent
    sprite(freeChannel).moveableSprite = TRUE -- make the sprite draggable
    -- attach the "move" behavior to the sprite
    sprite(freeChannel).scriptInstanceList = [new(script "move")]
    -- set the direction used by the behavior for the new
    sprite(freeChannel).pMyDirection = random(20) - 10 sprite
end

 

 

collision detection : 


determining if two sprites intersect

Sample Director movie: view directorCollision.dcr, download directorCollision.dir

It is possible to detect if two sprites intersect in Director using the following collision detection method:

if (sprite 1 intersects sprite 2) then

This method requires that both sprites have the "matte" type of ink used, otherwise it will not work.

 


the code : 

from directorCollision.dir

property pSpeed -- declare the properties

on beginSprite me
    pSpeed = 2 -- init the pSpeed property
end

on prepareFrame me
    -- note the current location so if there is a collision, we can move back
    lastLoc = sprite(the currentSpriteNum).loc

    -- check for each of the arrow keys
    if (keyPressed(123)) then
        -- left arrow
        sprite(the currentSpriteNum).locH = sprite(the currentSpriteNum).locH - pSpeed
    end if
    if (keyPressed(124)) then
        -- right arrow
        sprite(the currentSpriteNum).locH = sprite(the currentSpriteNum).locH + pSpeed
    end if
    if (keyPressed(125)) then
        -- down arrow
        sprite(the currentSpriteNum).locV = sprite(the currentSpriteNum).locV + pSpeed
    end if
    if (keyPressed(126)) then
        -- up arrow
        sprite(the currentSpriteNum).locV = sprite(the currentSpriteNum).locV - pSpeed
    end if

    -- check for a collision, and if there is one, return to the last location
    if (sprite 1 intersects sprite 2) then
        sprite(the currentSpriteNum).loc = lastLoc
    end if
end

 

 
     

 

 

 

all materials on this web site © copyright 2003, Philip van Allen

top