note

858Currently online
81Today's Reading
43Share Today
Multilingual display

[AS3 Programming Instruction] Dropped Balls (3)

2018-05-01 11:12:00

We can compare, the last article of this tutorial was written in December last year, today has been in June 2013, the reason for not continuing to write in these six months is not that the author resigned to go back to his hometown and found that he could not stay and ran to work, definitely not these bumps and trifles. On the contrary, the apprentice did not want to continue to learn after learning the second chapter, and as expected, this person was a person who gave up halfway and did not have strong willpower. To tell the truth, since the sophomore year, there have been people who come to me unilaterally and forcibly call me master, and now I have graduated and worked for four years, why there has not been a single apprentice in these six or seven years, which is really a question that needs to be thought about. Without much to say, this section begins to drop the ball this simple physical phenomenon with a program to achieve, to the end you will find that only the program to achieve the effect is the most real, because the program is rational, with the code written out is scientifically rigorous; And to do it with animation is emotional and not realistic enough. Goal: We let the ball drop to the ground at uniform speed, mainly learn the performance of uniform motion.

Tools/Materials

FlashCS5.5

Methods/Steps
1

Open FlashCS5.5, a file called "Dropped Ball 3.fla", draw a line under the stage as the ground, and save it somewhere on your computer. I don't want to say any more. We're not kids. There was once a man who did not save things, did 7 hours of animation and lost it... Doing it all over again takes time and is boring.

2

Draw a small circle on the stage, press F8, turn it into a movie clip, name it "ball", then go back to the stage and select the ball, the properties panel is named "mcBall".

3

Let's get this straight. We need to make a ball drop from a height and then stop. So what we're going to do here is we're going to use the code to put the ball somewhere up there. To do this, we press F9 and enter the following code in the first frame: // The height of the ball is 50, horizontal coordinate on our stage. mcBall.x = Math.random()*stage.stageWidth; mcBall.y = 50;

4

This step is going to start falling. You should know that dropping is a continuous process, which is manifested on the stage as the ball's y coordinate keeps increasing (the top left corner of the stage coordinate is the smallest (0,0) point), so we need a process that continuously calls a method to constantly change the ball's y position. There are four or five ways I can do this in my head, and I'll mention the most common one today: the ENTER_FRAME event. stage.addEventListener(Event.ENTER_FRAME,downBall); function downBall(e:Event):void{         mcBall.y+=5; }

5

To explain the code above, an ENTER_FRAME event is an event that continuously calls a method at a frame rate. The frame rate used in this tutorial is 24 frames, which means that the listener is called 24 times per second. What are we doing at such a high frequency? By moving the ball down 5 pixels at a time.

6

Test it and see if the ball has started to fall! It's just constant velocity. And the ball is going to fall indefinitely, even though you can't see it, but the ball is going to keep falling, and that's not necessary because you can't see things that you can't do, so let's make a judgment, when the ball hits the ground level stop doing the falling method. stage.addEventListener(Event.ENTER_FRAME,downBall); function downBall(e:Event):void{         mcBall.y+=5;                 // Determine whether it reaches the ground in real time, My ground is at the height of 367. if(mcBall.y+mcBall.height>=367) {mcBall.y= 367-mcball. height;                   stage.removeEventListener(Event.ENTER_FRAME,downBall);         }}

7

That's the end of this tutorial, and in the next one we'll talk about real accelerated drops.

Matters needing attention
1

This tutorial is an entry-level coding exercise, and the aspects of program optimization and efficiency and rationality will not be discussed until later.

2

If you don't get it right, try again.

Recommendation