Variables and Control Structures Part 1: Property Variables

example

Variables are containers for values. For example, if you refer to "sprite(me.spriteNum)" a lot in your handler you could just assign it to a variable. like this:

spr = sprite(me.spriteNum)

then you would use your variable like this:

on mouseWithin me
      spr = sprite(me.spriteNum)
      spr.rotation=spr.rotation +3

end

if you want the variable to be available to other handlers in your script, you would declare a property variable like this:

property pSpr

on beginSprite me
      pSpr = sprite(me.spriteNum)
end

on mouseWithin me
      pSpr.rotation = pSpr.rotation +10

end

on mouseUp me
      pSpr.color = rgb("FF0000")
end

--------

The property variable pSpr can be used by any handler inside the script but does not exist outside of the script. Only global variables are available to the whole movie.

--------

in this example a start value for the rotation speed is given "on beginSprite" and then the rotation speeds up each time the playhead loops through the frame:

example

 

property p_speed


on beginSprite me
      p_speed = 2
end


on mouseWithin me
      p_speed = p_speed + 1
      sprite(me.spriteNum).rotation=sprite(me.spriteNum).rotation +p_speed
end

 

Variables and Control Structures Part 2: Control Structures: the if statement

if ... then ..... end if
An if statement basically checks if a certain condition is true. for example here, it checks if the sprite has moved to the right side of the stage and if yes, then it resets the sprite back to 0.

example

on exitFrame me
      sprite(me.spriteNum).locH = sprite(me.spriteNum).locH +3
      if sprite(me.spriteNum).locH = 420 then
            sprite(me.spriteNum).locH = 0
      end if
end

 

With if...then, and else if ... then, you can control your sprites in a more complex way. for example, you can make a sprite speed up and then slow down again...

example

property p_speed
property p_inside

on beginSprite me
      p_speed = 0
      p_inside = 0
end

 

--- p_inside will be 1 as long as the mouse does not leave the sprite
on mouseEnter me
      p_inside = 1
end

---p_inside will be 0 when the mouse is outside of the sprite
on mouseLeave me
      p_inside = 0
end

--- the "on exitFrame" handler causes the animation to run in a loop.
on exitFrame me

---send continuous events
      if p_speed > 0 then
            p_state = 1
      else
            p_state = 0
      end if


--- make it speed up
      if p_inside = 1 then
            sprite(me.spriteNum).rotation = sprite(me.spriteNum).rotation + p_speed
            p_speed = p_speed + 1

--- make it slow down

      else if p_inside = 0 and p_state = 1 then
            sprite(me.spriteNum).rotation = sprite(me.spriteNum).rotation + p_speed
            p_speed = p_speed - 0.5

--- do nothing
else if p_inside = 0 and p_state = 0 then
            sprite(me.spriteNum).rotation = sprite(me.spriteNum).rotation
      end if
end

 

or you can make the movement stop and then start again

example

 

property p_click


on beginSprite me
     p_click = 0
end

on mouseUp me
     if p_click = 0 then
          p_click = 1
     else
          p_click = 0
     end if
end


on exitFrame me
     if p_click = 1 then
          sprite(me.spriteNum).locH = sprite(me.spriteNum).locH +1
          sprite(me.spriteNum).member = 4
     else
          sprite(me.spriteNum).locH = sprite(me.spriteNum).locH
          sprite(me.spriteNum).member= 2
     end if
end

 

with the function "the clickOn" you can figure out if a sprite other than the current one has been clicked.
Function; returns the last active sprite clicked by the user. An active sprite is a sprite that has a sprite or cast member script associated with it. When the user clicks the Stage, clickOn returns 0.
To detect whether the user clicks a sprite with no script, you must assign a placeholder script to it ("--," for example) so that it can be detected by the clickOn function.
Similar functions: the rollOver (detects rollOvers), the doubleClick (detects double clicks).

 

example