Minecraft Player updated for alternativa 8

David Jones
@david3jones
avatar-davidejones

I haven’t had much time to experiment with the new updates coming from alternativa3d, what with moving to another country and the various projects i’m involved with i just haven’t had time. Now i’ve started to settle in, it seems like a good a time as any to get back on and try some things out. First up i thought i would ease back in and just update my flash minecraft player to alternativa version 8.17 making use of the gpu support in flash player 11 and also trying out the new syntax and class changes. It turned out pretty well but I was a little disappointed at not being able to recreate the pixel like effect the textures have last time. For some reason in version 8 the textures are smoothed without a way to change it, perhaps its just a result of using the gpu I’m not sure, either way its still faster and using less resources than the previous version. minecraft player This time I separated the minecraft player into a separate “Player” class just so its a tiny bit cleaner. Here is the Character.as class, note that the new version 8 is slightly different in that it calls upon the stage3d and uploads resources to it to make use of the gpu rendering i mentioned earlier.

package 
{	
    	import alternativa.engine3d.controllers.SimpleObjectController;
    	import alternativa.engine3d.core.Camera3D;
    	import alternativa.engine3d.core.Object3D;
    	import alternativa.engine3d.core.Resource;
    	import alternativa.engine3d.resources.Geometry;
    	import alternativa.engine3d.core.View;
    	
    	import flash.display.Stage3D;
    	import flash.display.Sprite;
    	import flash.display.StageAlign;
    	import flash.display.StageScaleMode;
    	import flash.display.StageQuality;
    	import flash.events.Event;
    	import flash.events.ProgressEvent;
    	import flash.display.BitmapData;
    	import flash.geom.Point;
    	import flash.geom.Rectangle;
    	
    	import Player;
    	
    	[SWF(backgroundColor="#000000", frameRate="30", width="800", height="600")]
    	
    	public class Character extends Sprite 
    	{
    		[Embed(source="char.png")] static private const CharSkin2:Class;
    		
    		private var scene:Object3D = new Object3D();
    		private var camera:Camera3D;
    		private var controller:SimpleObjectController;
    		private var stage3D:Stage3D;
    		private var player:Player;
    		
    		public function Character():void
    		{
    			stage.align = StageAlign.TOP_LEFT;
    			stage.scaleMode = StageScaleMode.NO_SCALE;
    			
    			camera = new Camera3D(1, 1000);
    			camera.view = new View(stage.stageWidth, stage.stageHeight, false, 0, 0, 4);
    			addChild(camera.view);
    			addChild(camera.diagram);
    			
    			camera.rotationX = -90*Math.PI/180;
    			camera.y = -60;
    			camera.z = 8;
    			controller = new SimpleObjectController(stage, camera, 50);
    			scene.addChild(camera);
    			
    			stage3D = stage.stage3Ds[0];
    			stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
    			stage3D.requestContext3D();
    		}
    		
    		private function onContextCreate(e:Event):void 
    		{
    			stage3D.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
    			
    			addPlayer();
    			
    			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    			stage.addEventListener(Event.RESIZE, onResize);
    		}
    		
    		private function addPlayer():void
    		{
    			player = new Player(new CharSkin2().bitmapData, stage3D);
    			scene.addChild(player);
    			uploadResources(player.getResources(true));
    			
    			/*
    			for each (var resource:Resource in scene.getResources(true)) {
    				resource.upload(stage3D.context3D);
    			}
    			*/
    		}
    		
    		private function uploadResources(resources:Vector.):void {
    			for each (var resource:Resource in resources) {
    				resource.upload(stage3D.context3D);
    			}
    		}
    
    		private function onEnterFrame(e:Event = null):void {
    			controller.update();
    			player.rotationZ -= 0.05;
    			camera.render(stage3D);
    		}
    		
    		private function onResize(e:Event = null):void {
    			camera.view.width = stage.stageWidth;
    			camera.view.height = stage.stageHeight;
    			onEnterFrame();
    		}
    	}
}

Here is the Player.as class

package
{
    	import alternativa.engine3d.core.Object3D;
    	import alternativa.engine3d.objects.Surface;
    	import alternativa.engine3d.primitives.Box;
    	import alternativa.engine3d.materials.TextureMaterial;
    	import alternativa.engine3d.materials.FillMaterial;
    	import alternativa.engine3d.resources.BitmapTextureResource;
    	
    	import flash.display.Stage3D;
    	import flash.display.BitmapData;
    	import flash.geom.Point;
    	import flash.geom.Rectangle;
    
    	public class Player extends Object3D
    	{
    		private var stage3D:Stage3D;
    		private var bmd:BitmapData;
    		private var head:Box;
    		private var headE:Box;
    		private var body:Box;
    		private var legL:Box;
    		private var legR:Box;
    		private var armL:Box;
    		private var armR:Box;
    		private var mat:FillMaterial = new FillMaterial(0xFF0000);
    	
    		public function Player(bmd:BitmapData,stage3D:Stage3D):void
    		{
    			this.bmd = bmd;
    			this.stage3D = stage3D;
    						
    			// Create Head
    			head = new Box(8, 8, 8, 1, 1, 1);
    			head.z = 8;
    			
    			// Create Head Outer Box
    			headE = new Box(8.9, 8.9, 8.9, 1, 1, 1);
    			headE.z = 8;
    			
    			// Create Body
    			body = new Box(8, 4, 12, 1, 1, 1);
    			body.z = -2;
    			
    			// Create Left Leg
    			legL = new Box(4, 4, 12, 1, 1, 1);
    			legL.z = -14;
    			legL.x = 2;
    			
    			// Create Right Leg
    			legR = new Box(4, 4, 12, 1, 1, 1);
    			legR.z = -14;
    			legR.x = -2;
    			
    			// Create Left Arm
    			armL = new Box(4, 4, 12, 1, 1, 1);
    			armL.z = -2;
    			armL.x = 6;
    			
    			// Create Right Arm
    			armR = new Box(4, 4, 12, 1, 1, 1);
    			armR.z = -2;
    			armR.x = -6;
    			
    			//create, apply, upload textures
    			createPlayerMaterials();
    			
    			// Add all body parts to the player object
    			addChild(head);
    			addChild(headE);
    			addChild(body);
    			addChild(legL);
    			addChild(legR);
    			addChild(armL);
    			addChild(armR);
    		}
    		
    		private function createPlayerMaterials():void
    		{ 
    			// HEAD
    			
    			var front_head_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(8, 8), 8, 8));
    			var front_head:TextureMaterial = new TextureMaterial(front_head_res);
    			head.addSurface(front_head, 12, 2);
    			//head.geometry.upload(stage3D.context3D);
    			//front_head_res.upload(stage3D.context3D);
    			
    			var back_head_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(24, 8), 8, 8));
    			var back_head:TextureMaterial = new TextureMaterial(back_head_res);
    			head.addSurface(back_head, 18, 2);
    			
    			var left_head_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(16, 8), 8, 8));
    			var left_head:TextureMaterial = new TextureMaterial(left_head_res);
    			head.addSurface(left_head, 24, 2);
    			
    			var right_head_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(0, 8), 8, 8));
    			var right_head:TextureMaterial = new TextureMaterial(right_head_res);
    			head.addSurface(right_head, 30, 2);
    			
    			var top_head_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(8, 0), 8, 8));
    			var top_head:TextureMaterial = new TextureMaterial(top_head_res);
    			head.addSurface(top_head, 6, 2);
    			
    			var bottom_head_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(16, 0), 8, 8));
    			var bottom_head:TextureMaterial = new TextureMaterial(bottom_head_res);
    			head.addSurface(bottom_head, 0, 2);
    			
    			// HEADE
    			var front_heade_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(40, 8), 8, 8));
    			var front_heade:TextureMaterial = new TextureMaterial(front_heade_res);
    			headE.addSurface(front_head, 12, 2);
    			
    			var back_heade_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(56, 8), 8, 8));
    			var back_heade:TextureMaterial = new TextureMaterial(back_heade_res);
    			headE.addSurface(back_head, 18, 2);
    			
    			var left_heade_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(48, 8), 8, 8));
    			var left_heade:TextureMaterial = new TextureMaterial(left_heade_res);
    			headE.addSurface(left_head, 24, 2);
    			
    			var right_heade_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(32, 8), 8, 8));
    			var right_heade:TextureMaterial = new TextureMaterial(right_heade_res);
    			headE.addSurface(right_head, 30, 2);
    			
    			var top_heade_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(40, 0), 8, 8));
    			var top_heade:TextureMaterial = new TextureMaterial(top_heade_res);
    			headE.addSurface(top_head, 6, 2);
    			
    			var bottom_heade_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(48, 0), 8, 8));
    			var bottom_heade:TextureMaterial = new TextureMaterial(bottom_heade_res);
    			headE.addSurface(bottom_head, 0, 2);
    			
    			// BODY
    			var front_body_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(20, 20), 8, 8)); //8 , 12
    			var front_body:TextureMaterial = new TextureMaterial(front_body_res);
    			body.addSurface(front_body, 12, 2);
    			
    			var back_body_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(32, 20), 8, 8)); // 8, 12
    			var back_body:TextureMaterial = new TextureMaterial(back_body_res);
    			body.addSurface(back_body, 18, 2);
    			
    			var left_body_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(29, 20), 4, 8)); // 4, 12
    			var left_body:TextureMaterial = new TextureMaterial(left_body_res);
    			body.addSurface(left_body, 24, 2);
    			
    			var right_body_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(16, 20), 4, 8)); // 4, 12
    			var right_body:TextureMaterial = new TextureMaterial(right_body_res);
    			body.addSurface(right_body, 30, 2);
    			
    			var top_body_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(20, 16), 8, 4)); // 8, 4
    			var top_body:TextureMaterial = new TextureMaterial(top_body_res);
    			body.addSurface(top_body, 6, 2);
    			
    			var bottom_body_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(28, 16), 8, 4)); // 8, 4
    			var bottom_body:TextureMaterial = new TextureMaterial(bottom_body_res); 
    			body.addSurface(bottom_body, 0, 2);
    			
    			// LEGL
    			var front_legL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(4, 20), 4, 8)); //4,12
    			var front_legL:TextureMaterial = new TextureMaterial(front_legL_res);
    			legL.addSurface(front_legL, 12, 2);
    			
    			var back_legL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(12, 20), 4, 8)); //4,12
    			var back_legL:TextureMaterial = new TextureMaterial(back_legL_res);
    			legL.addSurface(back_legL, 18, 2);
    			
    			var left_legL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(8, 20), 4, 8)); //4,12
    			var left_legL:TextureMaterial = new TextureMaterial(left_legL_res);
    			legL.addSurface(left_legL, 24, 2);
    			
    			var right_legL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(0, 20), 4, 8)); //4,12
    			var right_legL:TextureMaterial = new TextureMaterial(right_legL_res);
    			legL.addSurface(right_legL, 30, 2);
    			
    			var top_legL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(4, 16), 4, 4)); //4,4
    			var top_legL:TextureMaterial = new TextureMaterial(top_legL_res);
    			legL.addSurface(top_legL, 6, 2);
    			
    			var bottom_legL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(8, 16), 4, 4)); //4,4
    			var bottom_legL:TextureMaterial = new TextureMaterial(bottom_legL_res);
    			legL.addSurface(bottom_legL, 0, 2);
    			
    			// LEGR
    			var front_legR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(4, 20), 4, 8)); //4,12
    			var front_legR:TextureMaterial = new TextureMaterial(front_legR_res);
    			legR.addSurface(front_legR, 12, 2);
    			
    			var back_legR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(12, 20), 4, 8)); //4,12
    			var back_legR:TextureMaterial = new TextureMaterial(back_legR_res);
    			legR.addSurface(back_legR, 18, 2);
    			
    			var left_legR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(0, 20), 4, 8)); //4,12
    			var left_legR:TextureMaterial = new TextureMaterial(left_legR_res);
    			legR.addSurface(left_legR, 24, 2);
    			
    			var right_legR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(0, 20), 4, 8)); //4,12
    			var right_legR:TextureMaterial = new TextureMaterial(right_legR_res);
    			legR.addSurface(right_legR, 30, 2);
    			
    			var top_legR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(4, 16), 4, 4)); //4,4
    			var top_legR:TextureMaterial = new TextureMaterial(top_legR_res);
    			legR.addSurface(top_legR, 6, 2);
    			
    			var bottom_legR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(8, 16), 4, 4)); //4,4
    			var bottom_legR:TextureMaterial = new TextureMaterial(bottom_legR_res);
    			legR.addSurface(bottom_legR, 0, 2);
    			
    			// ARML
    			var front_armL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(44, 20), 4, 8)); //4,12
    			var front_armL:TextureMaterial = new TextureMaterial(front_armL_res);
    			armL.addSurface(front_armL, 12, 2);
    			
    			var back_armL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(52, 20), 4, 8)); //4,12
    			var back_armL:TextureMaterial = new TextureMaterial(back_armL_res);
    			armL.addSurface(back_armL, 18, 2);
    			
    			var left_armL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(48, 20), 4, 8)); //4,12
    			var left_armL:TextureMaterial = new TextureMaterial(left_armL_res);
    			armL.addSurface(left_armL, 24, 2);
    			
    			var right_armL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(40, 20), 4, 8)); //4,12
    			var right_armL:TextureMaterial = new TextureMaterial(right_armL_res);
    			armL.addSurface(right_armL, 30, 2);
    			
    			var top_armL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(44, 16), 4, 4)); //4,4
    			var top_armL:TextureMaterial = new TextureMaterial(top_armL_res);
    			armL.addSurface(top_armL, 6, 2);
    			
    			var bottom_armL_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(48, 16), 4, 4)); //4,4
    			var bottom_armL:TextureMaterial = new TextureMaterial(bottom_armL_res);
    			armL.addSurface(bottom_armL, 0, 2);
    			
    			//ARMR
    			var front_armR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(44, 20), 4, 8)); //4,12
    			var front_armR:TextureMaterial = new TextureMaterial(front_armR_res);
    			armR.addSurface(front_armR, 12, 2);
    			
    			var back_armR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(52, 20), 4, 8)); //4,12
    			var back_armR:TextureMaterial = new TextureMaterial(back_armR_res);
    			armR.addSurface(back_armR, 18, 2);
    			
    			var left_armR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(40, 20), 4, 8)); //4,12
    			var left_armR:TextureMaterial = new TextureMaterial(left_armR_res);
    			armR.addSurface(left_armR, 24, 2);
    			
    			var right_armR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(48, 20), 4, 8)); //4,12
    			var right_armR:TextureMaterial = new TextureMaterial(right_armR_res);
    			armR.addSurface(right_armR, 30, 2);
    			
    			var top_armR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(44, 16), 4, 4)); //4,4
    			var top_armR:TextureMaterial = new TextureMaterial(top_armR_res);
    			armR.addSurface(top_armR, 6, 2);
    			
    			var bottom_armR_res:BitmapTextureResource = new BitmapTextureResource(cropBitmapData(bmd, new Point(48, 16), 4, 4)); //4,4
    			var bottom_armR:TextureMaterial = new TextureMaterial(bottom_armR_res);
    			armR.addSurface(bottom_armR, 0, 2);
    		}
    		
    		private function cropBitmapData(sourceBitmapData:BitmapData, startPoint:Point, width:Number, height:Number):BitmapData
            {
                var croppedBD:BitmapData = new BitmapData(width, height, true);
                croppedBD.copyPixels(sourceBitmapData, new Rectangle(startPoint.x, startPoint.y, width, height), new Point(0, 0));
                return croppedBD.clone();
                croppedBD.dispose();
    		}
    	}
}

You can download the files here, or view the demo here. Also make sure to download the alternativa3d 8.17.0 swc library from alternativaplatform.com to make use of the download files.

Downloads

Comments

    Comments are currently closed