Flash as 3.0 Novice Learning to fly Tutorial: class writing does not use library components

Source: Internet
Author: User
Tags addchild

Writing AS3.0 classes (without using library components)

In the last lecture we used a movie clip that was created. And in the library to do the type of link, which has a complex graphic creation is a better choice, if you can skillfully use the drawing API to draw any graphics you want, you can not use the library components, directly in the class to write. Here's how we write the class code in this way:

Create a Documentclass class (create 100 randomly placed circles)

1.代码:
2.

3.package {
4.
5.import flash.display.MovieClip;
6.public class DocumentClass extends MovieClip {
7.// 属性
8.private var _circle:Drag_circle;
9.private const maxBalls:int = 100;
10.// 构造函数
11.public function DocumentClass() {
12.
13.var i:int;
14.// 循环创建小球
15.for(i = 0; i<= maxBalls; i++) {
16.// 创建可拖动小球的实例
17._circle = new Drag_circle();
18.// 设置小球实例的一些属性
19._circle.scaleY = _circle.scaleX = Math.random();
20.// 场景中的x,y位置
21._circle.x = Math.round(Math.random() *(stage.stageWidth - _circle.width));
22._circle.y = Math.round(Math.random() *(stage.stageHeight - _circle.height));
23.// 在场景上显示
24.addChild(_circle);
25.}
26.}
27.}
28.}

Drag_circle class (Draw a red circle with drag-and-drop function)

1. Code:
2.

3.package {
4.
5.import Flash.display.Sprite;
6.import Flash.display.Shape;
7.import flash.events.MouseEvent;
8.

9.public class Drag_circle extends Sprite {
10.
11.private var _circle:sprite;
12.
13.public function drag_circle () {
14.
15._circle = new Sprite ();
16._circle.graphics.beginfill (0xff0000);
17._circle.graphics.drawcircle (0, 0, 10);
18._circle.graphics.endfill ();
19._circle.buttonmode = true;
20.addChild (_circle);
21st.
22.
23._circle.addeventlistener (Mouseevent.click,onclick);
24._circle.addeventlistener (Mouseevent.mouse_down,ondown);
25._circle.addeventlistener (Mouseevent.mouse_up,onup);
26.
27.}
28.
29.
30.private function OnClick (event:mouseevent): void {
31.trace ("Circle clicked");
32.}
33.

34.private function Ondown (event:mouseevent): void {
35._circle.startdrag ();
36.}
37.

38.private function Onup (event:mouseevent): void {
39._circle.stopdrag ();
40.}
41.}
42.}

Create a new FLA file and save it in the same directory as the document.as and drag_class.as classes. Note: Unlike the last-spoken component class, it is no longer necessary to have anything in the scene, as we have dynamically added and displayed CIRCLE_MC in the main class documentclass.as. Enter the class name Documentclass in the document class input box in the property panel, and you can test it. (You can try to add random or gradient colors to the ball)

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.