Creating a (for example:) “player”-class [solved]

Software: Away3D 4.x

Albert, Newbie
Posted: 07 December 2011 10:44 PM   Total Posts: 16

Hi,
I’ve finally learnt how to add an model from the main class using the AssetLibrary, and it works great! Now I want to be able to take use of the object orientation in as3.

I want to create a “player”-class. I don’t know if it’s right, but I made it extend the Mesh-class. But what should I do then? Because this code doesn’t show anything:

(This code is simplyfied) + (I’m using the latest version from github, not broomstick)

[IMPORTS]
 
public class Player extends Mesh 
{
 
private var _material:BitmapMaterial;
 private var 
_lights:Array = new Array();
  
 public function 
Player(lights:Array) 
 
{
  _lights 
lights;
   
  
initMaterial();
  
initMesh();
   
  
super(_material);
 
}
  
 
private function initMaterial():void
 {
  _material 
= new BitmapMaterial(new E._texture().bitmapData);
  
_material.lights _lights;
  
_material.ambient 0.2;
 
}
  
 
private function initMesh():void
 {
  AssetLibrary
.addEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);
  
AssetLibrary.loadData(new Embeds._player_mesh_3ds());
 
}
 
 
//Not sure about this:
 
private function onAssetComplete(e:AssetEvent):void
 {
  
if (e.asset.assetType == AssetType.GEOMETRY)
  
{
   this
._geometry e.asset as Geometry;
   
this.geometry.scale(100);
  
}
 }

Thanks for helping! grin

   

Avatar
mrpinc, Sr. Member
Posted: 08 December 2011 12:58 AM   Total Posts: 119   [ # 1 ]

Genrally you want to avoid inheritance unless you really need to.  Your player should HAVE a mesh not BE a mesh.  But either way this is not your problem,  the reason you can’t see the player is because the player needs to be added to the scene.

var p:Player = new Player(_lights);

scene.addChild(p); 
   

Albert, Newbie
Posted: 08 December 2011 06:35 AM   Total Posts: 16   [ # 2 ]

Thanks. But shouldn’t my “player”-class extend anything at all? I’m so used to extending something after using a 2d library. But if I’m not extending a mesh, how should I then add it to the view? Are there a example code that shows how this should be done?

   

inSertCodE, Sr. Member
Posted: 08 December 2011 01:31 PM   Total Posts: 105   [ # 3 ]

I would use (extended) object container and load the mesh in it.

   

Albert, Newbie
Posted: 08 December 2011 01:40 PM   Total Posts: 16   [ # 4 ]

Yes, I tested that and it seems logical. But how do I add event listeners, because when I add them inside the class they just wont work?

   

inSertCodE, Sr. Member
Posted: 08 December 2011 02:04 PM   Total Posts: 105   [ # 5 ]

Depends on what you want to listen to… From my experience the main problem you can run up to with event listeners is creating them before the thing you want to listen still exist.
For example I have a class which adds a listener to the parent of the object. However when I assign the class to a variable it immediately tried to add the listener. What I did was I added a check if my class has parent (if the object is added to the stage) and I add the listeners AFTER the object is added to the stage.

So the main rule is that you make sure the things you want to add listeners to exist at the moment you first call up your class.

   

Albert, Newbie
Posted: 08 December 2011 02:15 PM   Total Posts: 16   [ # 6 ]

Well, I have these two classes:

public class AObject extends ObjectContainer3D
{
 
private var _material:BitmapMaterial;
 private var 
_mesh:Mesh;
 private var 
_scale:Number;
  
 public function 
AObject(model:Class = nullmaterial:BitmapMaterial nullscale:Number 100
 
{
  _material 
material;
  
_scale scale;
  
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);
  
AssetLibrary.loadData(model);
 
}
 
 
private function onAssetComplete(e:AssetEvent):void
 {
  _mesh 
= new Mesh(_materiale.asset as Geometry);
  
_mesh.geometry.scale(_scale);
  
this.addChild(_mesh);
 
}
public class Player extends AObject 
{
 
private var _material:BitmapMaterial;
  
 public function 
Player(lights:Array) 
 
{
  _material 
= new BitmapMaterial(new Embeds._texture().bitmapData);
  
_material.lights lights;
   
  
super(Embeds._player_mesh_3ds_material100);
   
  
this.addEventListener(Event.ENTER_FRAMEonEnterFrame);
 
}
  
 
private function onEnterFrame(e:Event):void
 {
  rotationY 
+= 10;
  
trace(".");
 
}

There is no rotation or no dots, but it seems as if the object I add the listener to exists.

   

Avatar
mrpinc, Sr. Member
Posted: 08 December 2011 03:10 PM   Total Posts: 119   [ # 7 ]

ObjectContainer3D might not implement ENTER_FRAME events.  Try adding that event to a sprite and then manipulating the player object you created.

var p:Player = new Player(_lights);

scene.addChild(p);

addEventListener(Event.ENTER_FRAMEonEnterFrame);

function 
onEnterFrame(e:Event):void
{
     p
.rotationY += 10;
   

Albert, Newbie
Posted: 08 December 2011 05:48 PM   Total Posts: 16   [ # 8 ]

But then what’s the reason with having a player class? Is there seriously no way to use away3d in a comfortable way? How do you do this? Please tell.

   

Avatar
mrpinc, Sr. Member
Posted: 08 December 2011 06:21 PM   Total Posts: 119   [ # 9 ]

I can’t tell you why you would have a player class - that up to you and your application and it’s requirements.  Your player class should contain the properties and methods that your Player should have.

If you absouloutely need ENTER_FRAME type functionality inside of your player class then use the Timer class it does the same thing.

   

Albert, Newbie
Posted: 08 December 2011 06:30 PM   Total Posts: 16   [ # 10 ]

When making 2d games you use event listeners often inside a Sprite or MovieClip class. Why wouldn’t it be the same in a 3d game?

   

Avatar
mrpinc, Sr. Member
Posted: 08 December 2011 07:26 PM   Total Posts: 119   [ # 11 ]

Flash player internally uses a timer to dispatch the ENTER_FRAME event every time Flash Player renders a frame. 

Away3D’s ObjectContainer3D does not inherit from sprite so does not dispatch this event.  Away 3D does not implement it’s own version of the event either (which is a good thing)

As I stated above if you absolutely need the ENTER_FRAME functionality inside of your player class - or whatever class - then user a Timer object.  Examples are clearly detailed in the Adobe documentation.

   

Albert, Newbie
Posted: 08 December 2011 08:05 PM   Total Posts: 16   [ # 12 ]

Ok, thanks for the help. But how do you check for keyboard and mouse events?

   

Avatar
mrpinc, Sr. Member
Posted: 08 December 2011 08:51 PM   Total Posts: 119   [ # 13 ]

Please look at the tutorials here:

http://www.allforthecode.co.uk/aftc/forum/user/modules/forum/welcome.php?index=4

They include all the functionality you have asked for.

   

Albert, Newbie
Posted: 08 December 2011 09:27 PM   Total Posts: 16   [ # 14 ]

Thank you for all help!

   

Delfeld, Newbie
Posted: 09 December 2011 04:16 AM   Total Posts: 16   [ # 15 ]

I know I am late in this discussion, but there is a very good reason to not have the player-class inherit anything: code coherence.

I think it would be helpful to look into things like MVC programming patterns.  Java is a pretty good starting point, since a lot of examples and explanations are easy to find.  Once you understand why MVC (and other) patterns exist, then apply that knowledge to Away3D coding.

 

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X