Another Property-Changing Example
Don't worrythis example is simpler than the previous one. We're going to take the movie of Jake the fish swimming into the pea shark's mouth and getting scared, and we're going to add a little bit to it. We'll make Jake face in whatever direction the mouse is moving. If the mouse is moving left, Jake will face to the left. If the mouse is moving right, Jake will face to the right.
Open chapter3/drag_jake2.fla.
Click the frame in the actions layer and open up the Actions panel.
Enter the following code:
// Set Jake's position _root.lastXPosition = jake._x;
This sets a global variable called _root.lastXPosition, and initializes it to Jake's current _x.
Click on Jake and open the Actions panel.
Enter the following code:
onClipEvent(enterFrame) { startDrag(this, true); if(this.hitTest(_root.shark)) { this.gotoAndStop("scared"); _root.shark.gotoAndStop("hungry"); } else { this.gotoAndStop("calm"); _root.shark.gotoAndStop("normal"); } // set direction if(this._x < _root.lastXPosition) { this._xscale = -35; } else if(this._x > _root.lastXPosition) { this._xscale = 35; } _root.lastXPosition = this._x; }
You'll notice we added a new chunk of code toward the end where we set Jake's direction, based on his current _x and what his last x-position was. Take a look at how we're using _xscale here. If Jake is moving to the left (the first condition), then we change the _xscale to 35. The negative sign flips Jake around. If Jake is moving to the right (his _x is bigger than lastXPosition), then we set him to his original orientation. It's then necessary to update lastXPosition to keep up with Jake.
We're using 35 instead of 100 because this is a smaller version of Jake, and the full size would be too big for this movie. If you need convincing, change the values to 100 and 100. You'll see what I mean.
Now let's add one line of code, just to make the movie a little easier to look at:
onClipEvent(enterFrame) { startDrag(this, true); Mouse.hide(); _ }
Since Jake's so small, the mouse cursor overshadowed him. Fortunately, that's an easy thing to fix. Mouse.hide() simply hides the mouse cursor. It's still there, and responding to the user's movements, but it can't be seen. You can bring it back with Mouse.show().