飞机

来源:互联网 发布:成本报表数据分析 编辑:程序博客网 时间:2024/04/26 03:41

public class MainActivity extends BaseGameActivity {

 private static final int CAMERA_WIDTH =800;
 private static final int CAMERA_HEIGHT =480;
 private RepeatingSpriteBackgroundbackground;
 private Camera mCamera;
 private TiledTextureRegionmFaceTextureRegion;
 private Font mfont,autofont;
 Text textCenter,autotext;
 Boolean is=false;
 Sprite face;
 @Override
 public EngineOptions onCreateEngineOptions(){
  // TODO 自动生成的方法存根
  mCamera = newCamera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
  EngineOptions mEngineOptions =new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
    newRatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
  
  return mEngineOptions;
 }
 @Override
 public voidonCreateResources(OnCreateResourcesCallbackpOnCreateResourcesCallback)
   throwsException {
  // TODO 自动生成的方法存根
  this.background = newRepeatingSpriteBackground(CAMERA_WIDTH,CAMERA_HEIGHT, 
               getTextureManager(),AssetBitmapTextureAtlasSource.create( 
       this.getAssets(), "background.png"), 
       getVertexBufferObjectManager());  
  BitmapTextureAtlas mTexture =new BitmapTextureAtlas(getTextureManager(),
    400,118,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  mFaceTextureRegion =BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture,
    this,"face.png",0 ,0,3,1);//2列1行
  mTexture.load();
  BitmapTextureAtlasmfonttexture=new BitmapTextureAtlas(
    getTextureManager(),256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
   mfont=newFont(getFontManager(),mfonttexture,
    Typeface.create(Typeface.DEFAULT,Typeface.BOLD),20,true,Color.RED);
  mfonttexture.load();
  mfont.load();
  BitmapTextureAtlasautofonttexture=new BitmapTextureAtlas(
    getTextureManager(),256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
   autofont=newFont(getFontManager(),autofonttexture,
    Typeface.create(Typeface.DEFAULT,Typeface.BOLD),20,true,Color.RED);
  autofonttexture.load();
  autofont.load();
  pOnCreateResourcesCallback.onCreateResourcesFinished();
  
 }
 @Override
 public void onCreateScene(OnCreateSceneCallbackpOnCreateSceneCallback) throws Exception {
  // TODO 自动生成的方法存根
  Scene scene=new Scene();
  scene.setBackground(background); 
   final Dong as=newDong(20,
    100,mFaceTextureRegion, getVertexBufferObjectManager());
  textCenter = new Text(10, 10,mfont,
    "Velocity_X:\nVelocity_Y:\n"+
    "Accleration_X:\nAccleration_Y:",getVertexBufferObjectManager());
  autotext=new Text(110, 10,autofont, "Velocity_X:\nVelocity_Y:\n"+
    "Accleration_X:\n  Accleration_Y:   ",getVertexBufferObjectManager());
 //as.move();
 scene.setOnSceneTouchListener(as); 
 scene.attachChild(textCenter);
 scene.attachChild(autotext);
 scene.registerUpdateHandler(new IUpdateHandler(){
  
  @Override
  public void reset() {
   // TODO自动生成的方法存根
   
  }
  
  @Override
  public void onUpdate(floatarg0) {
   // TODO自动生成的方法存根
   autotext.setText(String.valueOf(as.mvel)+"\n"+
   String.valueOf(as.mvely)+"\n   "+String.valueOf(as.AccelerationX)+"\n  "+
   String.valueOf(as.AccelerationY)
  );
  }
 });
 scene.attachChild(as);
 pOnCreateSceneCallback.onCreateSceneFinished(scene);
 }
 
 
 @Override
 public void onPopulateScene(Scene pScene,
   OnPopulateSceneCallbackpOnPopulateSceneCallback)
   throwsException {
  // TODO 自动生成的方法存根
  pOnPopulateSceneCallback.onPopulateSceneFinished();
 }
 
 public class Dong extends AnimatedSpriteimplements IOnSceneTouchListener{
    privatePhysicsHandler mphy;
    privatefloat mvel=200;
    privatefloat mvely=200;
    privatestatic final int maxvx=500;
    privatestatic final int maxvy=500;
float AccelerationX,AccelerationY;
  public Dong(int pX, int pY,TiledTextureRegion mFaceTextureRegion,
    VertexBufferObjectManagervertexBufferObjectManager) {
   super(pX, pY,mFaceTextureRegion, vertexBufferObjectManager);
   this.mphy=newPhysicsHandler(this);  
   registerUpdateHandler(mphy);
   // TODO自动生成的构造函数存根
  }

  @Override
 protected void onManagedUpdate(floatpSecondsElapsed){
   mvely=mphy.getVelocityY();
   mvel=mphy.getVelocityX();
   if(Math.abs(mphy.getVelocityX())>=maxvx){
    mphy.setVelocity(maxvx*Math.signum(mphy.getVelocityX()),mphy.getVelocityY());
   }
   if(Math.abs(mphy.getVelocityY())>=maxvy){
    mphy.setVelocity(mphy.getVelocityX(),maxvy*Math.signum(mphy.getVelocityY()));
   }
   if(this.getX()<=0){
    mvel=-mvel;
    this.mphy.setAcceleration(-this.mphy.getAccelerationX(),this.mphy.getAccelerationY());
    this.mphy.setVelocity(mvel,mvely);
    setFlippedHorizontal(false);
   }elseif(this.getX()+getWidth()>=CAMERA_WIDTH){
    mvel=-mvel;
    this.mphy.setAcceleration(-this.mphy.getAccelerationX(),this.mphy.getAccelerationY());
    this.mphy.setVelocity(mvel,mvely);
    setFlippedHorizontal(true);
   }elseif(this.getY()<=0){
    this.mphy.setAcceleration(this.mphy.getAccelerationX(),-this.mphy.getAccelerationY());
    this.mphy.setVelocity(mvel,-mvely);
   }else if(this.getY()+getHeight()>=CAMERA_HEIGHT){
    this.mphy.setAcceleration(this.mphy.getAccelerationX(),-this.mphy.getAccelerationY());
    this.mphy.setVelocity(mvel,-mvely);
       
   super.onManagedUpdate(pSecondsElapsed);
  }
  
  private void move(float f,float g) {
   // TODO自动生成的方法存根
   
   animate(newlong[]{100,100,100},0,2,true);
   AccelerationX=f;AccelerationY=g;
//   if(Math.abs(ax)>maxvxx){
//    ax=maxvxx*Math.signum(ax);
//   }
//   if(Math.abs(ay)>maxvyy){
//    ay=maxvyy*Math.signum(ay);
//   }
   mphy.setAcceleration(AccelerationX,AccelerationY);
  }
  
  @Override
  public booleanonSceneTouchEvent(Scene pScene,TouchEvent pSceneTouchEvent) {
   // TODO自动生成的方法存根
   floattouchX=pSceneTouchEvent.getX();
   floattouchY=pSceneTouchEvent.getY();
   floathelix=getX();
   floatheliy=getY();
   move((touchX-helix)/25,(touchY-heliy)/15);
   returntrue;
  }
   
  
 }

 
}

0 0