Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random bird

Status
Not open for further replies.

babytarantula

Technical User
Feb 23, 2006
6
MX
Hi do you know which code i should use in order to make a bird fly around the screen randomly and without leaving the screen


Thanks a lot!!!
 
There are many ways, but here's one example:
Code:
-- Behaviour script attached to a Sprite
property pSprite
property pSpeed
property pIdle
property pStageWidth, pStageHeight
property pCounter
property pLimit
property pVector

on beginSprite me
  pSprite = sprite(me.spriteNum)
  pSpeed = 3 
  pIdle = 30
  stageRect = _movie.stage.rect
  pStageWidth = stageRect[3]-stageRect[1]
  pStageHeight = stageRect[4]-stageRect[2]
  pCounter = 0
  setNewVector()
end beginSprite

on enterFrame me
  currentLoc = pSprite.loc
  if currentLoc[1] < pSpeed then reverseVector(1)
  if currentLoc[1] > (pStageWidth - pSpeed) then reverseVector(1)
  if currentLoc[2] < pSpeed then reverseVector(2)
  if currentLoc[2] > (pStageHeight - pSpeed) then reverseVector(2) 
  if pCounter > pLimit then setNewVector()
  pSprite.loc = pSprite.loc + pVector
  pCounter = pCounter + 1
end enterFrame

on reverseVector arg 
  pVector[arg] = -pVector[arg]
  pSprite.loc = pSprite.loc + pVector
end reverseVector

on setNewVector
  pCounter = 0
  pLimit = random(pIdle)
  pVector = point(random(3)-2, random(3)-2)*pSpeed
  if pVector = point(0, 0) then
    setNewVector()
  else
    return
  end if
end setNewVector
--
As you see I wrote this in such a way so that the Sprite moves about very randomly. (Not very "bird".) Adjusting "pSpeed" and "pIdle" value is the first place to tweak.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top