Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Creating Moving Objects - Adobe Flash CS3

Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Client Technologies - Adobe Flash CS3: Building an Interactive Game

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Creating Moving Objects - Adobe Flash CS3

Adding the Target: In our game, the player’s goal is to hit a moving target, which we create in this section.

Creating Moving Objects

 

Adding the Target

In our game, the player’s goal is to hit a moving target, which we create in this section. Create a keyframe in the second frame of the Target layer, then drag an instance of the tar-get movie clip from the Library onto the stage. Using the Property Inspector, position the target at the x- and y-coordinates 490 and 302, respectively. The position (0, 0) is located in the upper-left corner of the screen, so the target should appear near the lower-right cor-ner of the stage. Give the target symbol the instance name target. Right click the target symbol in the Library and select Linkage. In the box that pops up, select Export for Action-Script and enter Target in the Class field.

 

The target symbol is now linked with a class named Target. Create a new ActionScript File from the File > New menu. Save this file immediately and give it the name Target.as. This will serve as the Target class definition. In this file, add the code in Fig. 17.5.

 

The Target class has four instance variables—the speed of the Target (speed), the direction of the Target (upDown), the number of times the Target has been hit by the ball (hitCounter), and the Timer variable (moveTargetTimer). We specify that moveTarget-Timer is a Timer using the colon syntax in line 18. The first parameter of the Timer con-structor is the delay between timer events in milliseconds. The second parameter is the

 

1    // Fig. 17.5: Target.as

 

2    // Move target, set direction and speed,

 

3    // and keep track of number of blocks hit.

 

4    package

5    {

 

6          // import relevant classes

 

7          import flash.display.MovieClip;

 

8          import flash.events.TimerEvent;

 

9          import flash.utils.Timer;

10

11         public class Target extends MovieClip

12         {

 

13               var speed; // speed of Target

 

14               var upDown; // direction of Target

 

15                 var hitCounter; // number of times Target has been hit

16          

17               // timer runs indefinitely every 33 ms

 

18               var moveTargetTimer : Timer = new Timer ( 33, 0 );

19          

20               // register function moveTarget as moveTargetTimer’s

 

21               // event handler, start timer

 

22               public function Target() : void

23               {

 

24                      moveTargetTimer.addEventListener (

25                            TimerEvent.TIMER, moveTarget );

 

26                      moveTargetTimer.start(); // start timer

 

27               } // end Target constructor

28

29               // move the Target

 

30               private function moveTarget( t : TimerEvent )

31               {

 

32                      // if Target is at the top or bottom of the stage,

 

33                      // change its direction

34                      if ( y > 310 )

35                      {

36                            upDown = -1; // change direction to up

 

37                      } // end if

38

39                      else if ( y < 90 )

40                      {

41                            upDown = 1; // change direction to down

 

42                      } // end else

43

44                      y += (speed * upDown); // move target

 

45               } // end function moveTarget

46

47               // set direction of the Target

 

48               public function setUpDown( newUpDown : int )

49               {

 

50                      upDown = newUpDown;

 

51                      } // end function setUpDown

 

 

53                // get direction of the Target

 

54                public function getUpDown() : int

55                {

 

56                      return upDown;

 

57                } // end function getUpDown

58

59             // set speed of the Target

 

60                public function setSpeed ( newSpeed : int )

61                {

 

62                      speed = newSpeed;

 

63                } // end function setSpeed

64

65                // get speed of the Target

 

66                public function getSpeed() : int

67                {

 

68                      return speed;

 

69                } // end function getSpeed

70

71                // set the number of times the Target has been hit

 

72                public function setHitCounter( newCount : int )

73                {

 

74                      hitCounter = newCount;

 

75                } // end setHitCounter function

76

77                // return the number of times the Target has been hit

 

78                public function getHitCounter () : int

79                {

 

80                      return hitCounter;

 

81                } // end function getHitCounter

82

83                // stop moveTargetTimer

 

84                public function stopTimers() : void

85                {

 

86                      moveTargetTimer.stop();

87                }

 

88         } // end class Target

 

89   } // end package

 

Fig. 17.5 | Move target, set direction and speed, and track number of blocks hit.

number of times the Timer should repeat. A value of 0 means that the Timer will run inde-finately. The constructor function (lines 22–27) activates moveTargetTimer, which in turn calls the moveTarget function (lines 30–45) to move the Target every 33 millisec-onds. The moveTarget function contains a nested ifelse statement (lines 34–42) that sets upDown to -1 (up) when the target reaches the bottom of the screen and sets upDown to 1 (down) when it reaches the top of the screen. It does this by testing if the target’s y-coor-dinate is greater than 310 or less than 90. [Note: The property y refers specifically to the y-coordinate of the small white circle that appears on the main stage.] Since the stage is 400 pixels high and the target is 180 pixels high (half of which is below its y-coordinate), when the target’s y-coordinate is equal to 310, the bottom end of the target is even with bottom of the stage. Similar logic applies when the target is at the top of the stage.

Line 44 moves the target by incrementing its y-coordinate by the result of getSpeed() * upDown. The remaining functions in this class are the public get and set functions for the upDown, speed and hitCounter variables. These allow us to retrieve and set the values out-side of the class. The stopTimers function allows us to stop the moveTargetTimer from outside of the class.

 

Now, we can enable the target on stage, target, to move vertically simply by adding the calling methods setSpeed, setUpDown and setHitCounter in the second frame of the Actions layer:

 

target.setSpeed( 8 );

target.setUpDown( -1 );

target.setHitCounter( 0 );

 

Now, test the movie to see the target oscillate between the top and bottom of the stage.

 

Adding the Blocker

An additional moving object is used to block the ball, increasing the game’s difficulty. In-sert a keyframe in the second frame of the Blocker layer and drag an instance of the block-er object from the Library onto the stage. Give this blocker instance the name blocker. Set the blocker instance’s x- and y-coordinates to 415 and 348, respectively. Create a Block-er.as file and class and link it to the blocker symbol. In this file, add the code in Fig. 17.6.

 

 

1    // Fig. 17.6: Blocker.as

 

2    // Set position and speed of Blocker.

 

3    package

4    {

 

5          // import relevant classes

 

6          import flash.display.MovieClip;

 

7          import flash.events.TimerEvent;

 

8          import flash.utils.Timer;

9

10         public class Blocker extends MovieClip

11         {

 

12               var speed : int; // speed of Blocker

 

13               var upDown : int; // direction of Blocker

 

14               var moveBlockerTimer : Timer = new Timer ( 33, 0 );

15          

16               // call function moveBlocker as moveBlockerTimer event handler

 

17               public function Blocker() : void

18               {

 

19                      moveBlockerTimer.addEventListener (

20                            TimerEvent.TIMER, moveBlocker );

 

21                      moveBlockerTimer.start();

 

22               } // end Blocker constructor

23

24               // move the Blocker

 

25               private function moveBlocker( t : TimerEvent )

26               {

 

27                      // if Blocker is at the top or bottom of the stage,

 

28                      // change its direction

 

29                      if ( y > 347.5 )

30                      {

31                            upDown = -1;

 

32                      } // end if

33

34                      else if ( y < 52.5 )

35                      {

36                            upDown = 1;

 

39                      } // end else

40                      y += getSpeed() * upDown;

 

41                } // end function moveBlocker

42          

42                // set speed for the Blocker

 

43                public function setSpeed ( v : int )

44                {

 

45                      speed = v;

 

46                } // end function setSpeed

47

48                // get speed of the Blocker

 

49                public function getSpeed() : int

50                {

 

51                      return speed;

 

52                } // end function getSpeed

53

54                // set direction for the Blocker

 

55                public function setUpDown( newUpDown : int )

56                {

 

57                      upDown = newUpDown;

 

58                } // end function setUpDown

59

60                // get direction of the Blocker

 

61                public function getUpDown() : int

62                {

 

63                      return upDown;

 

64                } // end function getUpDown

65

66                // stop moveBlockerTimer

 

67                public function stopTimers() : void

68                {

 

69                      moveBlockerTimer.stop();

70                }

 

71         } // end class Blocker

 

72   } // end package

 

Fig. 17.6 | Set position and speed of Blocker.

This code is very similar to that of the Target.as. Add the following code in the second frame of the Actions layer to set the speed and direction of the blocker:

 

blocker.setSpeed( 5 ); blocker.setUpDown( 1 );

 

Test the movie. The blocker and target should both oscillate at different speeds (Fig. 17.7).



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.