2007/6/23

會連鎖引爆的球(類別應用)

import mx.utils.Delegate;


class ball extends MovieClip
{
var _target:MovieClip;
public var id:Number;
public var start_hit=0;

private static var all_of_class:Array=new Array();

public function ball(parent:MovieClip,ball_name:String,icon_photo:String)
{
_target=parent.attachMovie(icon_photo,ball_name,
parent.getNextHighestDepth(),{_x:Math.random()*Stage.width,_y:Math.random()*Stage.height});
_target.stop();
_target.onEnterFrame=Delegate.create(this,freerun);
_target.onPress=Delegate.create(this,onpress_fun);

id=all_of_class.push(_target)-1; //矩陣長度減一
trace(id);
}

public function freerun(sss:String)
{
_target._x+=(Math.random()*Stage.width-_target._x)*0.05;
_target._y+=(Math.random()*Stage.height-_target._y)*0.05;

check_hit()
if(_target._currentframe==_target._totalframes)
{
_target.removeMovieClip();
}
}
public function onpress_fun()
{
start_hit=1;
trace(_target+"press");
}


public function check_hit()
{

if(start_hit==1)
{
_target.play()
for(var i=0;i
{
if(_target!=all_of_class[i] &&_target.hitTest(all_of_class[i]))
{
var tmp=all_of_class[i];
tmp.onPress();
}
}
}
}
}

2007/6/21

FLASH物件導向範例

FLASH物件導向範例

這是一個會自動追縱滑鼠的影片

FLA中只要加入
new my_icon(母影片片段,元件庫元件名稱即可)



AS類別
my_icon的程式碼:
import mx.utils.Delegate;

class my_icon extends MovieClip {
private var _target:MovieClip;
private var sx:Number;
private var sy:Number;
public function my_icon(parent, icon_photo)
{

_target=parent.attachMovie(icon_photo, "icon"+parent.getNextHighestDepth(),
parent.getNextHighestDepth(), {_x:Math.random()*550, _y:Math.random()*400});
sx=_target._x;
sy=_target._y;
_target.onEnterFrame=Delegate.create(this,okok);
}

public function okok()
{
//trace(_root._xmouse+","+_root._ymouse);
//trace(_target._x+","+_target._y)
if( Math.sqrt((sx-_root._xmouse)*(sx-_root._xmouse) + (sy-_root._ymouse)*(sy-_root._ymouse)) <70>
{
//trace(_target);
//_target._x=_root._xmouse;
//_target._y=_root._ymouse;
trace_mouse(_root._xmouse,_root._ymouse);


}else
{
trace_mouse(sx,sy);
}
}

public function trace_mouse(dx:Number,dy:Number)
{
_target._x+=Math.round((dx-_target._x)*0.2);
_target._y+=Math.round((dy-_target._y)*0.2);
}

}

反彈練習

/*
onClipEvent (load) {
var max_move_x:Number = 1;
var max_move_y:Number = 1;
var dx:Number = 0;
var dy:Number = 0;
var dirx:Number = 1;
var diry:Number = 1;
var lastx = _x;
var lasty = _y;
var press_ch:Number=0;
var gg:Number=10;
setInterval(this, "setlast", 500);
function setlast():Void {
this.lastx = _x;
this.lasty = _y;


}
}
onClipEvent (enterFrame) {
//動力
_x += (this.dx*dirx);
_y += (this.dy*diry);


//阻力
this.dx = (this.dx>0) ? (this.dx-0.1) : 0;
this.dy = (this.dy>0) ? (this.dy-0.1) : 0;

//trace(bounds.xMin+","+bounds.yMin+","+bounds.xMax+","+bounds.yMax);
//改變方向
var bounds:Object = this.getBounds(_root);
if ((bounds.xMin<0>550 &&amp;amp;amp; this.dirx>0)) {
this.dirx *= -1;
}
if ((bounds.yMin<0>400 &&amp;amp;amp; this.diry>0)) {
this.diry *= -1;
}

}
on (press) {
this.dx = 0;
this.dy = 0;
this.startDrag();
this.press_ch=1;
}
on (release, releaseOutside) {
this.press_ch=0;
this.stopDrag();
if (_x>this.lastx) {
this.dirx = 1;
this.dx = this.max_move_x*(_x-this.lastx)*0.2;
} else if (_xthis.lasty) {
this.diry = 1;
this.dy = this.max_move_y*(_y-this.lasty)*0.2;
} else if (_y
this.diry = -1;
this.dy = this.max_move_y*(this.lasty-_y)*0.2;
}
}

*/
img.setMask(影片元件.子影片元件);

ex:

img1.setMask(mask_mc.mm);
mask_mc.startDrag(true);

碰撞測試

A.hitTest(B);
測試A是否碰撞到B。
其中B與A的關系要弄清楚,如_root.B的注澺


ex:

area.onEnterFrame=function()
{
if(this.hitTest(this._parent.cube))
trace("hit");
}

如何從原件庫中,把影片物件丟入場景

如何從原件庫中,把影片物件丟入場景呢?


1、
在原件庫中的影片原件上>右鍵>連結>連結屬性:

識別名稱:定義要在AS裡產要用的物件類別名稱。
並在匯出給AS使用打勾

2、
var 容器:MovieClip=this.attachMovie("識別名稱","產生的影片名稱",深度);

另外再定義容器的所在他置即可

3、
參考書本9-5頁

由一個物件發出訊息給另一個物件

如何由一個物件發出訊息給另一個物件呢?

1、預先載入import mx.events.EventDispatcher;套件
EventDisatcher套件可以給予Flash中的類別有廣播事件和接收事件的能力。載入後,就可使用addEventListener()加入接收事件、removeEventListener()移除接收事件、dispatchEvent()廣播事件函式。

2、載入後,仍需要另定義宣告每個函式

public var addEventListener:Function;
public var removeEventListener:Function;
private var dispatchEvent:Function;


3、要讓目前物件有事件發的功能,初始化。
EventDispatcher.initialize(this);

4、廣播事件訊息
diapatchEvent是針對類別內部的事件傳送方法。
diapatchEvent({type:"事件的類別名稱",欲傳遞的參數或物件});


5、讓欲對事件有反應的物件有接受的功能
物件.addEventListener("事件的類別名稱",執行函式名稱)

只用一個影格重覆執行函式/每格幾秒執行函式

函式setInterval(目標this,"function_name",秒數)

EX:

setInterval(this,"myfunction",250);

function myfunction():Void
{
...
...
}

就會每隔0.25秒執行myfunction


另一個方法,我覺得這個比較好

_root.onEnterFrame =function()
{
程式碼


}

直接進影格就run一次
不過只能有一個影格

將flash資訊儲存在cookie之中,SharedObject的使用

var myobject:SharedObject = SharedObject.getLocal("name");

從電腦中載入name中的資料,如果沒有則建立一個。

myobject.data.userName從中取得userName的資料

myobject.clear()清空電腦中存的資料

myobject.flush()立即存檔。

檢視flash是否讀完,或讀到

_framesloaded可以得知目前影片讀取的情形,如果影片總長120,未讀完是0,讀完就變120,可以判斷_framesloaded是否等於120,決定是否讀完。

_root.onEnterFrame = function() {
if (this._framesloaded//判斷是否讀完
this.gotoAndPlay(1);//繼續留在同一個影格
var pctLoaded:Number = Math.round(this.getBytesLoaded()/this.getBytesTotal()*100);//計算百分比
bar_mc._xscale = pctLoaded;
} else {
this.gotoAndStop(2);//跳下一個影格
}
};
stop();

flash,攝影機運用,使用攝影機抓取影像

還沒有加入互動
希望不久的將來可以放入一些互動

import flash.display.BitmapData;

var map :BitmapData = new BitmapData(320,240,true, 0);
var cam:Camera=Camera.get();
//cam.setMode(320,240,10,true);

webcam.attachVideo(cam);


_root.createEmptyMovieClip("fake",this.getNextHighestDepth());


onEnterFrame=function()
{
map.draw(webcam);

var clr=map.getPixel(webcam._xmouse,webcam._ymouse);
trace(clr.toString(16));
}

flash影片百分比

_root.getBytesLoaded()
目前已讀的byte數

_root.getBytesTotal()
總byte數

100*(_root.getBytesLoaded()/_root.getBytesTotal())
已讀百分比

if(_root.getBytesLoaded()<_root.getBytesTotal())
檢查是影片是否已讀完


stop();
this.onEnterFrame=function()
{
if(_root.getBytesLoaded()<_root.getBytesTotal())
{
gotoAndPlay(1);
}
bar._xscale=Math.round(100*(_root.getBytesLoaded()/_root.getBytesTotal()))
}