AndEngine_example2.1 运动的球


现在的SimpleBaseGameActivity

和1.x一样, 先onCreateEngineOptions()设置Engine, 再onCreateResources()读取资源, 最后onCreateScene()来创建Scene

【不理解先看1.1】

 

先onCreateResources()读取资源

 这张图片是2个图片合成,以后如果需要, 可以切成2张来使用(或者拼凑成动画)

 

这里是用的TiledTextureRegion 类, 相当于 很多TextureRegion组成的数组  【类似 libgdx中的  TextureRegion[][]  】

 

@Override
protected void onCreateResources() {
	// TODO Auto-generated method stub
	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/face/");//设置对应的目录
	this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 32, TextureOptions.BILINEAR);
	this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 0, 2, 1);
	this.mBitmapTextureAtlas.load();
}


再将Ball放入Scene中

我这里写了3个Ball 分别是 Ball, Ball2, Ball3,

其实没有什么区别, 只是显示的图像不一样罢了。

 

@Override
protected Scene onCreateScene() {
	// TODO Auto-generated method stub
	final Scene scene = new Scene();
	scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

	final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
	final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
	final Ball ball = new Ball(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
	final Ball2 ball2 = new Ball2(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
	ball2.animate(new long[]{200, 200}, 0, 1, true);
	final Ball3 ball3 = new Ball3(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
	scene.attachChild(ball);
	scene.attachChild(ball2);
	scene.attachChild(ball3);

	return scene;
}


 

对应的3个Ball的类

 

第一个Ball 只动

private static class Ball extends AnimatedSprite {
	private final PhysicsHandler mPhysicsHandler;

	public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
		this.mPhysicsHandler = new PhysicsHandler(this);
		this.registerUpdateHandler(this.mPhysicsHandler);
		this.mPhysicsHandler.setVelocity(DEMO_VELOCITY, DEMO_VELOCITY);//设置最初的速度
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		//如果到屏幕左边的时候,设置速度为 +100
		//如果到屏幕的右边,       设置速度为 -100
		if(this.mX < 0) {
			this.mPhysicsHandler.setVelocityX(MoveBallEE_2_1.DEMO_VELOCITY);
		} else if(this.mX + this.getWidth() > MoveBallEE_2_1.CAMERA_WIDTH) {
			this.mPhysicsHandler.setVelocityX(-MoveBallEE_2_1.DEMO_VELOCITY);
		}
		//如果到屏幕上边     ,设置速度为 +100
		//如果到屏幕下边,       设置速度为 -100
		if(this.mY < 0) {
			this.mPhysicsHandler.setVelocityY(MoveBallEE_2_1.DEMO_VELOCITY*2);
		} else if(this.mY + this.getHeight() > MoveBallEE_2_1.CAMERA_HEIGHT) {
			this.mPhysicsHandler.setVelocityY(-MoveBallEE_2_1.DEMO_VELOCITY*2);
		}

		super.onManagedUpdate(pSecondsElapsed);
	}
}


 

第二个Ball2, 每0.2秒变换一下头像

private static class Ball2 extends AnimatedSprite {
	private final PhysicsHandler mPhysicsHandler;

	public Ball2(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
		this.mPhysicsHandler = new PhysicsHandler(this);
		this.registerUpdateHandler(this.mPhysicsHandler);
		this.mPhysicsHandler.setVelocity(DEMO_VELOCITY*5, DEMO_VELOCITY);//设置最初的速度
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		//如果到屏幕左边的时候,设置速度为 +100
		//如果到屏幕的右边,       设置速度为 -100
		if(this.mX < 0) {
			this.mPhysicsHandler.setVelocityX(MoveBallEE_2_1.DEMO_VELOCITY);
		} else if(this.mX + this.getWidth() > MoveBallEE_2_1.CAMERA_WIDTH) {
			this.mPhysicsHandler.setVelocityX(-MoveBallEE_2_1.DEMO_VELOCITY);
		}
		//如果到屏幕上边     ,设置速度为 +100
		//如果到屏幕下边,       设置速度为 -100
		if(this.mY < 0) {
			this.mPhysicsHandler.setVelocityY(MoveBallEE_2_1.DEMO_VELOCITY);
		} else if(this.mY + this.getHeight() > MoveBallEE_2_1.CAMERA_HEIGHT) {
			this.mPhysicsHandler.setVelocityY(-MoveBallEE_2_1.DEMO_VELOCITY);
		}

		super.onManagedUpdate(pSecondsElapsed);
	}
}


第3个Ball3, 每次撞击边缘后, 改变头像

 

private static class Ball3 extends AnimatedSprite {
	private final PhysicsHandler mPhysicsHandler;

	public Ball3(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
		this.mPhysicsHandler = new PhysicsHandler(this);
		this.registerUpdateHandler(this.mPhysicsHandler);
		this.mPhysicsHandler.setVelocity(DEMO_VELOCITY, DEMO_VELOCITY*3);//设置最初的速度
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		//如果到屏幕左边的时候,设置速度为 +100
		//如果到屏幕的右边,       设置速度为 -100
		if(this.mX < 0) {
			this.mPhysicsHandler.setVelocityX(MoveBallEE_2_1.DEMO_VELOCITY*5);
			changeTileState();
		} else if(this.mX + this.getWidth() > MoveBallEE_2_1.CAMERA_WIDTH) {
			this.mPhysicsHandler.setVelocityX(-MoveBallEE_2_1.DEMO_VELOCITY);
			changeTileState();
		}
		//如果到屏幕上边     ,设置速度为 +100
		//如果到屏幕下边,       设置速度为 -100
		if(this.mY < 0) {
			this.mPhysicsHandler.setVelocityY(MoveBallEE_2_1.DEMO_VELOCITY*5);
			changeTileState();
		} else if(this.mY + this.getHeight() > MoveBallEE_2_1.CAMERA_HEIGHT) {
			this.mPhysicsHandler.setVelocityY(-MoveBallEE_2_1.DEMO_VELOCITY);
			changeTileState();
		}

		super.onManagedUpdate(pSecondsElapsed);
	}
	private void changeTileState(){
		if(this.getCurrentTileIndex() == 0){
			this.setCurrentTileIndex(1);
		}else{
			this.setCurrentTileIndex(0);
		}
	}
}


 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值