PDA

View Full Version : Rubik-Game và lập trình giả lập


quanthien84
10-04-2008, 05:43 PM
http://diendan02x1a.net/rubik.swf
Đã thất hẹn với lớp thật lâu.

Dựa trên việc phân tích sản phẩm, Th sẽ đưa những khái niệm về lập trình thật đơn giản như bản chất của nó .
Hiện tại, AS là ngôn ngữ Th tiếp xúc thường xuyên.

Như các bạn đã biết đến môn hình họa (Mr. Thọ dạy). Các bạn thể hiện một mô hình không gian dựa trên 3 mặt cắt, sản phẩm cuối cùng là một mô hình 3 chiều và một kết quả ưng ý. Bây giờ, với kiến thức tưởng lãng quên ấy, bạn phát triển.


Bỏ qua những lớp thượng tầng, ta bắt tay vào lập trình một chương trình ứng dụng-Rubik game trên, là một ví dụ.

1.Điểm nhìn

(còn tiếp)

quanthien84
10-05-2008, 05:36 PM
class ViewPoint
public function ViewPoint()
}
public static function initialize(rotX:Number, rotY:Number, rotZ:Number):Void
_rotationX = rotX;
_rotationY = rotY;
_rotationZ = rotZ;
}
public static function get rotationX():Number
return _rotationX;
}
public static function get rotationY():Number
return _rotationY;
}
public static function get rotationZ():Number
return _rotationZ;
}
public static function set rotationX(rot:Number):Void
_rotationX=rot;
}
public static function set rotationY(rot:Number):Void
_rotationY=rot;
}
public static function set rotationZ(rot:Number):Void
_rotationZ=rot;
}
private static var _rotationX:Number;
private static var _rotationY:Number;
private static var _rotationZ:Number;
}
private, public như tên gọi của nó, là namespaces, khai báo vùng sử dụng hàm hoặc biến được định nghĩa.
các biến là các thuộc tính điểm nhìn so với tọa độ thực. rotationx, rotationy,rotationz giá trị góc xoay theo trục tọa độ thực.
(còn tiếp)
(cố lên, không thể để chìm được, sao 1 mình 1 ngựa vầy ta? :(()

darthtuan
10-05-2008, 09:52 PM
Hay lắm Thiện. Bữa nay trình độ T lên cao quá nhỉ. Cố lên

hieu171ce
10-07-2008, 11:20 AM
Cha cha, không ngờ X1A chúng ta lại có một tài năng tin học đó. Cố gắng trau dồi để góp công góp sức cho một nền tin học nước nhà còn nhiều hạn chế nhé.

quanthien84
10-10-2008, 10:47 AM
Bây giờ đến phần lõi của chương trình. Mình tập trung vào các lớp chuyển hệ trục 3 chiều thành 2 chiều theo phương pháp họa hình

import flash.geom.Point;// gọi dùng lại lớp Point
class core.Number2D extends Point
public function Number2D(xas:Number, yas:Number)
super(xas,yas);
}
public function clone():Number2D
return new Number2D(x, y);
}
}



import core.Number2D;
class core.Number3D extends Object

public var x:Number;
public var y:Number;
public var z:Number;
public function Number3D (xas:Number, yas:Number, zas:Number)

super ();
x = xas;
y = yas;
z = zas;
}
public function to2D (simtype:String):Number2D//chuyển trục

var num2D:Number2D = new Number2D ();
var clone3D:Number3D = clone ();

if (simtype == "Rubik")


/*
clone3D.rotZ (ViewPoint.rotationZ);
clone3D.rotX (ViewPoint.rotationX);
clone3D.rotY (ViewPoint.rotationY);
*/
//uncomment khi thêm góc nhìn

var view:Number = Math.PI / 6;
num2D.x = clone3D.x * Math.cos (view) - clone3D.y * Math.cos (view);
num2D.y = clone3D.z - y * Math.sin (view) - clone3D.x * Math.sin (view);
}
return num2D;
}
public function rotZ (angle:Number):Void

var rad:Number = Math.sqrt (x * x + y * y);
var curag:Number = Math.atan (y / x);//return radians
if (curag != NaN && rad != 0)

if (x < 0)

curag = curag + Math.PI;
}
var nextangle:Number = curag + angle * Math.PI / 180;
x = rad * Math.cos (nextangle);
y = rad * Math.sin (nextangle);
}
}
public function rotX (angle:Number):Void

var rad:Number = Math.sqrt (z * z + y * y);
var curag:Number = Math.atan (y / z);//return radians
if (curag != NaN && rad != 0)

if (z < 0)

curag = curag + Math.PI;
}
var nextangle:Number = curag + angle * Math.PI / 180;
z = rad * Math.cos (nextangle);
y = rad * Math.sin (nextangle);
}
}
public function rotY (angle:Number):Void

var rad:Number = Math.sqrt (z * z + x * x);
var curag:Number = Math.atan (z / x);//return radians
if (curag != NaN && rad != 0)

if (x < 0)

curag = curag + Math.PI;
}
var nextangle:Number = curag + angle * Math.PI / 180;
z = rad * Math.sin (nextangle);
x = rad * Math.cos (nextangle);
}
}
public function toDepth (simtype:String):Number

if (simtype == "Rubik")

return (x + y + z);
}
else

return 0;
}
}
public function clone ():Number3D

return new Number3D (x, y, z);
}
public function get getX ():Number

return x;
}
public function get getY ():Number

return y;
}
public function get getZ ():Number

return z;
}
public function toString ():String

return Math.round (x * 10) + ":" + Math.round (y * 10) + ":" + Math.round (z * 10);
}
}

quanthien84
11-14-2008, 01:43 PM
Namespaces

Namespaces give you control over the visibility of the properties and methods that you create. Think of the public,
private, protected, and internal access control specifiers as built-in namespaces. If these predefined access
control specifiers do not suit your needs, you can create your own namespaces.
If you are familiar with XML namespaces, much of this discussion will not be new to you, although the syntax and
details of the ActionScript implementation are slightly different from those of XML. If you have never worked with
namespaces before, the concept itself is straightforward, but the implementation has specific terminology that you
will need to learn.
To understand how namespaces work, it helps to know that the name of a property or method always contains two
parts: an identifier and a namespace. The identifier is what you generally think of as a name. For example, the identifiers
in the following class definition are sampleGreeting and sampleFunction():
class SampleCode

var sampleGreeting:String;
function sampleFunction ()
trace(sampleGreeting + " from sampleFunction()");
}
}


Namespaces mang đến cho bạn khả năng điều khiển khả năng nhận biết (visibility) của các thuộc tính và phương thức do bạn tạo.
Nghĩ về các từ khóa điều khiền truy xuất (access control specifiers) public, private, protected, và internal như các namespaces có sẵn.
Nếu như những từ khóa điều khiển truy xuất được định nghĩa trước này không đáp ứng được nhu cầu bạn, bạn có thể tạo các namespaces cho chính bạn.

Nếu bạn quen thuộc với XML namespaces, phần nhiều sự tháo luận này sẽ không mới với bạn, mặc dù các mã tự (syntax) và chi tiết của
sự thực hiện ngôn ngữ ActionScript có một ít khác biệt so với những điều này trong XML. Nếu bạn đã chưa bao giờ làm việc với namespaces trước đây, chính khái niệm chính nó đã dễ hiểu ,nhưng sự thực thi có kỹ thuật đặc trưng mà bạn sẽ cần phải học.

Để hiểu namespaces làm việc ra sao, rất có ích khi biết rằng tên của một thuộc tính hay phương thức luôn chứa hai phần: biệt danh (identifier) và namespace.
Biệt danh là thứ mà bạn thường nghĩ như tên gọi. Ví dụ, các biệt danh trong định nghĩa lớp dưới đây gọi là sampleGreeting và sampleFunction():
class SampleCode

var sampleGreeting:String;
function sampleFunction ()
trace(sampleGreeting + " from sampleFunction()");
}
}


Whenever definitions are not preceded by a namespace attribute, their names are qualified by the default internal
namespace, which means they are visible only to callers in the same package. If the compiler is set to strict mode, the
compiler issues a warning that the internal namespace applies to any identifier without a namespace attribute. To
ensure that an identifier is available everywhere, you must specifically precede the identifier name with the public
attribute. In the previous example code, both sampleGreeting and sampleFunction() have a namespace value of
internal.

Bất cứ khi nào sự định nghĩa không được bắt đầu bởi thuộc tính namespaces, tên gọi của chúng được xác nhận bởi namespace
internal mặc định, điều đó nghĩa là chúng được nhận thấy chỉ khi gọi trong cùng package. Nếu chương trình biên dịch được thiết lập đến chế độ kiểm tra nghiêm ngặt (strict mode), chương trình biên dịch sẽ phân phối một thông báo rằng namespace internal gán vào bất kì biệt danh nào không có thuộc tính namespace. Để chắc chắn rằng một biệt danh có sẵn bất kỳ đâu, bạn phải đặt trước rõ ràng biệt danh với thuộc tính public. Trong đoạn mã ví dụ trước, cả sampleGreeting và sampleFunction() có namesace giá trị thành internal.


There are three basic steps to follow when using namespaces. First, you must define the namespace using the
namespace keyword. For example, the following code defines the version1 namespace:
namespace version1;
Second, you apply your namespace by using it instead of an access control specifier in a property or method declaration.
The following example places a function named myFunction() into the version1 namespace:
version1 function myFunction() }
Third, once you’ve applied the namespace, you can reference it with the use directive or by qualifying the name of
an identifier with a namespace. The following example references the myFunction() function through the use
directive:
use namespace version1;
myFunction();
You can also use a qualified name to reference the myFunction() function, as the following example shows:
version1::myFunction();

Có 3 bước cơ bản để theo khi sử dụng các namespace. Đầu tiên, bạn phải định nghĩa namespace bằng cách sử dụng từ khóa namespace.
Ví dụ, đoạn mã dưới định nghĩa namespace version1:
namespace version1;
Bước thứ hai, bạn gán namespace của bạn bằng cách sử dụng nó thế chỗ cho từ khóa điều khiền truy xuất trong sự khai báo thuộc tính hoặc
phương thức. Ví dụ đưới đặt một hàm được đặt tên là myFunction() vào namespace version1
Bước thứ ba, mỗi lần bạn dán namespace xong, bạn có thể tham chiếu nó bằng cách sử dụng trực tiếp hoặc bằng xác nhận tên của biệt danh với namespace. Ví dụ dưới đây tham chiếu hàm myFunction() thông qua cách sử dụng trực tiếp
use namespace version1;
myFunction();
Bạn còn có thể sử dụng tên gọi được xác nhận để tham chiếu đến hàm myFunction(), như ví dụ dưới đây chứng minh:
version1::myFunction();

-----
(còn tiếp)

(dịch từ Adobe Developer Guide)

hieu171ce
11-15-2008, 04:36 PM
Rất hoan nghênh Thiện mang lên diễn đàn những thứ thú vị. Nhưng theo Hiếu Th nên tạp trung vào mảng XD+PM ứng dụng cơ.Th vừa có khả năng tin học, lại là một KS XD nên phải cống hiến chứ. he he