4 Replies Latest reply on Oct 19, 2011 12:24 PM by Natalie Butler

    Help with basic behavior control?

    Natalie Butler Level 1

      So, I'm pretty much a beginner to Lingo and am developing a simple interactive game. I'm finding it hard to find direct answers to many of my questions, so I'm hoping that this forum will serve me well.

       

      How can I take a complexly-written behavior - the Random Movement behavior (the one included as a default in the library palette) - and say that it can only run while the mouse is up? Where in the code would I tell it to check to see if the mouse is up before it actually runs the behavior?

       

      I want the Random Movement behavior to only be called when the mouse is up, and while the mouse is DOWN, I want a separate movement behavior to run instead. I'm assuming I'd have to direct each behavior to check the state of the mouse every single frame (on enterFrame or something of that nature), but I'm not sure where to put that kind of thing in this pre-written code when there are all sorts of other things going on.

       

      Thanks to anyone who can help. I know it's not a complex question, but I'm afraid I don't have anyone specific to ask for help as far as Lingo goes.

        • 1. Re: Help with basic behavior control?
          James Newton, ACP Level 3

          In the Random Movement and Rotation behavior, at line 76, change this...

           

          on prepareFrame me

            if NOT(pError) then

              mUpdate me

            end if 

          end prepareFrame

           

          ... to this:

           

          on prepareFrame me

            if NOT(pError) then

              if the mouseUp then

                mUpdate me

              end if

            end if 

          end prepareFrame

           

          Does that do what you want it to do? Does it give you an idea about how to change the other behavior on the same sprite, so that it only operates if the mouse is down?

          • 2. Re: Help with basic behavior control?
            Natalie Butler Level 1

            Yes, thank you so much! Using the if statement within prepareFrame works with my other behavior too. Great!

             

            Now, one further question if you or anyone else knows the answer:

             

            So, the two behaviors I am switching between now on mouseUp and mouseDown are the default random movement (randomMove) and sprite track sprite (followSprite).

            The sprite they are following is a completely opaque one that has a Sprite Track Mouse behavior.

             

            The problem I'm seeing now is that the sprites' positions for randomMove and followSprite seem to be entirely independent of each other. When you switch between activating randomMove and followSprite (with the mouse button), the sprites seem to have their own set of locations that I'm assuming are contained within the code that are relative to their location on stage within that behavior only. In essence, when they move between these two behaviors, I want their transitions to be fluid. I want the calculations of their positions to be dependent on their location when the frame updates, not the last location they were at when the behavior last made its calculations. Otherwise, they jump all over the screen.

             

            I know there's got to be a way to fix this, but I'm having a hard time wrapping my mind around the default coding for these behaviors. I realize it has to do something with setting the loc variables, but there are so many things going on within the script that I don't know how to proceed.

            • 3. Re: Help with basic behavior control?
              Production Monkey Level 3

              There are a couple of problems with what you want to do. Both of these behaviors are changing the location of the sprite so when you attach both of them they are basically fighting each other, and both behaviors are time based so when for example you let the mouseup, the Random Movement behavior jumps to where it would have been if you had let it update.

               

              My idea was to write a behavior that toggles which behavior is attached. As usual it turned out to be more work than I thought, but I got something working if you woul like to try it out.

               

              Step 1. Undo the change that openspark suggested. It doesn't work with my code.

              Step 2. Around line 275 in the same script is the following line: "vP0 = pPath.p3" 

                           Change it to "  vP0 = pSprite.loc".

              Step 3. Paste the behavior code below into a Behavior Script.

              Step 4. Remove all behaviors from the "random movement" sprite and attach this new behavior.

                           Set the properties in the property dialogue box appropriately.

              Step 5. Run the program. It should work as you described.

               

              -- Random move and Follow

               

              property mouseDownBehavior

              property mouseUpBehavior

              property ancestor

               

               

              On GetBehaviorDescription me

                return "Random move and Follow. "

              end GetBehaviorDescription

               

               

              On GetPropertyDescriptionList me

                Props = Script("Random Movement and Rotation").GetPropertyDescriptionList()

                if Props.voidP then exit

               

                Tmp = Script("Follow Sprite").GetPropertyDescriptionList()

                repeat with cnt = 1 to tmp.count

                  propName = tmp.getPropAt(cnt)

                  Props[propName] = tmp[cnt]

                end repeat

                return Props

              end GetPropertyDescriptionList

               

               

              on new me

                mouseUpBehavior = Script("Random Movement and Rotation").new()

                mouseDownBehavior = Script("Follow Sprite").new()

              end

               

               

              on beginsprite me

                -- Transfer all the GPDL props to both behaviors that we are usurping

                repeat with cnt = 1 to me.count

                  propName = me.getPropAt(cnt)

                  mouseDownBehavior[propName] = me[cnt]

                  mouseUpBehavior[propName] = me[cnt]

                end repeat

               

                -- assing this sprite to these behaviors

                mouseDownBehavior[#spriteNum] = me.spriteNum

                mouseUpBehavior[#spriteNum] = me.spriteNum

               

                -- initialize

                mouseDownBehavior.beginSprite()

                mouseUpBehavior.beginSprite()

               

                ancestor = mouseUpBehavior  -- begin with random movement behavior

               

                -- route mouse events to this sprite

                  sp = sprite(me.spriteNum)

                  the mousedownscript = String(sp) & ".setMouseDownBehavior()"

                  the mouseupscript = String(sp) & ".setMouseUpBehavior()"

              end

               

               

              on prepareFrame me

                ancestor.prepareFrame()  -- forward event to behavior

              end

               

               

              on setMouseDownBehavior me

                ancestor = mouseDownBehavior

                ancestor.beginSprite()

              end setMouseDownBehavior

               

               

              on setMouseUpBehavior me

                ancestor = mouseUpBehavior

                ancestor.beginSprite()

              end setMouseUpBehavior

               

               

              on endSprite me

                -- set mouse events back to normal behavior

                the mousedownscript = ""

                the mouseupscript = ""

              end

              -------------------------------------------------------------

              • 4. Re: Help with basic behavior control?
                Natalie Butler Level 1

                I apologize for the late reply; I've been on vacation.

                This sounds more along the lines of what might work. I was thinking creating a third behavior to control the two might be the best way to do it.

                 

                The error I'm getting now though:

                "Random Movement and Rotation behavior movement limitations are too small to allow for full rotation of sprite."

                I'm assuming this is because of how it defines the movement parameters when the randomMove behavior is called? The followSprite behavior does not initialize either.