วันศุกร์ที่ 26 กรกฎาคม พ.ศ. 2556

action script 3.0 ทำโปรแกรมคำนวณหาค่าร้อยละ

1 . สร้าง text box 3 อัน
percent1 ใส่ผลลัพท์ ตั้งเป็น dynamic text
num1 ใส่จำนวนเศษที่จะหา ตั้งเป็น input text
full1 ใส่ค่าจำนวนเต็ม ตั้งเป็น input text

2.สร้างปุ่ม ตั้ง ชื่อว่า btn1 (วาดรูปแล้วเมาส์คลุมกด f8)
*ทั้ง textbox และ ปุ่มตั้งชื่อใน instant name เหมือนกันด้วยนะครับ
3.เพิ่ม layer code แล้วใส่ code action script ดังนี้

//code start
btn1.addEventListener(MouseEvent.CLICK,atk);//ปุ่ม btn1 เมื่อคลิ๊ก CLICK แล้วจะไปเรียก function atk()

function atk(MouseEvent)
{
       percent1.text = String((int(num1.text) / int(full1.text))*100);//คำนวนแล้วแสดง percent1
       
}
//code end

ฟังก์ชั้น String() คือแปลงเป็น ตัวอักษร String
ฟังก์ชั้น Int() คือแปลงค่าเป็น ตัวเลข Integer

วันจันทร์ที่ 22 กรกฎาคม พ.ศ. 2556

action script 2.0 กับ เกมปิงปอง

แบ่ง เป็น 3 ส่วนนะครับ
1 ไม้ปิงปองของเรา ควบคุมด้วย keyboard
2 ลูกปิงปองเด้งไปมา
3 ไม้ปิงปองของคอมตีแข่งกับเรา

1 code ไม้ของเรา
on(keyPress "<Left>" ){
this._x-=10
}
on(keyPress"<Right>"){
this._x+=10
}

2 code ไม้ของคอม

onClipEvent(load){
speed=10;
}
onClipEvent(enterFrame){
_root.p2._x+=speed;
if(_root.p2._x>550){
speed=(speed*-1)
}
if(_root.p2._x<20){
speed=(speed*-1)
}
}

3 code ของลูกปิงปอง

onClipEvent(load){
speed=10;
speedy=20;

}
onClipEvent(enterFrame){
_root.ball._x+=speed;
_root.ball._y+=speedy;  //บอลวิ่งเฉียงๆ

if(_root.ball._x>500){ //ชนเด้งกลับ
speed=-10;
}
if(_root.ball._x<20){//ชนเด้งกลับ
speed=10;
}

if(_root.ball._y>380){//ชนเด้งกลับ
speedy=-10;
}
if(_root.ball._y<20){//ชนเด้งกลับ
speedy=10;
}
if(_root.ball.hitTest(_root.p1)){//ชนกับไม้ของเรา หรือ mc ชื่อ p1
speedy=-10;
}
if(_root.ball.hitTest(_root.p2)){//ชนกับไม้ของคอม หรือ p2
speedy=10;
}
}
//end
ส่วนคะแนน ให้ตั้งเป็น textbox แล้วกำหนค่าไปคงไม่ยากเกินไปนะครับ
อาจจะเพิ่มให้ชนกำแพงด้านล่างแล้ว gameover ต่อไป

action script 2.0 บอลเด้งๆ

ac 2.0 นี้เขียน code ใส่ movieclip ครับ


//code start
onClipEvent(load){//เมื่อ movieclip นี้เกิดขึ้นมา
speed=10;//กำหนดความเร็วแกน x
speedy=20;//กำหนดความเร็วแกน y
}

onClipEvent(enterFrame){
_root.ball._x+=speed;//ลูกบอลวิ่งเฉียง
_root.ball._y+=speedy;
//ชนขวาเด้งกลับ
if(_root.ball._x>500){
speed=-10;
} //ชนซ้ายเด้งกลับ
if(_root.ball._x<20){
speed=10;
}
//ชนล่างเด้งขึ้น
if(_root.ball._y>380){
speedy=-10;
}//ชนเพดานเด้งลง
if(_root.ball._y<20){
speedy=10;
}
}
//end

code นี้เคยใช้สอนนักเรียนชั้น ม.3 ในการเรียนพื้นฐานโปรแกรมมิ่งครับ

action script 3.0 การใช้ keyboard และ Hittest

เหมือนเดิมครับ แยก layer code กับ graphic ไว้ 2 layer
แล้วเขียน code ใน frame ของ layer as

movieclip ชื่อ mc1 ครับ
stage คือ ฉากรวมทั้งหมด

//code start
import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyPressed);//stage เตรียมรับค่าจาก kb

function onKeyPressed(KeyboardEvent)
{

switch (KeyboardEvent.keyCode)
{
case Keyboard.LEFT:
mc1.x-=10;
break;
case Keyboard.RIGHT:
mc1.x+=10;
break;
case 65: //a
mc1.x-=10;
break;
case 68: //d
mc1.x+=10;
break;
case 87: //w
mc1.y-=10;
break;
case 83: //s
mc1.y+=10;
break;
}
}
//code end


ตัวอย่างที่สอง Hittest หรือการตรวจจับการชน(คนละไฟล์กันนะ)
เป็นจุดเด่นของ โปรแกรม Flash ในการเขียนเกมส์หรือสื่อออนไลน์เลยละ
มี mc 2 ตัวนะรับ mc1 กับ mc2
mc2 ให้ทำ tween มาจะชน mc1

//code start
mc1.addEventListener(Event.ENTER_FRAME, circleHit);

function circleHit(event:Event):void {
if (mc1.hitTestObject(mc2)) { //ตรวจสอบการชน
trace("ahh!");
mc1.visible= false; //mc1 หายตัวไป
}
}
//code end

action script 3.0 กับ helloworld

1.สร้าง movieclip ชื่อว่า btn1 กด f8 แปลงเป็น button ซะ
2.สร้าง layer บน timeline ใหม่่ ตั้งชื่อว่า as แล้วเขียน code ใน frame
(เพื่อแยก layer ระหว่าง code กับ กราฟฟิก) 

//code btn1.addEventListener(MouseEvent.CLICK,atk);//ปุ่ม btn1 เื่เมื่อclick แล้วจะเรียกใช้ฟังชั้น atk 


function atk(MouseEvent)//ฟังชั่น atk
{

 var hello:String = "hello world"; //ประกาศตัวแปร แล้วใส่ text ว่า helloworld
 result1.text=""+hello+""; //
output trace(hello);//trace ดูต่าตัวแปรว่าถูกต้องไหม บรรทัดนี้ไม่ใส่ก็ได้ครับ

 }

ตัวอย่างที่2 ควบคุม mc ให้เล่นโดยใช้ gotoAndPlay
stop();
mc1.stop();
mc1.addEventListener(MouseEvent.CLICK,atk);

function atk(MouseEvent)
{
mc1.gotoAndPlay(2);
}