3 Replies Latest reply on Aug 15, 2013 2:23 PM by rduane

    Random Moving Sprite

    DirBuddy12 Level 1

      I have a game where I want a ball(sprite 28) to move randomly until it intersects with a paddle(sprite 27), this is where I have got so far.

       

       

      property  pIncrement

       

      on prepareFrame me

        pIncrement = random(240)

       

      end

       

       

      on beginSprite me

       

        set the loc of sprite(28) =  the loc of sprite(28) + pIncrement

        if sprite 28 intersects  sprite 27 then

          set the loch of sprite(28) = the loch of sprite(28) + 5

        end if

         

      end

       

      This code does not produce any errors but it does not move the sprite, any advice on where I have gone wrong would be appreciated. Thanks

        • 1. Re: Random Moving Sprite
          Sean_Wilson Adobe Community Professional

          Where is the code above attached? When you select the code cast member in the library and open the Property Inspector and activate the Script tab, what Type of script does it indicate this code is?

          • 2. Re: Random Moving Sprite
            DirBuddy12 Level 1

            The code is attached to sprite 28 and is type behaviour.

            Is behaviour not correct because it is causing the sprite to change it properties?

            • 3. Re: Random Moving Sprite
              rduane Level 1

              Hmm, where to begin?  Part of the problem is that the beginSprite handler only gets called one time for the sprite - even if the movie is looping.  And I have to assume you have your movie set to loop, otherwise Director will execute the prepareFrame handler, execute the beginSprite handler and then go on to the next frame of the movie! The code you have in that handler would have been better served in an exitFrame handler, plus you ought to add the line "go to the frame" to keep the script looping on that frame, and keep the prepareFrame and exitFrames repeatedly firing.  But then again, why not remove the prepareFrame handler, and just put the "pIncrement = random(240)" as the first line of the exitFrame handler?

               

              Then there's the problem of the fact that the "loc of sprite(28)" is a point with a locH and a locV value.  If you add an integer to this, the integer will be added to both the locH and the locV.  Is that what you really wanted?  This script (with my suggested changes) is only going to keep the sprite moving down and to the right until (and only if) the sprites eventually intersect, then it will keep moving the sprite to the right 5 pixels at a time until they don't intersect anymore, then continue moving down and to the right forever.  Cool.