http://bbs.9ria.com/viewthread.php?tid=76326&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000
资讯类型: 翻译
来源页面: http://www.emanueleferonato.com/2011/02/28/flash-3d-sokoban-prototype-with-away3d-%E2%80%93-final-version/
资讯原标题: Flash 3D Sokoban Prototype with Away3D – final version
资讯原作者: Heorhiy
我的评论:我很惭愧,其实这已经不是新闻了。不过还好,这个时效性不是很强……上帝啊,宽恕我的罪过吧……阿门……
对这篇文你有啥看法,跟贴说说吧!欢迎口水和板砖,哈哈。欢迎大家和我们一同分享更多资讯。
这是Away3D中的脚本,其中包括我在Flare3D版本中展示的同样的功能。
这里有几个问题,如果像我在62行那样使用Z轴排序的话,像246和248行那样更改了箱子顶部材质,一旦越过了目标,并且帧率很低,那就不对了。
不管怎么样,下面这些就是它的代码:
package {
//需要导入的Flash内置类
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Vector3D;
import flash.display.BitmapData;
//需要导入的Away3D引擎的内置类
import away3d.containers.*;
import away3d.primitives.*;
import away3d.cameras.*;
import away3d.core.render.*;
import away3d.materials.*;
import away3d.lights.DirectionalLight3D;
import away3d.debug.*;
public class Main extends Sprite {
private const CUBESIZE:Number=50;
//推箱子演示的玩家等级和玩家位置
private var levels:Array=[[1,1,1,1,0,0,0,0],[1,0,0,1,1,1,1,1],[1,0,2,0,0,3,0,1],[1,0,3,0,0,2,4,1],[1,1,1,0,0,1,1,1],[0,0,1,1,1,1,0,0]];
private var playerCol:uint;
private var playerRow:uint;
private var playerRotation:Number=0;
private var playerAngle:Number=0;
private var playerMovement:Number=0;
private var dRow:int;
private var dCol:int;
//Away3D引擎变量
private var view:View3D;// View3D类表示绘图背景
private var theCamera:SpringCam;// 自定义一个摄像机来管理第一和第三人物摄像机
private var player:Sphere;// 玩家控制物体
private var cameraTarget:Sphere;
private var movingCrate:Cube;// 箱子物体
// 位图对象
private var crateBitmap:BitmapData=new BitmapData(256,256);
private var crateTopBitmap:BitmapData=new BitmapData(256,256);
private var floorBitmap:BitmapData=new BitmapData(256,256);
private var wallBitmap:BitmapData=new BitmapData(256,256);
private var goalBitmap:BitmapData=new BitmapData(256,256);
private var crateTopGoalBitmap:BitmapData=new BitmapData(256,256);
private var backBitmap:BitmapData=new BitmapData(512,512);
private var playerBitmap:BitmapData=new BitmapData(512,512);
// 一些材质对象
private var floorMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(floorBitmap);
private var wallMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(wallBitmap);
private var goalMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(goalBitmap);
private var crateMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(crateBitmap);
private var crateTopMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(crateTopBitmap);
private var crateTopGoalMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(crateTopGoalBitmap);
private var backMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(backBitmap);
private var playerMaterial:WhiteShadingBitmapMaterial=new WhiteShadingBitmapMaterial(playerBitmap);
function Main() {
floorBitmap.draw(new floorPic(256,256));
wallBitmap.draw(new wallPic(256,256));
crateBitmap.draw(new cratePic(256,256));
crateTopBitmap.draw(new crateTopPic(256,256));
crateTopGoalBitmap.draw(new crateTopGoalPic(256,256));
goalBitmap.draw(new goalPic(256,256));
backBitmap.draw(new backPic(512,512));
playerBitmap.draw(new playerPic(512,512));
// 场景设置
view=new View3D({x:320,y:240});
//view.renderer = Renderer.CORRECT_Z_ORDER;
var stats:AwayStats=new AwayStats(view);
addChild(view);
addChild(stats);
theCamera= new SpringCam();
view.camera=theCamera;
var light:DirectionalLight3D = new DirectionalLight3D();
light.direction=new Vector3D(CUBESIZE*10,- CUBESIZE*6,CUBESIZE*4);
view.scene.addLight(light);
var sky:Skybox=new Skybox(backMaterial,backMaterial,backMaterial,backMaterial,backMaterial,backMaterial);
view.scene.addChild(sky);
// 等级结构设置
var cube=Cube;
for (var i:uint=0; i<6; i++) {
for (var j:uint=0; j<8; j++) {
switch (levels[i][j]) {
case 0 :
cube=new Cube({material:floorMaterial,depth:CUBESIZE,width:CUBESIZE,height:CUBESIZE/2,x:CUBESIZE*i,y:0,z:CUBESIZE*j});
view.scene.addChild(cube);
break;
case 1 :
cube=new Cube({material:floorMaterial,depth:CUBESIZE,width:CUBESIZE,height:CUBESIZE/2,x:CUBESIZE*i,y:0,z:CUBESIZE*j});
view.scene.addChild(cube);
cube=new Cube({material:wallMaterial,depth:CUBESIZE,width:CUBESIZE,height:CUBESIZE,x:CUBESIZE*i,y:CUBESIZE*3/4,z:CUBESIZE*j});
view.scene.addChild(cube);
break;
case 2 :
cube=new Cube({material:goalMaterial,depth:CUBESIZE,width:CUBESIZE,height:CUBESIZE/2,x:CUBESIZE*i,y:0,z:CUBESIZE*j});
view.scene.addChild(cube);
break;
case 3 :
cube=new Cube({material:floorMaterial,depth:CUBESIZE,width:CUBESIZE,height:CUBESIZE/2,x:CUBESIZE*i,y:0,z:CUBESIZE*j});
view.scene.addChild(cube);
cube=new Cube({material:crateMaterial,depth:CUBESIZE,width:CUBESIZE,height:CUBESIZE,x:CUBESIZE*i,y:CUBESIZE*3/4,z:CUBESIZE*j});
cube.cubeMaterials.bottom=crateTopMaterial;
cube.name="crate_"+i+"_"+j;
view.scene.addChild(cube);
break;
case 4 :
cube=new Cube({material:floorMaterial,depth:CUBESIZE,width:CUBESIZE,height:CUBESIZE/2,x:CUBESIZE*i,y:0,z:CUBESIZE*j});
view.scene.addChild(cube);
player=new Sphere({material:playerMaterial,radius:CUBESIZE/2,segmentsW:16,segmentsH:16,x:CUBESIZE*i,y:CUBESIZE*3/4,z:CUBESIZE*j});
view.scene.addChild(player);
cameraTarget=new Sphere({material:playerMaterial,radius:CUBESIZE/5,segmentsW:4,segmentsH:4,x:CUBESIZE*i,y:CUBESIZE*3/4,z:CUBESIZE*j});
view.scene.addChild(cameraTarget);
playerCol=j;
playerRow=i;
break;
}
}
}
// 事件监听器
addEventListener(Event.ENTER_FRAME,onEnterFrm);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDwn);
}
private function onKeyDwn(e:KeyboardEvent):void {
if (playerRotation==0&&playerMovement==0) {
switch (e.keyCode) {
case 37 :
playerRotation=-9;
break;
case 38 :
movingCrate=null;
var playerAngle:Number=Math.round(player.rotationY)%360;
if (playerAngle<0) {
playerAngle+=360;
}
// 我们需要判断出当前的行列数值
// 并且新的行列数值将取决于玩家的方向
switch (playerAngle) {
case 0 :
dRow=0;
dCol=-1;
break;
case 90 :
dRow=-1;
dCol=0;
break;
case 180 :
dRow=0;
dCol=1;
break;
case 270 :
dRow=1;
dCol=0;
break;
}
if (levels[playerRow+dRow][playerCol+dCol]==0||levels[playerRow+dRow][playerCol+dCol]==2) {
// 玩家可以移动了
playerMovement=- CUBESIZE/10;
} else {
if (levels[playerRow+dRow][playerCol+dCol]==3||levels[playerRow+dRow][playerCol+dCol]==5) {
if (levels[playerRow+2*dRow][playerCol+2*dCol]==0||levels[playerRow+2*dRow][playerCol+2*dCol]==2) {
// 玩家可以移动并推动箱子
movingCrate=view.scene.getChildByName("crate_"+(playerRow+dRow)+"_"+(playerCol+dCol))as Cube;
playerMovement=- CUBESIZE/10;
}
}
}
break;
case 39 :
playerRotation=9;
break;
}
}
}
private function onEnterFrm(e:Event):void {
if (playerRotation) {
// 下面的代码是Away3D如何旋转一个物体
player.rotationY+=playerRotation;
cameraTarget.rotationY+=playerRotation;
var reachedAngle:Number=Math.round(player.rotationY);
if (reachedAngle%90==0) {
playerRotation=0;
}
}
if (playerMovement) {
playerAngle=Math.round(player.rotationY)%360;
if (playerAngle<0) {
playerAngle+=360;
}
// 下面的代码是Away3D如何移动一个物体
cameraTarget.moveForward(playerMovement);
switch (playerAngle) {
case 0 :
player.z+=playerMovement;
if (Math.round(player.rotationZ)%360==0) {
player.rotationX+=18;
} else {
player.rotationX-=18;
}
break;
case 90 :
player.x+=playerMovement;
player.rotationZ+=18;
break;
case 180 :
player.z-=playerMovement;
if (Math.round(player.rotationZ)%360==0) {
player.rotationX+=18;
} else {
player.rotationX-=18;
}
break;
case 270 :
player.x-=playerMovement;
player.rotationZ-=18;
break;
}
if (movingCrate) {
if (playerAngle<0) {
playerAngle+=360;
}
switch (playerAngle) {
case 0 :
movingCrate.moveForward(playerMovement);
break;
case 90 :
movingCrate.moveRight(playerMovement);
break;
case 180 :
movingCrate.moveBackward(playerMovement);
break;
case 270 :
movingCrate.moveLeft(playerMovement);
break;
}
}
// 我们需要通过这种方式来判断玩家控制的物体是否到达目的地
if (Math.round(player.rotationY)%180==0) {
if (Math.round(player.z)%CUBESIZE==0) {
playerMovement=0;
}
} else {
if (Math.round(player.x)%CUBESIZE==0) {
playerMovement=0;
}
}
if (playerMovement==0) {
levels[playerRow+dRow][playerCol+dCol]+=4;
levels[playerRow][playerCol]-=4;
if (movingCrate) {
levels[playerRow+2*dRow][playerCol+2*dCol]+=3;
if (levels[playerRow+2*dRow][playerCol+2*dCol]==5) {
movingCrate.cubeMaterials.bottom=crateTopGoalMaterial;
} else {
movingCrate.cubeMaterials.bottom=crateTopMaterial;
}
levels[playerRow+dRow][playerCol+dCol]-=3;
movingCrate.name="crate_"+(playerRow+2*dRow)+"_"+(playerCol+2*dCol);
movingCrate=null;
}
playerRow+=dRow;
playerCol+=dCol;
}
}
// 摄像机管理
theCamera.target=cameraTarget;
theCamera.positionOffset=new Vector3D(0,CUBESIZE*8,CUBESIZE*6);
theCamera.lookOffset=new Vector3D(0,0,-2*CUBESIZE);
theCamera.damping=10;
theCamera.view;
view.render();
}
}
}