3 Replies Latest reply on Aug 15, 2007 5:02 AM by James Newton, ACP

# changing direction of sprite

hi, does anyone know why this piece of code wont send the sprite in the opposite direction to that it was travelling in before it hits the other sprite??

property pSprite, pleftBound, prightBound, ptopBound, pbottomBound, pMove, pMove2
global gvScore, gvlives

on beginSprite me

pSprite = sprite(me.spritenum) -- sets pSprite to the sprite of me
pleftBound = 25 -- sets the left boundary to 50
prightBound = 750 -- sets the right boundary to 750
ptopBound = 0
pbottomBound = 550
pMove = 5
pMove2 = 5
end
on exitframe me
pSprite.loch=pSprite.loch+pMove -- moves pSprite by a horizontal increment of pMove

pSprite.locV=pSprite.locV+pMove2

if pSprite.locV < ptopBound then
pMove2 = pMove2*-1
end if

if pSprite.locV > pbottomBound then
pMove2 = pMove2*-1
end if

if pSprite.loch > prightBound then -- if pSprite passes the right boundary
pMove=pMove*-1 -- change direction

end if

if pSprite.loch < pleftBound then -- if pSprite passes left boundary
pMove=pMove*-1 -- change direction

end if

if sprite 22 intersects 23 THEN

pMove2 = pMove*-1

pMove = pMove2*-1

pMove=pMove2*-1 -- change direction

pMove2=pMove*-1 -- change direction

end if

if sprite 23 intersects 24 THEN

gvlives = gvlives - 1
put gvlives into field "lives"
if gvlives = 0 then
go to frame "gameover"

end if
end if
end

• ###### 1. Re: changing direction of sprite
Hi, i sugges you try the tutorials at this link( Animation math with Lingo)

http://www.jmckell.com/contents.html

Cheers,
Wolfgang
• ###### 2. Re: changing direction of sprite
what one in particular theres loads there
• ###### 3. Re: changing direction of sprite
quote:

Originally posted by: andy_scot
if sprite 22 intersects 23 THEN
pMove2 = pMove*-1
pMove = pMove2*-1

pMove=pMove2*-1 -- change direction
pMove2=pMove*-1 -- change direction
end if

Try stepping through the debugger to see exactly what happens to pMove and pMove2 in this section of code. You are setting pMove2 to a value based on pMove, then pMove to a value... based on the original value of pMove, which gets you back to where you started.

Try something like:

vTemp = pMove
pMove = -pMove2
pMove2 = -Temp