Articles Archive
Articles Search
Director Wiki
 

Scrollbar Cast Cycling

March 28, 1999
by Pat McClellan

Dear Multimedia Handyman,

I have seen a way to cycle though pictures by using a scroller bar to do so. I know it is possible to do this while scrolling text, but how can you do it to scroll thru images?

Brian Sutherland

Dear Brian,

A while back, I wrote an article and a set of behaviors for scrolling. Those behaviors included one for the scroll bar buttons (up, down, left, or right), one for the scroll "thumb", one for the scroll thumb track, and one to apply to all the sprites on the stage which you want to scroll. I think what we can do is re-use the first three of those behaviors (button, thumb, and track) and then create a new one which I'll call scrollCast.

The whole idea is that the button, thumb and track behaviors simply adjust a global variable for the horizontal (gScrollH) or vertical (gScrollV) scroll amount, then send out a message to all sprites that it's time to scroll. All we need to do is create the scrollCast behavior which can look at that global variable and adjust its castMember accordingly.

The key is that we have to devide the number of cast members by the number of pixels in the scroll thumb's range. For example, let's say that the scroll thumb can travel 100 pixels top to bottom. If we have a total of 50 cast members to cycle through, then it's easy to see that we need to change the cast member each time the scroll thumb travels 2 pixels. You'll rarely have such an easy division. In fact, you'll want to use the float() function to get an accurate decimal value.

Download Mac (151 K) or PC (106 K)format.

In this example, I've got 52 castmembers to cycle through and the scroll thumb can move a total of 212 pixels. Since we see the first member without scrolling at all, that's only additional 51 castmembers to scroll over 212 pixels. So, we'll divide the total number of castmembers minus 1 by the range of scroll (212). That means that each pixel of scroll amounts to a castmember change of 0.2406. Obviously, I can change by a fractional amount, but for accuracy, it's best to calculate it to that degree, then convert back to the nearest integer at the end.

When told to scroll, the scrollCast behavior looks at the global variable (gScrollV in this example), multiplies that amount by the ratio I described above, then adds the result (in integer format) to the number of the first cast member in the series. That's the logic, here's the code.

-- Copyright c. 1999, ZZP Online, LLC.  Free use for readers of 
-- Director Online's "The Multimedia Handyman". Visit Director 
-- Online at http://www.director-online.com.
--This behavior works with the ScrollButton, ScrollThumb, and 
-- ScrollThumbTrack behaviors. Instead of horizontal or vertical 
-- motion, this behavior cycles through a series of adjacent cast members.
-- Apply to the first cast member in the series
property pNumOfCastMems  
-- number of cast members in cycle
property pMyIncrement    
-- number of pixels per castMem
property pMyInitMemNum   
-- the memberNum of the first castMem in the series
property pMyCastLibNum   
-- the castLibNum of the first castMem in the series
property pSprite         
-- the spriteNum of me
property pScrollRange    
-- number of pixels in scroll
global gScrollV          
-- amount scroll has been incremented Vertically 
-- from initial value
global gScrollH          
-- amount scroll has been incremented Horizontally 
-- from initial value
on getPropertyDescriptionList
  set pdlist to [:]
  addprop pdlist, #pNumOfCastMems,[ #comment:"Cast members ¬
    in the series:", #format: #integer, #default:50] 
  addprop pdlist, #pScrollRange, [#comment:"Number of pixels ¬
    in scroll:", #format:#integer, #default:100]
  return pdlist 
end
on beginSprite me
  set pSprite = the spriteNum of me
  set myMember = the member of sprite pSprite
  set pMyInitMemNum = the memberNum of myMember
  set pMyCastLibNum = the castLibNum of myMember
  set pMyIncrement = (pNumOfCastMems - 1)/float(pScrollRange)
end
on scrollH me
  set the member of sprite pSprite = member (pMyInitMemNum + ¬
    integer(gScrollH * pMyIncrement)) of castLib pMyCastLibNum
  updateStage
end
on scrollV me
  set the member of sprite pSprite = member (pMyInitMemNum + ¬
    integer(gScrollV * pMyIncrement)) of castLib pMyCastLibNum
  updateStage
end

This component approach of the scroll behaviors demonstrates the beauty of object-oriented programming. Behaviors are essentially sprite specific objects. Because the behaviors I created before simply send out messages to scroll (without caring how that scroll happens) we can reuse them without modification. This scrollCast behavior makes a nice addition to the set. Good luck with your project.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

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