Detecting the Edge of the Stage


Now that we have our project working, we will add one final feature. You'll notice that in our current version of this project, the target clip keeps moving past the edge of the stage. Now we will add some conditional logic to our move functions so that they will detect the edge of the stage.

1.

Open Key03.fla.

We'll continue building on the file you worked with in the preceding exercise. The changes we will be making here are represented in the Key04.fla file in the Lesson03/Completed folder.

2.

Replace the move functions with the following script:

  function moveLeft():Void {     var leftWall:Number = 0;     if(target._x > leftWall) {       target._x -= SPEED;     }else {       target._x = leftWall;     }   }   function moveRight():Void {     var rightWall:Number = Stage.width - target._width;     if(target._x < rightWall) {       target._x += SPEED;     }else {       target._x = rightWall;     }    }   function moveUp():Void {     var topWall:Number = 0     if(target._y > topWall) {       target._y -= SPEED;     }else {       target._y = topWall;     }   }   function moveDown():Void {     var bottomWall:Number = Stage.height - target._height;     if(target._y < bottomWall) {       target._y += SPEED;     }else {       target._y = bottomWall;     }   }


In each function, we are calculating the edge of our stage and making sure that the target clip hasn't gone too far. If it has, then we position it up against the edge. The edge values are represented by the topWall, bottomWall, leftWall, and rightWall properties. This is a simple example of an if/else statement.

3.

Choose Control > Test Movie.

Now when the movie plays you can try and get the target off the stage. But as you can see, the target stops moving when it reaches the edge of the stage. Therefore, we have just created a boundary for our moving target.

4.

Close the test environment to return to the authoring environment, and save your work as Key04.fla.

This step completes the exercise and this lesson. By using conditional logic in your scripts, you can greatly increase your project's dynamism and provide a unique experience to every person who interacts with it.




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net