Google


   


You are here: CodeIdol.com > Flash > Macromedia Flash® ActionScript: Training from the Source > Lesson3.Conditional Logic > Detecting The Edge Of The Stage

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

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.


SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > Flash > Macromedia Flash® ActionScript: Training from the Source > Lesson3.Conditional Logic > Detecting The Edge Of The Stage


   
Related tags







Popular Categories
Unix books and guides

AJAX popular information
C# language guides
Windows books and cookbooks

.......








    С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.

    Русский Polski Francais Deutsch
    support sitemap terms

© CodeIdol Labs, 2007 - 2009