thanhanlang Co so lap trinh flash AS 30

14 5 0
thanhanlang Co so lap trinh flash AS 30

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lưu ý : khi dùng cách này ta có thể ñưa các ñối tượng vào màn hình làm việc của flash ( như thêm symbol, hình vẽ, chữ ..) nhưng không ñược viết code trong file .fla này , nếu không khi[r]

(1)

CƠ SỞ CỦA LẬP TRÌNH FLASH AS 3.0 VIẾT CODE CHO AS 3.0

Tạo file flash 3.0 ( có fla ): lưu lại

Tạo file ActionScript ( có as để viết code cho package ):

lưu lại

Trong chương trình đơn giản nên lưu file as fla vào chung folder Viết code cho file flash ñơn giản :

Có cách : Cách 1:

- Viết lớp vào file as ( actionScript File) lưu lại với tên tên lớp - Trong file fla dùng lớp ta đặt tên lớp vào mục Document class

- Bấm Ctrl+Enter ñể chạy thử Sau chạy rhwr folder chứa file xuất thêm file swf

Lưu ý : dùng cách ta đưa đối tượng vào hình làm việc flash ( thêm symbol, hình vẽ, chữ ) khơng viết code file fla , không biên dịch bị lỗi.Chỉ ñược phép viết code file as

Cách 2: Mở trang viết code cách ấn F9 hay chọn Khi trang viết code mở ta kích vào Script Assist để đóng trợ giúp dễ dang chỉnh sửa code viết ( để Script Assist khơng viết code trực tiếp dĩ nhiên khó sửa code hơn) Sau ta nhập code vào ( viết code trực tiếp AS 2.0 )

(2)

Trong file fla ñưa tên class sau:

(3)

Cách :

Viết code trực tiếp :

Xử lý kiện :

Cấu trúc xử lý kiện ñối tượng thiết lập addEventListener vào kiện Cú pháp sau:

(ñối tượng).addEventListener( loại kiện , tên hàm xử lý kiện ) Trong :

Loại kiện : kiện tác ñọng lên ñối tượng kích chuột ,nhả chuột , gõ phím , kiện frame …Mỗi kiện ñều ñược ñưa vào tham số dạng String mô tả kiên , ví dụ :MouseEvent.CLICK, MouseEvent.MOUSE_UP, Event.ENTER_FRAME,

Event.ADDED_TO_STAGE, KeyboardEvent.KEY_DOWN …

Tên hàm xử lý kiện : tên hàm ñược gọi kiện xảy Các hàm kiện có tham số kiện

(4)

import flash.display.Sprite; import flash.events.Event; graphics.lineStyle(1, 0, 1);

addEventListener(Event.ENTER_FRAME, onEnterFrame); function onEnterFrame(event:Event):void {

graphics.lineTo(Math.random( ) * 400, Math.random( ) * 400);

}

Sử dụng lối viết theo package ta ñược : package {

import flash.display.Sprite; import flash.events.Event;

public class ExampleApplication extends Sprite {

public function ExampleApplication( ) { graphics.lineStyle(1, 0, 1);

addEventListener(Event.ENTER_FRAME, onEnterFrame); }

private function onEnterFrame(event:Event):void { graphics.lineTo(Math.random( ) * 400,

Math.random( ) * 400); }

} }

Sau ví dụ số kiện keyboard Khi chạy chương trình ta kích vào hình vng màu xanh gõ phím Trên hình output xuất số thơng tin

Code viết dạng 1: package {

import flash.display.Sprite;

import flash.display.DisplayObject; import flash.events.*;

public class KeyboardEventExample extends Sprite { private var child:Sprite = new Sprite();

private var bgColor:uint = 0x00CCFF; private var size:uint = 80;

public function KeyboardEventExample() { child.graphics.beginFill(bgColor);

child.graphics.drawRect(0, 0, size, size); child.graphics.endFill();

(5)

child.addEventListener(MouseEvent.CLICK, clickHandler);

child.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

}

private function clickHandler(event:MouseEvent):void { stage.focus = child;

}

private function

keyDownHandler(event:KeyboardEvent):void {

trace("keyDownHandler: " + event.keyCode); trace("ctrlKey: " + event.ctrlKey);

trace("keyLocation: " + event.keyLocation); trace("shiftKey: " + event.shiftKey);

trace("altKey: " + event.altKey); }

}

}

Code viết theo cách :

import flash.display.Sprite;

import flash.display.DisplayObject; import flash.events.*;

var child:Sprite = new Sprite(); var bgColor:uint = 0x00CCFF;

var size:uint = 80;

child.graphics.beginFill(bgColor);

child.graphics.drawRect(0, 0, size, size); child.graphics.endFill();

addChild(child);

child.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

child.addEventListener(MouseEvent.CLICK, clickHandler); function keyDownHandler(event:KeyboardEvent):void { trace("keyDownHandler: " + event.keyCode); trace("ctrlKey: " + event.ctrlKey);

(6)

trace("shiftKey: " + event.shiftKey); trace("altKey: " + event.altKey); }

function clickHandler(event:MouseEvent):void { stage.focus = child;

}

Kết chạy chương trình hình Output:

Sử dụng phép toán :

Flash cho phép sử dụng phép tốn : =,+,-,*,/, +=,-=,*=,/=,++, ,==,… ngơn ngữ lập trình khác để xử lý phép tốn , phép gán ðể dùng hàm toán học, số, ta sử dụng cú pháp : Math.(tenham(các đơi_số)) , Math.(ten số) Ví dụ:

(7)

hay :

quantity += 6;

quantity = quantity / factor;hay: quantity /= factor;

quantity++; nghĩa : quantity = quantity + hay quantity += 1;

quantity =Math.sin(1.234); quantity=Math.PI;

trace(5 == 6); // kết : false trace(6 == 6); // kết : true trace(6 == "6"); // kết : true trace(5 == "6"); // kết : false trace(5 != 6); // kết : true trace(6 != 6); // kết : false trace(6 != "6"); // kết : false trace(5 != "6"); // kết : true

var quantity:int = 5; if (quantity ==6) { trace("Chinh xac !"); }

trace("quantity bang " + quantity); // ket qua: quantity bang

var quantity:int = 5; if (quantity is int) {

trace("loai du lieu cua quantity la integer."); }

trace(5 < 6); // kết : true trace(5 > 5); // kết : false Các cấu trúc ñiều kiện :

Các cấu trúc điều kiện AS 3.0 hồn tồn giống cấu trúc ñiều kiện AS 2.0 ñại ña số ngơn ngữ khác Các ví dụ sau minh hoạ cho điều

if (animalName == "rua") {

trace("rua la dong vat di chuyen rat cham"); }

(8)

if (animalName == "turtle") { trace("Ban da tra loi Dung"); }

else {

trace("Ban da tra loi Sai."); }

-

if (animalName == "turtle") {

trace ("Ban da tra loi Dung."); }

else if (animalName == "dove") {

showMessage ("dove cung khong phai."); }

-

if (animalName == "turtle") { race ("Dung.");

}

else if (animalName == "dove") {

trace ("Sai."); }

else {

trace("thu lai "); }

-

var animalName:String = "turtle"; switch (animalName) {

case "turtle": trace("Dung."); case "dove":

trace("Sai."); default:

trace("lap lai."); }

-

(9)

if (current.getDate( ) == 17 && current.getMonth( ) == 3) { trace ("Happy Birthday, Bruce!");

}

if (!((userName == "ABC") || (userName == "CDE"))) {

trace ("Xin loi chi co ABC hay CDE moi co quyen truy cap."); }

Các cấu trúc lặp :

Do tương tự cấu trúc lặp ngôn ngữ khác nên khơng nêu lại cú pháp

for (var i:int = 0; i < 10; i++) { // Display the value of i

TRace(i); }

-

for (var i:int = 1; i <= 3; i++) { for (var j:int = 1; j <= 2; j++) {

trace(i + " X " + j + " = " + (i * j)); }

}

for (var i:int = 1; i <= 3; i++) { for (var j:int = 1; j <= 3; j++) { for (var k:int = 1; k <= 3; k++) {

trace(i + " X " + j + " X " + k + " = " + (i * j * k)); }

} } -

for (var i:int = 10; i > 0; i ) { trace(i);

}

-

for (var i:Number = 50000; i > 2; i = Math.sqrt(i)) { trace(i);

(10)

var myObject:Object = {firstName:"Tara", age:27, city:"San Francisco"};

for (var prop in myObject) {

trace("myObject."+prop+" = "+myObject[prop]); }

-

var myObject:Object = {firstName:"Tara", age:27, city:"San Francisco"};

for each (var item in myObject) { trace(item);

} -

var i:Number = 0; while (i < 20) { trace(i); i += 3; }

-

var myVar:Number = 0; {

trace(myVar); myVar++;

}

while (myVar < 5); -

with (someOther_mc) { _x = 50;

_y = 100;

gotoAndStop(3); }

Cấu trúc try … catch

import flash.display.Sprite; var spr:Sprite = new Sprite(); try {

spr.addChild(spr); }

catch (e:ArgumentError) {

trace (e); // ArgumentError: Error #2024: An object may not be added as a child of itself

}

- try catch finally

private function displayMessage(message:String):void { try {

(11)

throw new Error("The message is undefined."); }

trace(message); }

catch (errObject:Error) { trace(errObject.message); return;

}

finally {

// This runs, even if an error is caught trace("This is the last line displayed."); }

}

- ném lỗi

private function displayMessage(message:String):void { try {

if(message == undefined) {

throw new Error("The message is undefined."); }

trace(message); }

catch (errObject:Error) { trace(errObject.message); return;

}

// This line won't run if an error is caught trace("This is the last line displayed."); }

- Timer : Thực action sau khoảng thời gian Dùng lớp Timer

var timer:Timer = new Timer(delay, repeatCount); delay : thời gian dừng ( miligiay)

repeatCount : số lần lặp

ví dụ : cho xuất khoảng thời gian từ lần gọi hàm onTimer trước đến lần gọi sau ( tính miligiây), khoảng thời gian xấp xỉ 500 miligiay ta đặt thời gian chờ 500miligiay package {

import flash.display.Sprite; import flash.events.TimerEvent; import flash.utils.Timer;

(12)

public class ExampleApplication extends Sprite {

// Declare and initialize a variable to store the value

// of the previous timer reading private var _PreviousTime:Number = 0;

public function ExampleApplication( ) { var tTimer:Timer = new Timer(500, 10); tTimer.addEventListener(TimerEvent.TIMER, onTimer);

tTimer.start( ); }

private function onTimer(event:TimerEvent):void {

// Output the difference between the current timer value and

// its value from the last time the function was called

trace(flash.utils.getTimer( ) - _PreviousTime); _PreviousTime = flash.utils.getTimer( );

} } }

Ví dụ sau cho hình : trịn hình chữ nhật di chuyển sau khoảng thời gian delay 0,05s (hình chữ nhật ) 0,1s ( đường trịn ):

package {

import flash.display.Sprite; import flash.events.TimerEvent; import flash.utils.Timer;

public class ExampleApplication extends Sprite { private var _square:Sprite;

private var _circle:Sprite;

public function ExampleApplication( ) {

// Create the two sprites and draw their shapes _square = new Sprite( );

_square.graphics.beginFill(0xff0000);

_square.graphics.drawRect(0, 0, 100, 100); _square.graphics.endFill( );

(13)

_circle = new Sprite( );

_circle.graphics.beginFill(0x0000ff); _circle.graphics.drawCircle(50, 50, 50); _circle.graphics.endFill( );

addChild(_circle); _circle.x = 100; _circle.y = 200;

// Create the two timers and start them var squareTimer:Timer = new Timer(50, 0);

squareTimer.addEventListener(TimerEvent.TIMER, onSquareTimer);

squareTimer.start( );

var circleTimer:Timer = new Timer(100, 0); circleTimer.addEventListener(TimerEvent.TIMER, onCircleTimer);

circleTimer.start( ); }

// Define the two handler methods

private function onSquareTimer(event:TimerEvent):void {

_square.x++; }

private function onCircleTimer(event:TimerEvent):void {

_circle.x++; }

(14)

Ngày đăng: 01/05/2021, 13:31

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan