首页 > 代码库 > AndEngine游戏引擎之BoundCamera
AndEngine游戏引擎之BoundCamera
如何追逐一个玩家,始终让玩家在视野范围之内。
AndEngine引擎中使用BoundCamera类。
步骤
1.构造BoundCamera类,起始坐标,长宽,然后传给engine
2.设置BoundCamera的追踪范围一般范围会比之前的摄像头面积要大 setBounds(minX, maxX,minY, maxY)
3.设置BoundCamera可以追踪
4.给BoundCamera设置追踪的实体,然后BoundCamera就可以追踪这个实体了
下面是示例
package com.example.SpriteTest;import android.util.Log;import android.widget.Toast;import org.anddev.andengine.engine.Engine;import org.anddev.andengine.engine.camera.BoundCamera;import org.anddev.andengine.engine.camera.Camera;import org.anddev.andengine.engine.options.EngineOptions;import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;import org.anddev.andengine.entity.IEntity;import org.anddev.andengine.entity.layer.tiled.tmx.*;import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;import org.anddev.andengine.entity.modifier.*;import org.anddev.andengine.entity.scene.Scene;import org.anddev.andengine.entity.sprite.Sprite;import org.anddev.andengine.input.touch.TouchEvent;import org.anddev.andengine.opengl.texture.Texture;import org.anddev.andengine.opengl.texture.TextureOptions;import org.anddev.andengine.opengl.texture.region.TextureRegion;import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;import org.anddev.andengine.ui.activity.BaseGameActivity;import org.anddev.andengine.util.modifier.IModifier;/** * Created by Administrator on 2014/12/26. */public class TiledMapActivity extends BaseGameActivity{ /*----------------????-------------------------------*/ protected int CAMERA_WIDTH=1280; protected int CAMERA_HEIGHT=720; /*-----------------AndEngine????----------------------------*/ protected Engine mEngine; protected Camera mCamera; protected BoundCamera mBoundCamera; // protected Scene mMainScene; protected TextureRegion spriteRegion; @Override public Engine onl oadEngine() { mBoundCamera=new BoundCamera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT); mCamera=new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT); mEngine=new Engine(new EngineOptions(true, EngineOptions.ScreenOrientation.LANDSCAPE,new FillResolutionPolicy(),mBoundCamera)); return mEngine; } @Override public void onl oadResources() { Texture mTexture=new Texture(32,32, TextureOptions.BILINEAR); spriteRegion= TextureRegionFactory.createFromAsset(mTexture,this,"face_box.png",0,0); mEngine.getTextureManager().loadTexture(mTexture); } @Override public Scene onl oadScene() { final Scene mMainScene=new Scene(1); TMXLoader mTMXloader=new TMXLoader(this,mEngine.getTextureManager(),new TMXLoader.ITMXTilePropertiesListener() { @Override public void onTMXTileWithPropertiesCreated(TMXTiledMap pTMXTiledMap, TMXLayer pTMXLayer, TMXTile pTMXTile, TMXProperties<TMXTileProperty> pTMXTileProperties) { Log.i("Property",pTMXLayer.getName()+": "+pTMXTileProperties.toArray().toString()); Toast.makeText(TiledMapActivity.this,"dgsdsdf",Toast.LENGTH_LONG).show(); } }); try { final TMXTiledMap map=mTMXloader.loadFromAsset(this, "gzip.tmx"); final TMXLayer tmxLayer=map.getTMXLayers().get(0); mMainScene.getFirstChild().attachChild(tmxLayer); Log.i("Property", "高度:"+tmxLayer.getHeight()); //用于限制镜头的大小 最小X,最大X,最小Y,最大Y this.mBoundCamera.setBounds(0, tmxLayer.getWidth(),0, tmxLayer.getHeight()); this.mBoundCamera.setBoundsEnabled(true); mMainScene.setOnSceneTouchListener(new Scene.IOnSceneTouchListener() { @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { switch(pSceneTouchEvent.getAction()) { case TouchEvent.ACTION_DOWN: TMXTile mTile=tmxLayer.getTMXTileAt(pSceneTouchEvent.getX(),pSceneTouchEvent.getY()); StringBuffer sb = new StringBuffer(); sb.append("你点击的瓦片信息如下:"); sb.append("globalId: "+mTile.getGlobalTileID()); sb.append("col:"+mTile.getTileColumn()); sb.append("row:"+mTile.getTileRow()); Log.i("Property",sb.toString()); //瓦片是根据瓦片集中的全局ID来决定的,如果瓦片的ID发生了改变,就会在瓦片集中根据修改后的ID进行更新 mTile.setGlobalTileID(map,1); return true; default: return false; } } }); } catch (TMXLoadException e) { e.printStackTrace(); }catch (IllegalArgumentException ee){ ee.printStackTrace(); Toast.makeText(TiledMapActivity.this,ee.getMessage(),Toast.LENGTH_LONG).show(); } Sprite boy=new Sprite(0,0,spriteRegion); boy.registerEntityModifier(new SequenceEntityModifier(new MoveYModifier(10.0f,0.0f,960.0f-32), new DelayModifier(3.0f), new MoveXModifier(10.0f,0.0f,640-32))); mMainScene.getLastChild().attachChild(boy); //设置需要追逐的实体(chase:追逐,追寻) mBoundCamera.setChaseEntity(boy); return mMainScene; } @Override public void onl oadComplete() { }}
AndEngine游戏引擎之BoundCamera
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。