coffeescriptでprivate

あらためて復習。
CoffeeScriptのクラスではprivateな変数・関数はつくれないっぽい。
(継承のオーバーライドを想定しない場合は、できないことはないっぽい →参考欄の1つ目)
最初に比較用のASのコードを載せています。

 

– AS脳で理解するための比較用 ActionScript
→ private var, private function がcoffeeだと使えない

 

package{
class SampleClass{
public static var staticVar:String = "staticVar";
public static var staticVar2:String = "staticVar2";

private static var _staticVar3:String = "_staticVar3";

public var publicVar2:String= "publicVar2";

/* CONSTRUCTOR */
function SampleClass(){
// インスタンスのプロパティ
this.publicVar = "publicVar";
}

// PUBLIC STATIC FUNCTION
public static staticPublicFunction(){
trace("\n**** from staticPublicFunction");
trace(this);
trace(_staticVar3);
trace(SampleClass.staticVar);
trace(publicVar);
trace(publicVar2);
}

/* PRIVATE FUNCTION */
private function _privateFunction(){
trace("\n**** from _privateFunction");
trace(this);
trace(_staticVar3);
trace(SampleClass.staticVar);
trace(publicVar);
trace(publicVar2);
}

// PUBLIC FUNCTION
public function publicFunction(){
trace("\n**** from publicFunction");
trace(this);
trace(_staticVar3);
trace(SampleClass.staticVar);
trace(publicVar);
trace(publicVar2);
}

public function callPrivateFunction(){
_privateFunction();
}

public function updateStaticVar3(){
_staticVar3 = "_staticVar3:changed";
}
}
}

 

– CoffeeScript

 

class SampleClass

### PUBLIC STATIC VAR ###
@staticVar: '@staticVar'
@staticVar2 = '@staticVar2'

###
# PRIVATE STATIC VAR
# クロージャー内の変数扱い? インスタンスの変数ではない
###
_staticVar3 = '_staticVar3'

### PUBLIC INSTANCE VAR ###
publicVar2: 'publicVar2'

###
# ここまでのまとめ:
# ・頭に@がつく場合
# 無条件で静的なプロパティになる
# ・@がない場合
# =を使うと静的なプロパティに、
# :を使うとインスタンス(prototype)のプロパティになる
###

### CONSTRUCTOR ###
constructor: ->
# インスタンスのプロパティ
@publicVar = '@publicVar'

### PUBLIC STATIC FUNCTION ###
@staticPublicFunction = ->
console.log('\n**** from staticPublicFunction')
console.log(@)
console.log _staticVar3 #OK
console.log SampleClass.staticVar #OK
console.log @publicVar #NG
console.log @publicVar2 #NG
@

###
# PRIVATE FUNCTION の出来損ない
# インスタンスのメンバー変数にアクセスできない!
# クロージャー内の関数扱い?
###
_privateFunction = ->
console.log('\n**** from _privateFunction')
console.log(@)
console.log _staticVar3 #OK
console.log SampleClass.staticVar #OK
console.log @publicVar #NG
console.log @publicVar2 #NG
@

### PUBLIC FUNCTION ###
publicFunction: ->
console.log('\n**** from publicFunction')
console.log(@)
console.log _staticVar3 #OK
console.log SampleClass.staticVar #OK
console.log @publicVar #OK
console.log @publicVar2 #OK
@

callPrivateFunction: ->
_privateFunction()
@

updateStaticVar3: ->
_staticVar3 = '_staticVar3:changed'
@

### 1つ目のインスタンス sample をつくって色々テスト ###

sample = new SampleClass()
console.log('\n**** from global')
console.log SampleClass.staticVar #OK
console.log sample.publicVar #OK
console.log sample.publicVar2 #OK
console.log sample._staticVar3 #NG

sample.publicFunction()
sample.callPrivateFunction()
SampleClass.staticPublicFunction()

### _staticVar3を更新する ###
sample.updateStaticVar3()

### 2つ目のインスタンス sample2 をつくって色々テスト ###
console.log '\n#### sample2 ####'

sample2 = new SampleClass()
console.log('\n**** from global')
console.log SampleClass.staticVar
console.log sample2.publicVar
console.log sample2.publicVar2
console.log sample2._staticVar3

sample2.publicFunction()
sample2.callPrivateFunction()
SampleClass.staticPublicFunction()

###
# sample2をつくっての実験のまとめ:
# _staticVar3 が変わってしまっていることからも、
# インスタンス変数ではなく、スタティックな変数であることがわかる。
###

 

– コンパイル結果 JavaScript

 

// Generated by CoffeeScript 1.4.0
(function() {
var SampleClass, sample, sample2;

SampleClass = (function() {
/* PUBLIC STATIC VAR
*/

var _privateFunction, _staticVar3;

SampleClass.staticVar = '@staticVar';

SampleClass.staticVar2 = '@staticVar2';

/*
# PRIVATE STATIC VAR
# クロージャー内の変数扱い? インスタンスの変数ではない
*/

_staticVar3 = '_staticVar3';

/* PUBLIC INSTANCE VAR
*/

SampleClass.prototype.publicVar2 = 'publicVar2';

/*
# ここまでのまとめ:
# ・頭に@がつく場合
# 無条件で静的なプロパティになる
# ・@がない場合
# =を使うと静的なプロパティに、
# :を使うとインスタンス(prototype)のプロパティになる
*/

/* CONSTRUCTOR
*/

function SampleClass() {
this.publicVar = '@publicVar';
}

/* PUBLIC STATIC FUNCTION
*/

SampleClass.staticPublicFunction = function() {
console.log('\n**** from staticPublicFunction');
console.log(this);
console.log(_staticVar3);
console.log(SampleClass.staticVar);
console.log(this.publicVar);
console.log(this.publicVar2);
return this;
};

/*
# PRIVATE FUNCTION の出来損ない
# インスタンスのメンバー変数にアクセスできない!
# クロージャー内の関数扱い?
*/

_privateFunction = function() {
console.log('\n**** from _privateFunction');
console.log(this);
console.log(_staticVar3);
console.log(SampleClass.staticVar);
console.log(this.publicVar);
console.log(this.publicVar2);
return this;
};

/* PUBLIC FUNCTION
*/

SampleClass.prototype.publicFunction = function() {
console.log('\n**** from publicFunction');
console.log(this);
console.log(_staticVar3);
console.log(SampleClass.staticVar);
console.log(this.publicVar);
console.log(this.publicVar2);
return this;
};

SampleClass.prototype.callPrivateFunction = function() {
_privateFunction();
return this;
};

SampleClass.prototype.updateStaticVar3 = function() {
_staticVar3 = '_staticVar3:changed';
return this;
};

return SampleClass;

})();

/* 1つ目のインスタンス sample をつくって色々テスト
*/

sample = new SampleClass();

console.log('\n**** from global');

console.log(SampleClass.staticVar);

console.log(sample.publicVar);

console.log(sample.publicVar2);

console.log(sample._staticVar3);

sample.publicFunction();

sample.callPrivateFunction();

SampleClass.staticPublicFunction();

/* _staticVar3を更新する
*/

sample.updateStaticVar3();

/* 2つ目のインスタンス sample2 をつくって色々テスト
*/

console.log('\n#### sample2 ####');

sample2 = new SampleClass();

console.log('\n**** from global');

console.log(SampleClass.staticVar);

console.log(sample2.publicVar);

console.log(sample2.publicVar2);

console.log(sample2._staticVar3);

sample2.publicFunction();

sample2.callPrivateFunction();

SampleClass.staticPublicFunction();

/*
# sample2をつくっての実験のまとめ:
# _staticVar3 が変わってしまっていることからも、
# インスタンス変数ではなく、スタティックな変数であることがわかる。
*/

}).call(this);


参考:
CoffeeScriptでstatic/private/publicなメンバ/メソッドをもったクラスのつくりかた « DevJamMemo
[改訂]CoffeeScriptでstatic/private/publicなメンバ/メソッドをもったクラスのつくりかた « DevJamMemo
JavaScriptでstatic/private/publicなメンバ/メソッドをもったクラスのつくりかた | ALUMICAN.NET
JavaScript のスコープチェーンとクロージャを理解する – tacamy memo
AcrionScript3やってた自分からみたCoffeeScript | 宇都宮ウエブ制作所

アルミ缶先生さすがっす

FLV周辺の再利用やノイズ対策

たくさんのFLVをいかに効率よく、美しく流すか。
みたいなメモです。

– 前提
Video、NetStreamやNetConnectionなどの処理を
まとめたVideoSpriteクラス、みたいなものを作る。
これらのインスタンスを毎回消去して、新しくnewすると
ブラウザに負荷がかかるので、消去せずに再利用する。

– 再利用と消去
以下に載せたクラスで、再利用前にやるのがsleepメソッド。
本当にいらなくなった時に消去するために呼ぶのがkillメソッド。

– ノイズ防止
FLVはキーフレーム間を差分で描画しているので、無茶な処理をすると
ピンクのノイズが出て、描画がボロボロになります。その対策。

1. 一度playを実行した_ns(NetStream)に再度playをかける時は、
必ずthis._ns.close();を呼んでから。

2. 再生中のものを頭出ししてすぐ再生したい時は
pause(); → seek(0); → NetStatusEventのNetStream.Seek.Notifyを受ける。
→ resume();
(単純に1のように、closeして再度playでもいい気がします)

— 追記(08/05/20):
再生中のものはすでにキャッシュ済みなのですぐ再生可能
→ 単純に1のように、closeして再度playした方が良いです。
—-

まとめると、closeし忘れに注意ということです。

以下 as。

package{

	import flash.display.Sprite;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;

	public class VideoSprite extends Sprite

		private var _video:Video;
		private var _nc:NetConnection;
		private var _ns:NetStream;

		public function VideoSprite():void{
			this._video = new Video();
			this.addChild(this._video);
			//NetConnectionをnew
			this._nc = new NetConnection();
			this._nc.connect(null);
			//NetStreamをnew
			this._ns = new NetStream(this._nc);
			var customClient = new Object();
			this._ns.client = customClient;
			//NetStreamをattach
			this._video.attachNetStream(this._ns);
			//イベント定義
			this._ns.addEventListener(NetStatusEvent.NET_STATUS, this.statusHandler); 
		}

		//NetStatusEvent処理
		private function statusHandler(event:NetStatusEvent):void
		{
			switch(event.info.code) {
				// 省略
				default:
					break;
			}
		}

		//色々省略

		//再利用時のクローズ作業
		public function sleep():void{
			this._video.clear();
			this._nc.close();
			this._ns.close();
		}

		//消去時
		public function kill():void {
			this.sleep();
			this.removeChild(this._video);
			this._ns.removeEventListener(NetStatusEvent.NET_STATUS, this.statusHandler); 
			this._video.attachNetStream(null);
			this._ns = null;
			this._nc = null;
			this._customClient = null;
			this._video = null;
		}
	}

}

参照の値渡し

「参照の値渡し」ということばを初めて聞いた。

まず基本から。プリミティブ型とオブジェクト型の違い。

/* ActionScript3 */
//テスト1
var strA:String = "AAA";
var strB:String = "BBB";
this.strA = this.strB; //BBB, BBB
this.strB = "CCC"; //BBB, CCC
this.strA = "DDD"; //DDD, CCC

//テスト2
var objA:Object = {value: "AAA"}
var objB:Object = {value: "BBB"}
this.objA = this.objB; //BBB, BBB
this.objB.value = "CCC"; //CCC, CCC
this.objA.value = "DDD"; //DDD, DDD

//テスト3
var objA:Object = {value: "AAA"}
var objB:Object = {value: "BBB"}
this.objA = this.objB; //BBB, BBB
this.objB = {value: "CCC"} //BBB, CCC
this.objA = {value: "DDD"} //DDD, CCC

次に実は今まで少し不安だった部分の検証。
引数として受ける時、どんな感じに参照されているか。
これが参照の値渡しってことなのかも。

/* ActionScript3 */
//テスト1
var strA:String = "AAA";
var strB:String = "BBB";
this.strA = this.strB; //BBB, BBB
this.setCCC(this.strB); //BBB, BBB
this.setDDD(this.strA); //BBB, BBB

function setCCC(str:String):void{
str = "CCC";
}
function setDDD(str:String):void{
str = "DDD";
}

//テスト2
var objA:Object = {value: "AAA"}
var objB:Object = {value: "BBB"}
this.objA = this.objB; //BBB, BBB
this.setCCC(this.objB); //CCC, CCC
this.setDDD(this.objA); //DDD, DDD

function setCCC(obj:Object):void{
obj.value = "CCC";
}
function setDDD(obj:Object):void{
obj.value = "DDD";
}

//テスト3
var objA:Object = {value: "AAA"}
var objB:Object = {value: "BBB"}
this.objA = this.objB; //BBB, BBB
this.setCCC(this.objB); //BBB, BBB
this.setDDD(this.objA); //BBB, BBB

function setCCC(obj:Object):void{
obj = {value:"CCC"};
}
function setDDD(obj:Object):void{
obj = {value:"DDD"};
}

たぶん当たり前のことなんだけど、
どこからどこまでが同じメモリの参照なのか
ということがわかりました。

- 参考サイト
AS3では関数の引数はすべて値渡し(call by value)である / flashrod
参照渡し・値渡し - PBD - subtech
func09 » コリン・ムック「今から始めるActionScript 3.0」に行ってきました

swfforcesize.js

swfforcesize.js。

swfObjectでflashコンテンツを100%, 100%表示時、
swfの既定サイズよりブラウザが小さくなっても、スクロールバーは出ない。
swfforcesize.jsを追加することで、スクロールバーが出るようになる。

以下cssの記述と、htmlの記述。(一部省略気味)

/* CSS */
html {
height: 100%;
overflow: auto;
}
#flashcontent {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
}
body {
height: 100%;
margin: 0px;
padding: 0px;
background-color: #fff;
}
<!-- HTML -->
<div id="flashcontents"></div>
<script type="text/javascript">
//<![CDATA[
var so = new SWFObject('./----.swf', 'website', '100%', '100%', '9', '#ffffff');
so.useExpressInstall('./js/expressinstall.swf');
so.addParam('menu', 'false');
so.addParam('scale', 'noscale');
so.addParam('salign', 'lt');
if(so.write('flashcontent')){
var forcesize = new SWFForceSize( so, 1024, 680 ); //←swfの設定サイズ
}
// ]]>
</script>

- 参考サイト
pixelbreaker : SWFObject add-on: Size limiting for full window flash

Singleton

/* ActionScript3 */
package{

public class Singleton{

//自身のインスタンス
private static var _instance:Singleton;

//コンストラクタ
public function Singleton(pvt:SingletonEnforcer){
this.init();
}

//インスタンスのgetter
public static function get instance():Singleton{
if ( Singleton._instance === null ) {
Singleton._instance = new Singeleton(new SingletonEnforcer());
}
return Singleton._instance;
}

//インスタンスの初期化
private function init():void{}
}
}
}

internal class SingletonEnforcer{}

– 参考URL
AS3 で Singleton – PBD – subtech
pixelbreaker : AS3.0 Better Singletons

TypeError: Error #1085

AS3でURLLoaderからXMLを読み込み後、
this._myXML = XML(this._myLoader.data);
って感じにXMLに代入するとき、

IE6で
「TypeError: Error #1085: エレメント型 “link” は対応する終了タグ “” で終了する必要があります。」
というエラーが出て困った。

原因は、hetemlの404ページの変なhtmlを
読み込んでしまっていたことだった。おそらく。
(Firefoxではスルーだった。)

htmlとswfを別の階層に置いていたので、
ローカルでパブリッシュ時は一発OK。
htmlにswfが乗っかって再生時は、一度ロードかけてエラーが出た後に
パスにswfのディレクトリを足してリロード。と思っていたが、
今後こんなことで悩みたくないので、htmlとswfは同じ階層に置こう。

Error Messages

Flash CS3のエラーメッセージ。
日本語版使ってれば日本語で出るけど、
これって英語だとなんて言うエラーだろう
という時のために書き出した。

1093
A class name was expected.
クラス名が必要です。

1094
A base class name is expected after the ‘extends’ keyword.
基本クラス名が、’extends’ キーワードの後に必要です。

1095
A member attribute was used incorrectly.
メンバー属性が正しく使用されていません。

1096
The same member name may not be repeated more than once.
同じメンバー名を繰り返し使用することはできません。

1097
All member functions need to have names.
すべてのメンバー機能には名前が必要です。

1099
This statement is not permitted in a class definition.
このステートメントは、クラス定義で許可されていません。

1100
A class or interface has already been defined with this name.
クラスまたはインターフェイスが既にこの名前で定義されています。

more >

Tweener 特殊プロパティ一覧

caurina.transitions.properties の中にある
6つのasファイルから抜き出した。

それぞれ 〜.init(); が必要。
(例: ColorShortcuts.init();)

AS2, AS3共通
———————————————————————-
■ ColorShortcuts

_color_ra
_color_rb
_color_ga
_color_gb
_color_ba
_color_bb
_color_aa
_color_ab

色 16進数形式 (0x000000〜0xFFFFFF)
_color

_colorTransform

明度
_brightness
_tintBrightness

コントラスト
_contrast

色相
_hue

彩度
_saturation
_dumbSaturation

詳細
_color_redMultiplier
_color_redOffset
_color_greenMultiplier
_color_greenOffset
_color_blueMultiplier
_color_blueOffset
_color_alphaMultiplier
_color_alphaOffset

———————————————————————-
■ CurveModifiers
_bezier

———————————————————————-
■ DisplayShortcuts
スケール (_xscale, _yscale連動)
_scale

アルファ (_visible切り替え機能付き)
_autoAlpha

フレーム
_frame

_scrollRect_x
_scrollRect_y
_scrollRect_left
_scrollRect_right
_scrollRect_top
_scrollRect_bottom
_scrollRect_width
_scrollRect_height

———————————————————————-
■ FilterShortcuts
ベベル
_Bevel_angle
_Bevel_blurX
_Bevel_blurY
_Bevel_distance
_Bevel_highlightAlpha
_Bevel_highlightColor
_Bevel_highlightColor_r
_Bevel_highlightColor_g
_Bevel_highlightColor_b
_Bevel_knockout
_Bevel_quality
_Bevel_shadowAlpha
_Bevel_shadowColor
_Bevel_shadowColor_r
_Bevel_shadowColor_g
_Bevel_shadowColor_b
_Bevel_strength
_Bevel_type

ブラー
_Blur_blurX
_Blur_blurY
_Blur_quality

カラーマトリックス
_ColorMatrix_matrix
_ColorMatrix_matrix_rr
_ColorMatrix_matrix_rg
_ColorMatrix_matrix_rb
_ColorMatrix_matrix_ra
_ColorMatrix_matrix_ro
_ColorMatrix_matrix_gr
_ColorMatrix_matrix_gg
_ColorMatrix_matrix_gb
_ColorMatrix_matrix_ga
_ColorMatrix_matrix_go
_ColorMatrix_matrix_br
_ColorMatrix_matrix_bg
_ColorMatrix_matrix_bb
_ColorMatrix_matrix_ba
_ColorMatrix_matrix_bo
_ColorMatrix_matrix_ar
_ColorMatrix_matrix_ag
_ColorMatrix_matrix_ab
_ColorMatrix_matrix_aa
_ColorMatrix_matrix_ao

_Convolution_alpha
_Convolution_bias
_Convolution_clamp
_Convolution_color
_Convolution_r
_Convolution_g
_Convolution_b
_Convolution_divisor
_Convolution_matrixX
_Convolution_matrixY
_Convolution_preserveAlpha

_DisplacementMap_alpha
_DisplacementMap_color
_DisplacementMap_color_r
_DisplacementMap_color_g
_DisplacementMap_color_b
_DisplacementMap_componentX
_DisplacementMap_componentY
_DisplacementMap_mapBitmap
_DisplacementMap_mapPoint
_DisplacementMap_mode
_DisplacementMap_scaleX
_DisplacementMap_scaleY

ドロップシャドウ
_DropShadow_alpha
_DropShadow_angle
_DropShadow_blurX
_DropShadow_blurY
_DropShadow_color
_DropShadow_color_r
_DropShadow_color_g
_DropShadow_color_b
_DropShadow_distance
_DropShadow_hideObject
_DropShadow_inner
_DropShadow_knockout
_DropShadow_quality
_DropShadow_strength

グロー
_Glow_alpha
_Glow_blurX
_Glow_blurY
_Glow_color
_Glow_color_r
_Glow_color_g
_Glow_color_b
_Glow_inner
_Glow_knockout
_Glow_quality
_Glow_strength

_GradientBevel_angle
_GradientBevel_blurX
_GradientBevel_blurY
_GradientBevel_distance
_GradientBevel_quality
_GradientBevel_strength
_GradientBevel_type
_GradientGlow_angle
_GradientGlow_blurX
_GradientGlow_blurY
_GradientGlow_distance
_GradientGlow_knockout
_GradientGlow_quality
_GradientGlow_strength
_GradientGlow_type

———————————————————————-
■ SoundShortcuts
_sound_volume
_sound_pan

———————————————————————-
■ TextShortcuts
_text_color
_text_color_r
_text_color_g
_text_color_b
_text_indent
_text_leading
_text_leftMargin
_text_letterSpacing
_text_rightMargin
_text_size

———————————————————————-

SWFObjectで100%サイズ

– 最新記事
swfforcesize.js – SOOHEI.NET/BLOG

SWfObject (v1.5) でswfを100%, 100%で表示する時、
height: 100%; が書かれていないと高さがフィットしない。

/* CSS */ <style type="text/css"> /* hide from ie on mac */ html { height: 100%; overflow: hidden; } #flashcontent { height: 100%; } /* end hide */ body { height: 100%; margin: 0; padding: 0; background-color: #f60; } </style>

– 参考サイト
deconcept › SWFObject: Javascript Flash Player detection and embed script

bitmapDataの最大サイズ