Papervision3DTest02 :テキスト表示、座標判定のテスト
/**
* Copyright siouxcitizen ( http://wonderfl.net/user/siouxcitizen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/eIbL
*/
// forked from hacker_7daxapax's Papervision3DTest01
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.AntiAliasType;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.render.BasicRenderEngine;
[SWF(width="500", height="500", frameRate="60", backgroundColor="#66CCFF")]
public class MoviePlane extends Sprite
{
private var container : Sprite;
private var viewport : Viewport3D;
private var scene : Scene3D;
private var camera : Camera3D;
private var material : ColorMaterial;
private var planeObj : Plane;
private var renderer : BasicRenderEngine;
private var segment : int = 1; //面の分割数
private var textarea : TextField; // テキスト表示部分
private var lifeCount : int = 256; // 破壊までの残り耐久力
//MoviePlaneクラスのコンストラクタ
public function MoviePlane():void
{
//ビューポート生成
viewport = new Viewport3D(500, 500, false, true);
addChild(viewport);
//シーン生成
scene = new Scene3D();
//レンダリングエンジン生成
renderer = new BasicRenderEngine();
//マテリアル設定
material = new ColorMaterial( 0x000033, 1 );
material.oneSide = false;
//Planeオブジェクト生成
planeObj= new Plane(material, 500, 300, segment, segment);
scene.addChild(planeObj);
//カメラ設定
camera = new Camera3D();
camera.z = -300;
camera.focus = 50;
camera.zoom = 1;
//耐久力表示のためのテキストエリア設定
setTestareaForLifeCount();
//ベント処理用リスナを設定
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
//プレーンオブジェトを回転させ続ける
private function loop( event:Event ):void
{
//X軸回転
planeObj.rotationX -= 5;
//再レンダリング
renderer.renderScene(scene, camera, viewport);
}
//A、S、Dボタン押下時処理
private function onKeyDown(event:KeyboardEvent):void{
//Aボタン
if(event.keyCode == 65) {
if ((150 <= viewport.mouseX) && (viewport.mouseX <=350)
&&(200<= viewport.mouseY) && (viewport.mouseY <= 300)) {
lifeCount--;
if (lifeCount == 0) {
appendText("YOU CLEARED!!!");
lifeCount = 1;
return;
}
appendText("Power of endurance " + lifeCount + " left!");
}
//Sボタン
} else if (event.keyCode == 83) {
if ((150 <= viewport.mouseX) && (viewport.mouseX <= 350)
&&(200 <= viewport.mouseY) && (viewport.mouseY <= 300)) {
lifeCount--;
if (lifeCount == 0) {
appendText("YOU CLEARED!!!");
lifeCount = 1;
return;
}
appendText("Power of endurance " + lifeCount + " left!");
}
//Dボタン
} else if (event.keyCode == 68) {
if ((150 <= viewport.mouseX) && (viewport.mouseX <= 350)
&&(200 <= viewport.mouseY) && (viewport.mouseY <= 300)) {
lifeCount--;
if (lifeCount == 0) {
appendText("YOU CLEARED!!!");
lifeCount = 1;
return;
}
appendText("Power of endurance " + lifeCount + " left!");
}
}
renderer.renderScene(scene, camera, viewport);
}
//残り耐久力表示のためのテキストエリアを設定
private function setTestareaForLifeCount():void
{
var txtFmt:TextFormat = new TextFormat();
txtFmt.font = 'Courier New'; // 等幅フォント
txtFmt.color = '0xFFFFFF';
txtFmt.size = 15;
textarea = new TextField();
textarea.defaultTextFormat = txtFmt;
textarea.height = 465;
textarea.width = 465;
textarea.multiline = true;
textarea.antiAliasType = AntiAliasType.ADVANCED;
addChild(textarea);
}
public function appendText(text:String):void {
textarea.replaceText(0, 0, text + "\n");
}
}
}