AndEngine学习【一】

主要是理解了生命周期和加载图片,创建精灵,注释还算详细就直接贴代码了:

注:图片是放在assets里的

在真机上运行的,运行图片就不发了,效果就是小球在屏幕上运动

package com.game.moveball;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;
import android.util.Log;

public class MoveBallActivity extends BaseGameActivity {

    private static final int CAMERA_WIDTH = 800;
    private static final int CAMERA_HEIGHT = 480;
    private final static float BALL_VELOCITY = 100f;// 球的移动速度

    private Camera mCamera;
    private Scene mScene;
    private RepeatingSpriteBackground background;
    private TiledTextureRegion mFaceTextureRegion;

        /**
         * 负责产生一个镜头和加载一个引擎实体。
         * 记住,你后面在手机上看到的东西都是透过镜头看到的,
         * 镜头指定了游戏的解析率,无论你的手机屏幕分辨率多少,引擎都会按照自己指定的分辨率去重造你的屏幕。
         * 设定好镜头以后返回一个以此镜头为标准的引擎实体
         */
    @Override
    public EngineOptions onCreateEngineOptions() {
	// 创建一个摄像头,设置屏幕是800宽,480高
	mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
	// 创建游戏引擎
	// 引擎参数1.全屏否2.横屏还是竖屏3.屏幕分辨率4.引擎用的摄像头
	EngineOptions mEngineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);

	return mEngineOptions;
    }
        /**
         * 加载资源。
         * 其实加载资源多的工作可以放到onLoadScene()里面完成,
         * 但是作者既然写了这么个函数,我猜他就是想让代码层次清晰一些,
         * 在这里面你可以加载你需要的资源,以便后面加载场景的时候使用。
         */
    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
	// 此处读取游戏资源图片
	this.background = new RepeatingSpriteBackground(CAMERA_WIDTH,CAMERA_HEIGHT, getTextureManager(),AssetBitmapTextureAtlasSource.create(this.getAssets(),"bg_game.png"), getVertexBufferObjectManager());
	
	//创建一个Texture对象,这个对象的大小是32×64个像素,用来存储将要加载的图片
	BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(), 32, 64,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
	
	/**
	 * 参数说明:
	 * mTexure是在内存中放置贴图资源用的,64,32是图片要求的宽和高,必须是2的n次方大小.如:2,4,8,16,32,64,
	 * 128,512,1024.... 并且要比原图的宽高要大
	 * mFaceTextureRegion相当于从mTexure中扣图,因为mTexure是由很多图集组成的,要从中截取一片出来
	 * 0,0代表截图的top,right坐标(起点坐标),1和2分别代表贴图中一张存在1列2行
	 * 
	 */
	mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "hole_goal.png", 0,0, 1,2);

	// 最后一定要调用这个方法,否则图片不会加载上来 
	mTexture.load();
	
	// 最后一定要调用,通知程序可以开始调用onCreateScene方法 
	pOnCreateResourcesCallback.onCreateResourcesFinished();
    }
        /** 加载场景。有了镜头,就应该由场景吧,导演一般都是这么想的。
         * 这时候你需要创建一个场景,场景里加载刚才的资源,最后将场景返回,最基本的游戏就可以起来了,
         * 此时游戏是没有规则的,但是程序已经可以启动,画面里只有你加载的资源。
         */
    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
	//创建场景
	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);
    }
        /**
         *  这个函数是Option选项,写在其中的代码将在场景加载完毕后执行,
         *  相当于善后工作。
         */
    @Override
    public void onPopulateScene(Scene pScene,OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {

	pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

        /**
         *  Sprite:游戏中的精灵,表示游戏中的角色
         *  AnimatedSprite:Sprite的子类,可以支持动画效果的Sprite。
         */
    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);
	
	    mX = 100;
	    mY = 100;
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
	

	    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;
	}

    }

}


背景:AndEngine学习【一】_第1张图片小球:


你可能感兴趣的:(AndEngine)