在Andengine中创建游戏对象类的正确方法

时间:2015-08-18 16:17:57

标签: android andengine

我只是想知道,如果你想为游戏创建一个对象,那就说一个球,

你是否应该创建一个名为ball的类,它包含sprite和body?,如:

 public class Ball {

   BaseGameActivity mActivity;
   Sprite s;
   Body mBody
      public Ball(TextureRegion texture,BaseGameActivity mActivity){
          this.mActivity=mActivity;
          s= new Sprite (0,0,texture,mActivity.getVertexBufferObjectManager());
          mBody=PhysicsFactory.createBoxBody(mActivity.mWorld,s, BodyDef.BodyType.StaticBody,PhysicsFactory.createFixtureDef(1,0,2));

          //then it shoud be properly attached to scene and world etc...

      }
  }

扩展精灵的类?像:

 public class Ball extends Sprite {

   BaseGameActivity mActivity;
   Body mBody
      public Ball(TextureRegion texture,BaseGameActivity mActivity){
          super (0,0,texture,mActivity.getVertexBufferObjectManager());
          this.mActivity=mActivity;
          mBody=PhysicsFactory.createBoxBody(mActivity.mWorld,this, BodyDef.BodyType.StaticBody,PhysicsFactory.createFixtureDef(1,0,2));

          //then it shoud be properly attached to scene and world etc...

      }
  }

还是另一种方式?

哪个最快?哪个更容易?或者方式没关系?

提前致谢

2 个答案:

答案 0 :(得分:0)

就这样做..你可能需要其他的精灵和其他东西......

public class Ball extends BaseGameActivity{

//constructor and other methods

BitmapTextureAtlas ballAtlas;

ITextureRegion ballTexture;

//in onLoadResources load ur atlas

//create texture region

//then in onCreateScene create a scene

//simply attach ur sprite ball to ur scene

}

你可以在任何地方创建精灵..你不必为那个

创建单独的类

答案 1 :(得分:0)

我认为您可以使用这两种方法,这取决于您和您的编码风格/应用程序基础架构。 :)

Altough我使用一个通用的SceneManager类来引用活动,而不是多个活动