[PV3D]キーボードで移動
必要なライブラリをインポートします
/**
* Copyright yes_i_can ( http://wonderfl.net/user/yes_i_can )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/lrcq
*/
// forked from yes_i_can's forked from: [PV3D] Simple Sphere
package
{
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
// 必要なライブラリをインポートします
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.special.CompositeMaterial;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Sphere;
import org.papervision3d.view.BasicView;
import org.papervision3d.core.effects.view.ReflectionView;
public class Main extends ReflectionView
{
private var ball:Sphere;
private var dx:int = 8;
private var dz:int = 8;
private var keyRight:Boolean = false;
private var keyLeft:Boolean = false;
public function Main()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
init();
}
private function init():void
{
//カメラ位置////////////////////////////////////////////////
camera.y = 200;
//床の作成//////////////////////////////////////////////////
var wm:WireframeMaterial = new WireframeMaterial();
var col:ColorMaterial = new ColorMaterial(0xffffff, 0.5);
var co:CompositeMaterial = new CompositeMaterial();
co.addMaterial(wm);
co.addMaterial(col);
var floor:Plane = new Plane(co, 2000, 2000, 12, 12);
floor.pitch( -90);
//床を3Dシーンに表示/////////////////////////////////////////
scene.addChild(floor);
// 球面を作成///////////////////////////////////////////////
var material: CompositeMaterial = new CompositeMaterial();
material.addMaterial(new WireframeMaterial(0xff6633));
material.addMaterial(new ColorMaterial(0xdcdcdc));
ball = new Sphere(material, 50, 6, 6);
ball.y = 50;
// 球面を3Dシーンに表示//////////////////////////////////////
scene.addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
updateCamera();
singleRender();
}
//カメラの移動(オブジェクトについていく)
function updateCamera():void {
camera.target = ball;
camera.x = ball.x;
camera.y = ball.y + 200;
camera.z = ball.z + 200;
}
protected function keyDownHandler(event:KeyboardEvent):void
{
//reset all the visible to green when you hit space
if(event.keyCode == Keyboard.LEFT)
{
ball.rotationZ -= dz;
ball.x = ball.x + dx;
}
if(event.keyCode == Keyboard.RIGHT)
{
ball.rotationZ += dz;
ball.x = ball.x - dx;
}
if(event.keyCode == Keyboard.UP)
{
ball.rotationX -= dx;
ball.z = ball.z - dz;
}
if(event.keyCode == Keyboard.DOWN)
{
ball.rotationX += dx;
ball.z = ball.z + dz;
}
}
}
}