1 Reply Latest reply on Mar 9, 2011 8:18 PM by jchunick

    2D Collision, whats wrong with this script?

    Russel Kane

      Hey I'm quite new to director and I'm trying to get 2d collision working. I have a square block as the player and a square stage. Can anyone spot what is wrong with this script? The player doesn't move at all using this script. Thanks in advance

       

      The movie script

      global

       

      PlayerSpriteNum, Flag

      on

       

      detectcollision(MySpriteNum, Thrust)

      r1 =

      sprite(MySpriteNum).rect

      repeat with

       

      Myloop = 2 to 5

      TargetSprite =

      channel(MyLoop).sprite

      r2 =

      sprite(TargetSprite).rect

       

       

      if

       

      Flag = 1 then

      r1 = r1 +

      rect(Thrust,0,0,0)

       

      else if

       

      Flag = 2 then

      r1 = r1 +

      rect(0,0,Thrust,0)

       

      else if

       

      Flag =3 then

      r1 = r1 +

      rect(0,Thrust,0,0)

       

      else if

       

      Flag =4 then

      r1 = r1 +

      rect(0,0,0,Thrust)

      end if

       

      if

       

      intersect(r1, r2) <> rect(0,0,0,0) then

      return TRUE

      else

      return FALSE

      exit

      end if

       

      end repeat

       

      end

       

       

       

      the script attached to the player

       


      property

       

      PPosX, PSpeedXP, PSpeedXN

      property

       

      PPoxY, PSpeedYP, PSpeedYN

      global

       

      PlayerSpriteNum, Flag

      on

       

      beginsprite me

      sprite

       

      (1).locH = 190

      sprite

       

      (1).locV = 188

      PPosX =

      sprite(1).locH

      PPosX =

      sprite(1).locV

      PSpeedXP =

      10

      PSpeedXN = -

      10

      PSpeedYP =

      10

      PSpeedYN = -

      10

      Mychannel =

      1

      PlayerSpriteNum =

      1

      end

      on

       

      enterframe me

      if

       

      (_key.keyPressed(123) ) then --move left

      Flag =

      1

      if

       

      detectcollision(1, PSpeedXN)then

      sprite

       

      (1).locH = (sprite(1).locH)+PSpeedXN

      end if

       

       

      end if

       

      if

       

      (_key.keyPressed(124) ) then --move right

      Flag =

      2

      if

       

      detectcollision(1, PSpeedXP)then

      sprite

       

      (1).locH = (sprite(1).locH)+PSpeedXP

      end if

      end if

       

      if

       

      (_key.keyPressed(125) ) then --move down

      Flag =

      3

      if

       

      detectcollision(1, PSpeedYP)then

      sprite

       

      (1).locV = (sprite(1).locV)+PSpeedYP

      end if

      end if

       

       

      if

       

      (_key.keyPressed(126) ) then --move up

      Flag =

      4

      if

       

      detectcollision(1, PSpeedYN)then

      sprite

       

      (1).locV = (sprite(1).locV)+PSpeedYN

      end if

      end if

       

      end