Learn about the snow Effect of Flash (59) every day

Source: Internet
Author: User

On this day, I went hiking by myself, and the National Day was a bit stuffy. However, mountain climbing will make people feel relaxed and enjoy the happy time to communicate with nature without pressure. In the evening, I wrote a snow effect. Although this effect has been very backward, but when I was studying flash, this programming helped me understand a lot.

 

Bored, suddenly remembered the joy of Zhimo's Snowflake. But accidentally starts the story of the memories. Xu Zhimo, Lin Huiyin, and Liang sicheng Jin Yuelin.

Well, I don't have to talk about it.

 

Snowflake production principle:

Create a video clip snow, which is the key Snowflake. When making a ball, use a gray ball and then blur it with a filter to change the brightness. The timer allows the snowflake to generate different time intervals, and the snowflake will be deleted automatically when it falls down to a height. Otherwise, the memory overhead will be high and the card will be generated.

Next, you need to prepare a background image. It is better to have a relationship with snow.

After completing the preparation, you can start writing code.

 

Design document class: Main. As, and Snow. As can complete the basic work.

Snow writing

 

 

Package COM. effect <br/>{</P> <p> Import flash. display. movieclip; <br/> Import flash. events. *; <br/> Public Class Snow extends movieclip <br/> {<br/> public function snow () <br/>{</P> <p >}< br/>}

 

Package <br/> {</P> <p> Import flash. display. movieclip; <br/> Import flash. events. *; <br/> Import flash. utils. timer; <br/> Import flash. display. stage; <br/> Import flash. display. displayobject; <br/> Import COM. effect. snow; <br/> public class main extends movieclip <br/> {<br/> private var time: timer; <br/> private var speed: Int = 5; <br/> private var contain: movieclip = new movieclip (); // store the snowflake container </P> <p> public function main () <br/>{< br/> Init (); <br/>}< br/> // initialization timer <br/> private function Init (): void <br/>{< br/> addchild (contain); <br/> time = new timer (200); <br/> time. addeventlistener (timerevent. timer, ontimer); <br/> time. start (); <br/>}< br/> private function ontimer (Event: timerevent): void <br/>{< br/> creatsnow (); <br/> creatsnow (); <br/>}</P> <p> // copy snowflake <br/> private function creatsnow (): void <br/>{< br/> var mysnow: snow = new snow (); <br/> contain. addchild (mysnow); <br/> mysnow. y = 0; <br/> mysnow. X = math. random () * stage. stagewidth; <br/> mysnow. scalex = mysnow. scaley = math. random () + 0.1; <br/> mysnow. addeventlistener (event. enter_frame, run); <br/>}< br/> private function run (Event: Event): void <br/>{< br/> var MC: movieclip = movieclip (event. currenttarget); <br/> MC. Y + = speed; <br/> MC. rotation = math. random () * 360; <br/> If (MC. y> 650) <br/>{< br/> contain. removechild (MC); // Delete the MC in the memory <br/> MC. removeeventlistener (event. enter_frame, run); // deletion event <br/> MC = NULL; <br/>}< br/>}

 

Time timer usage:

Key programs mainly use counters to produce an object. Timer generates an object every 200 milliseconds, which produces a downward running effect.

Construct a timer when using the init () method.

 

 

Object motion

The snow object we created needs to be run, so we adopt event in a simple way. for event listening such as enter, we can call this event at every frame.

In this way, when we change the Y axis, a displacement change can be generated. In the process of dropping, changing the angle will produce different results.

 

 

Delete object 

It is not difficult to delete a node. However, when using the removechild method, pay attention to the node relationship.

Private var contain: movieclip = new movieclip (); // the snowflake container is used to store snow objects,

Contain. addchilc (mysnow );

 

In this way, we can easily know the relationship between the node parent and child. As a parent node, contain can only delete nodes in its container.

Contain. removechild (MC); // deletes the MC in the memory.

 

Similarly, delete event listening is required for deleted objects. When the event. enter_frame event is not used, delete the event. Otherwise, an error occurs.

MC. removeeventlistener (event. enter_frame, run); // delete an event

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.