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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

balls Bouncing off

Status
Not open for further replies.

chenyang

Technical User
Feb 6, 2005
1
GB
i just starting using director MX 2004, Lingo is brand new to me.
i need help in writing the lingo script for a ball to bounce off an object when it touches it..

i have set up the boundaries as 400 x 300
i have a four sided box in the middle as
sprite3(L=133,T=121,R=260,B=161)Left Top Right and Bottom of the sprite respectively.

ball size is 34pixels in diameter

below is the script where the boudaries are set
if ballH < 0 then
hmove = 4 - random(3)
vmove = 4 - random(8)
puppetsound(1), "tone3a"
end if

if ballH > 366 then
hmove = -(4 - random(3))
vmove = 4 - random(8)
puppetsound(1), "tone3a"
end if

if ballV < 0 then
vmove = 4 - random(3)
hmove = 4 - random(8)
puppetsound(1), "tone4"
end if

if ballV > 266 then
vmove = -(4 - random(3))
hmove = 4 - random(8)
puppetsound(1), "tone4"
end if

now how do i make the ball bounce off the square ive drawn?
 
This is probably close to what you want to achieve? This is a behaviour script ant should be attached to your ball sprite.
--
global gVMove, gHMove

on beginSprite me
gVMove = random(4) + 2
gHMove = random(4) + 2
end beginSprite

on enterFrame me
tBallV = sprite(me.spriteNum).locV
tBallH = sprite(me.spriteNum).locH
if tBallV > 266 or tBallV < 0 then
bounce("v")
end if
if tBallH > 366 or tBallH < 0 then
bounce("h")
end if
if tBallV > 87 and tBallV < 161 and tBallH > 99 and tBallH < 260 then
if tBallV > 91 and tBallV < 157 then
bounce("h")
else
bounce("v")
end if
end if
sprite(me.spriteNum).locV = tBallV + gVMove
sprite(me.spriteNum).locH = tBallH + gHMove
end enterFrame

on bounce arg
case arg of
"v": gVMove = 0 - gVMove
"h": gHMove = 0 - gHMove
end case
end bounce
--

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top