[AndEngine学习教程] 第2节 使用AndEngine.jar创建工程(二)

2014-11-24 10:09:02 · 作者: · 浏览: 1
LPHA);
mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "face_circle_tiled.png", 0, 0,2,1);

/**
* 参数说明:
* mTexure是在内存中放置贴图资源用的,64,32是图片要求的宽和高,必须是2的n次方大小.如:2,4,8,16,32,64,128,512,1024....
* 并且要比原图的宽高要大
*
* mFaceTextureRegion相当于从mTexure中扣图,因为mTexure是由很多图集组成的,要从中截取一片出来
* 0,0代表截图的top,right坐标(起点坐标),2和1分别代表贴图中一张存在2列1行
*
*/
mTexture.load();

pOnCreateResourcesCallback.onCreateResourcesFinished();
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
// TODO Auto-generated method stub
mScene = new Scene();
mScene.setBackground(background);

final float centerX = (CAMERA_WIDTH - mFaceTextureRegion.getWidth()) / 2;//计算贴图的中心坐标
final float centerY = (CAMERA_HEIGHT - mFaceTextureRegion.getHeight()) / 2;
final Ball mBall = new Ball(centerX, centerY,32, 32,this.mFaceTextureRegion,getVertexBufferObjectManager());

mScene.attachChild(mBall);
pOnCreateSceneCallback.onCreateSceneFinished(mScene);
}

@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
// TODO Auto-generated method stub



pOnPopulateSceneCallback.onPopulateSceneFinished();
}


private static class Ball extends AnimatedSprite{


float mVelocityX = BALL_VELOCITY;//球的x方向速度
float mVelocityY = BALL_VELOCITY ;//球的y方向速度

public Ball(float pX, float pY, float pWidth, float pHeight,
ITiledTextureRegion pTiledTextureRegion,
VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager);
// TODO Auto-generated constructor stub
mX = 100;
mY = 100;
}

@Override
protected void onManagedUpdate(float pSecondsElapsed) {
// TODO Auto-generated method stub

if(this.mX < 0) {
setVelocityX(BALL_VELOCITY);
} else if( this.mX + this.getWidth() > CAMERA_WIDTH){
setVelocityX(-BALL_VELOCITY);

}

if(this.mY < 0 ) {
setVelocityY(BALL_VELOCITY);
} else if(this.mY + this.getHeight() > CAMERA_HEIGHT){
setVelocityY(-BALL_VELOCITY);
}



mX += mVelocityX * pSecondsElapsed;
mY += mVelocityY * pSecondsElapsed;


this.setPosition(mX, mY);
Log.d("Season",pSecondsElapsed + "");

super.onManagedUpdate(pSecondsElapsed);


}

void setVelocityX(float vx){

mVelocityX = vx;
}


void setVelocityY(float vy){
mVelocityY = vy;
}