Flash CS3 ActionScript3.0 勉強部屋 |
・Flashを触る上で、クリックで物を動かしたり、キーボードが押されたときに物を動かす といった仕組みは必要不可欠です。 ◆クリックすると回転方向が反転します ◆左右キーで、左右方向の回転速度が上がります ◆お持ち帰り用 2_addEventListener.fla ・これには addEventListener というものを使います。 // Script --------------------------------------------------------------------- var rot:int = 10; this.addEventListener(Event.ENTER_FRAME,rotationBall); function rotationBall(event:Event):void{ this.ball.rotation += rot; } this.stage.addEventListener(MouseEvent.CLICK,reverseRotation); function reverseRotation(event:MouseEvent):void{ rot = -rot; } this.stage.addEventListener(KeyboardEvent.KEY_DOWN,accelerateBall); function accelerateBall(event:KeyboardEvent):void{ switch(event.keyCode){ case Keyboard.LEFT: rot -= 3; break; case Keyboard.RIGHT: rot += 3; break; } } // ---------------------------------------------------------------------------- ・this.stage.addEventListener(KeyboardEvent.KEY_DOWN,accelerateBall); という風に書くと、 this.stage よ、おまえは KeyboardEvent.KEY_DOWN が起こったら accelerateBall を呼ぶんだぞ!! と命令していることになるわけです。 |
・たくさんの絵を配置するとなると手動では面倒になってきます。 ・ActionScript2.0では、attachMovieというものを使っていましたが、ActionScript3.0では・・・ ◆クリックするとなんか配置されます ◆お持ち帰り用 3_addChild.fla ・これには addChild というものを使います。 // Script --------------------------------------------------------------------- this.stage.addEventListener(MouseEvent.CLICK,createCatBall); function createCatBall(event:MouseEvent):void{ var cb:CatBall = new CatBall(); cb.x = event.stageX; cb.y = event.stageY; addChild(cb); } // ---------------------------------------------------------------------------- ・前バージョンのFlashと同様にリンケージをするわけですが、基本クラスというものを選べます。 flash.display.MovieClipを選んでまず問題はありません。 動きのないもの(タイムラインが必要がないもの)の場合は、 flash.display.Spriteを選んだほうが処理が軽いです。 クラス、は使うときに利用する名前です。 ・リンケージが済んだら、newで生成し、addChildに渡すだけで表示されます。 ・明示されていませんが、生成された絵はメインタイムライン上に配置されています。 |
・ActionScript2.0での、当たり判定はhitTestというものを使っていました。ActionScript3.0では・・・ ◆マウスカーソルが当たると透けます ◆四角同士が当たると透けます ◆お持ち帰り用 4_hitTest.fla ・大きくは変わっていませんが、ちょっとした違いがあります。 // Script --------------------------------------------------------------------- // 移動量 var mx1:int = 3; var my1:int = 3; var mx2:int = 5; var my2:int = 5; this.stage.addEventListener(Event.ENTER_FRAME,func); function func(event:Event):void{ // box1の移動制御 this.box1.x += mx1; this.box1.y += my1; if( this.box1.x<=0 || (360-this.box1.width)<=this.box1.x ){ mx1 = -mx1; } if( this.box1.y<=0 || (240-this.box1.height)<=this.box1.y ){ my1 = -my1; } // box2の移動制御 this.box2.x += mx2; this.box2.y += my2; if( this.box2.x<=0 || (360-this.box2.width)<=this.box2.x ){ mx2 = -mx2; } if( this.box2.y<=0 || (240-this.box2.height)<=this.box2.y ){ my2 = -my2; } // 透明度初期化 this.box1.alpha = 1; this.box2.alpha = 1; // box1とbox2が当たれば透けさせる if(this.box1.hitTestObject(this.box2)){ this.box1.alpha = 0.2; this.box2.alpha = 0.2; } // box1にカーソルが当たれば透けさせる if(this.box1.hitTestPoint(this.mouseX,this.mouseY,true)){ this.box1.alpha = 0.2; } // box2にカーソルが当たれば透けさせる if(this.box2.hitTestPoint(this.mouseX,this.mouseY,true)){ this.box2.alpha = 0.2; } } // ---------------------------------------------------------------------------- ・ActionScript3.0では、hitTestObjectとhitTestPointの2種類に分かれています。 hitTestObjectはモノとモノの当たり判定です。 hitTestPointはモノと座標の当たり判定です。 ・ご注意いただきたいことがあります。 hitTestObjectと、hitTestPointで最後の引数がfalseの場合は、 モノの形通りではなく、モノを囲む四角で当たり判定が取られます。 |
◆クリックすると音が鳴ります ◆お持ち帰り用 5_Sound.fla // Script --------------------------------------------------------------------- var se1:Sound = new SE1(); var se2:Sound = new SE2(); var se3:Sound = new SE3(); var se4:Sound = new SE4(); this.stage.addEventListener(MouseEvent.CLICK, func); function func(event:MouseEvent):void { if (this.box1.hitTestPoint(this.mouseX, this.mouseY, true)) se1.play(); if (this.box2.hitTestPoint(this.mouseX, this.mouseY, true)) se2.play(); if (this.box3.hitTestPoint(this.mouseX, this.mouseY, true)) se3.play(); if (this.box4.hitTestPoint(this.mouseX, this.mouseY, true)) se4.play(); } // ---------------------------------------------------------------------------- ・newで生成して、play()を使うだけで再生できます。 ・準備としてリンケージをしておくのですが、ちょっとした注意があります。 基本クラスを「flash.media.Sound」にする ActionScriptに書き出し、にチェックを入れる 最初のフレームに書き出し、にチェックを入れる |
Copyright © 2008-2024 Nekorald All rights reserved.