>> Dear,
>> i want to ask the script is ;
>> when sprite 1 didn't move 1 min(ex.using Draggable behavior in sprite 1),
>> then
>> it will auto detect the behavior from library (ex.Random Movement and
>> Rotation) and run the function.
>>
>> thank
>> jeand
Hi jeand,
Checking a how its been since a sprite moved is relatively straightforward,
but changing behaviours of sprites can be a little complicated. Below is a
basic example of how you can have one behaviour work out how long its been
since its sprite has moved, then replace the behaviour with another
behaviour to do random movement.
This isn't a complete behaviour, but hopefully its enough to get you
started.
---- sprite behaviour ---
property mySpriteRef, myLastloc, myTimeOfMove
on beginSprite me
-- store the sprite reference (for convenience)
mySpriteRef = sprite(me.spriteNum)
-- initialise the properties of this behaviour instance
myLastloc = mySpriteRef.loc
myTimeOfMove = the milliseconds
end
on exitframe me
-- work out whether the sprite has moved.
-- if not moved, work out how long since it was last moved
if mySpriteRef.loc <> myLastloc then
-- this sprite has moved; reset the timer
myLastloc = mySpriteRef.loc
myTimeOfMove = the millisecond
else
-- haven't moved, so work out how long since it did move
secondsSinceMove = (the milliseconds - myTimeOfMove) / 1000
if secondsSinceMove > 60 then
-- it has been a minute since the sprite moved
-- so attach the random movement behaviour
newBehaviour = script("RandomMovement").new(mySpriteRef)
me.mSetMyBehaviour(newBehaviour)
end if
end if
end
on mSetMyBehaviour me, pBehaviourInstance
-- this removes whatever behaviours are currently attached
-- and replaces them with an instance of the new script
if objectP(pBehaviourInstance) then
mySpriteRef.scriptInstanceList= [pBehaviourInstance]
else
errMsg = "Hey, you should add script instances to the scriptInstanceList"
errMsg = errMsg & return & return & "You attempted to add a "
errMsg = errMsg & pBehaviourInstance.ilk
alert errMsg
halt
end if
end
--- Example "RandomMovement" Behaviour ---
property mySpriteRef, myVirtualLoc, myVector, myPlayingfield
on new me, pSriteRef
mySpriteRef = pSriteRef
myVirtualLoc = mySpriteRef.loc
stageWidth = (the stageRight - the stageLeft)
stageHeight = (the stageBottom - the stageTop)
myPlayingfield = [0,0,stageWidth,stageHeight]
me.mRandomVector()
return me
end
on mouseDown me
me.mRandomVector()
end
on exitframe me
me.mAnimate()
end
on mAnimate me
if mySpriteRef.left < myPlayingfield[1] then myVector = myVector * [-1,1]
else if mySpriteRef.right > myPlayingfield[3] then myVector = myVector *
[-1,1]
if mySpriteRef.top < myPlayingfield[2] then myVector = myVector * [1,-1]
else if mySpriteRef.bottom > myPlayingfield[4] then myVector = myVector *
[1,-1]
--
myVirtualLoc = myVirtualLoc + myVector
mySpriteRef.loc = myVirtualLoc
end
on mRandomVector me
x = (20 - random(50))/20.0
y = (20 - random(50))/20.0
myVector = point(x,y)
end
Attaching behaviours to sprites can be a little tricky. You can use
ancestors, scriptLists and scriptInstanceLists. In this example, I've used
the scriptInstanceList to replace whatever behaviours are attached to the
sprite with the new behaviour.
Also note that if you are using behaviours from the library palette, you
will need to modify them so that they can be attached without needing to run
the 'propertyDescriptionDialogBox" to set their properties, and if they are
doing any initialisation on 'beginSprite', then you will need to change this
to "on new" (since behaviours added to sprites via lingo and the
scriptInstanceLists do not receive the 'beginSprite' event automatically).
Luke
__________________________________________________________________________
Luke Wigley
Multimedia Slave
Mecca Medialight Pty Ltd
Mecca Medialight: Medialight Sound Studio:
111 Moor Street 1 Bakehouse Lane
Fitzroy, Vic. 3065 North Fitzroy, Vic. 3068
Tel +613 9416 2033 Tel +613 9416 2033
Fax +613 9416 2055 Fax +613 9416 2055
www.medialight.com.au
__________________________________________________________________________
[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi To post messages to the list,
email [EMAIL PROTECTED] (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo. Thanks!]