Animated Brushes 5

A little experiment with 'Animated Brushes'. There is one hidden 'leader' brush that animates around in a circle. There are 10 hidden 'wanderer' brushes that head towards the leader (most of the time). There are 100 other 'follower' brushes that pick a random wanderer and head towards its, occasionally changing wander mid journey.

Click to create a new set of follower brushes. The source (DirMX movie) is available here.

The main behaviour that drives the animation is as follows:

property myBuffer, myOutput, myPixel, myCanvasColour

property myBrushList, myWanderList, myLeader

on exitFrame me
  -- loop on the frame
  go to the frame

on beginSprite (me)
  -- create a reference to the stage's image
  myOutput = (the stage).image 
  -- create a 'buffer' image. The brushes will paint
  -- to this image 'offscreen'. When all the brushes
  -- have finished painting, then this buffer is 
  -- painted to the screen
  myBuffer = myOutput.duplicate()
  -- Create a little 'helper' pixel. Rather than filling
  -- the buffer with black each frame and then letting
  -- the brushes paint on it, this pixel will be copied 
  -- to the buffer with a lowish blend level (which means
  -- that the brush strokes will leave trails briefly)
  myPixel = image(1,1,myBuffer.depth)

  -- Create an empty list to store 'brush' objects in
  myBrushList = []
  myWanderList = []
  myLeader = script("Leader").new()

on enterframe (me)
  -- check if there are enough brushes. If not, then add more
  if myWanderList.count < 10 then me.AddWanderer()
  if myBrushList.count < 100 then me.AddBrush()
  -- fade out the last image that was painted 
  -- (lower blend levels reveal more of the previous image)
  myBuffer.copyPixels(myPixel, myBuffer.rect, myPixel.rect,[BlendLevel: 12])
  -- tell the brushes to paint themselves
  call(#Animate, myWanderList, myLeader)
  -- Randomise(myBrushList)
  mx = myBrushList.count
  repeat with i = 1 to mx
    myBrushList[i].Animate(myWanderList, i)
  end repeat
  tmp = myBrushList[1]
  -- finally, copy the buffer image to the output (so it is visible)
  myOutput.copyPixels(myBuffer, myOutput.rect, myBuffer.rect)

on mouseDown (me)
  --if the doubleClick then 
  --end if

on AddWanderer (me)

  aBrush = script("wanderer").new()
  -- add this brush to the list


on AddBrush (me)

  -- create a new brush object


  aBrush = script("follower").new()

  -- initialise it by passing in a reference to the buffer image


  -- add this brush to the list

  --  myBrushList.add(aBrush)

  if myWanderList.count then

    myBrushList.addAt(random(myWanderList.count), aBrush)


    myBrushList[1] = aBrush

  end if


First published 21/05/2005