最近写程序需要用到这类,研究了下,现把成果和大家分享。
NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为方便,
长话短说。
设置通知,就是说要在什么地方(哪个类)接受通知,一般在初始化中做。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"test" object:nil];
我仅对以上参数做以说明:addObserver 这个是观察者,就是说 在什么地方接收通知;
selector 这个是收到通知后,调用何种方法;
name: 这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知,就是说此时要调用观察者处的方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];
我仅对以上参数做以说明:
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
发送通知时,默认调用test方法。
- (void) test:(NSNotification*) notification
{
searchFriendArrary = [notification object];//通过这个获取到传递的对象
}
1.本节要点
制作一个虚拟手柄来控制动画精灵的移动,旋转.
2.新使用到的AndEngine资源
1.AnalogOnScreenControl 这是An的Engine给我们提供的用于虚拟手柄,虚拟控制器方面的基础类,其构造如下:
public AnalogOnScreenControl(final float pX, final float pY, final Camera pCamera, final ITextureRegion pControlBaseTextureRegion, final ITextureRegion pControlKnobTextureRegion, final float pTimeBetweenUpdates, final VertexBufferObjectManager pVertexBufferObjectManager, final IAnalogOnScreenControlListener pAnalogOnScreenControlListener) { super(pX, pY, pCamera, pControlBaseTextureRegion, pControlKnobTextureRegion, pTimeBetweenUpdates, pVertexBufferObjectManager, pAnalogOnScreenControlListener); this.mClickDetector.setEnabled(false); } public AnalogOnScreenControl(final float pX, final float pY, final Camera pCamera, final ITextureRegion pControlBaseTextureRegion, final ITextureRegion pControlKnobTextureRegion, final float pTimeBetweenUpdates, final long pOnControlClickMaximumMilliseconds, final VertexBufferObjectManager pVertexBufferObjectManager, final IAnalogOnScreenControlListener pAnalogOnScreenControlListener) { super(pX, pY, pCamera, pControlBaseTextureRegion, pControlKnobTextureRegion, pTimeBetweenUpdates, pVertexBufferObjectManager, pAnalogOnScreenControlListener); this.mClickDetector.setTriggerClickMaximumMilliseconds(pOnControlClickMaximumMilliseconds); }
2.参数说明:
pX,pY是控制器的左上角放置坐标;
pCamera是我们在场景中使用到的Camera;
pControlBaseTextureRegion相当于控制器的背景
pControlKnobTextureRegion相当于控制器的指针图标
pTimeBetweenUpdates指控制器的重绘刷新时间间隔,单位是秒
pVertexBufferObjectManager直接使用getVertxeBufferObjectManager()传进去就可以了
pAnalogOnScreenControlListener是控制器实现自动监听的接口,主要用来监听控制器的位置变化,
然后将相应的变化转化为一组通用的数据(-1到1之间),接口源代码为:
public interface IAnalogOnScreenControlListener extends IOnScreenControlListener { // =========================================================== // Constants // =========================================================== // =========================================================== // Methods // =========================================================== public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl); }
public static interface IOnScreenControlListener { // =========================================================== // Constants // =========================================================== // =========================================================== // Methods // =========================================================== /** * @param pBaseOnScreenControl * @param pValueX between <code>-1</code> (left) to <code>1</code> (right). * @param pValueY between <code>-1</code> (up) to <code>1</code> (down). */ public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY); }这两个接口一个实现点击监听,一个实现变化监听.所以我们可以根据需要,在我们自己的类当中使用.
3.构建符合自己需要的精灵
本节当中需要根据控制器来控制精灵的旋转,移动.而AndEngine给我们提供的AnimatedSprite类只实现了旋转,
并没有按照一定速度移动的方法,在这里我自己重写了该类:
public class MyControlSprite extends AnimatedSprite{ public MyControlSprite(float pX, float pY, float pWidth, float pHeight, ITiledTextureRegion pTiledTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) { super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager); // TODO Auto-generated constructor stub mVelocityX = 0; mVelocityY = 0; } private float mVelocityX;//水平速度 private float mVelocityY;//垂直速度 @Override protected void onManagedUpdate(float pSecondsElapsed) { // TODO Auto-generated method stub this.mX += mVelocityX * pSecondsElapsed; this.mY += mVelocityY * pSecondsElapsed; this.setPosition(mX, mY); super.onManagedUpdate(pSecondsElapsed); } void setVolocityX(float vX){ mVelocityX = vX; } void setVolocityY(float vY){ mVelocityY = vY; } void setVolocityXY(float vX, float vY){ mVelocityX = vX; mVelocityY = vY; } }
主要在onManagedUpdate方法中增加了精灵按照一定速度移动的方法
4.资源构建
1.本例子用到4张图片:背景,精灵,控制器背景,控制器指针
源代码如下:
private static final float CAMERA_WIDTH = 800; private static final float CAMERA_HEIGHT = 480; private Camera mCamera; private RepeatingSpriteBackground mBackground; private TiledTextureRegion mBaseRegion; private TiledTextureRegion mKnobRegion; private TiledTextureRegion mDragonRegion; @Override public EngineOptions onCreateEngineOptions() { // TODO Auto-generated method stub mCamera = new Camera(0,0,CAMERA_WIDTH, CAMERA_HEIGHT); EngineOptions mEngineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_SENSOR,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),mCamera); return mEngineOptions; } @Override public void onCreateResources( OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception { // TODO Auto-generated method stub mBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, getTextureManager(), AssetBitmapTextureAtlasSource.create(getAssets(), "background_grass.png"), getVertexBufferObjectManager()); BitmapTextureAtlas mDragonTexture = new BitmapTextureAtlas(getTextureManager(), 512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA); BitmapTextureAtlas mController = new BitmapTextureAtlas(getTextureManager(),256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA); mDragonRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mDragonTexture, this, "snapdragon_tiled.png", 0, 0, 4, 3); mBaseRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mController, this, "onscreen_control_base.png", 0, 0, 1, 1); mKnobRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mController, this, "onscreen_control_knob.png", 128, 0, 1, 1); mDragonTexture.load(); mController.load(); pOnCreateResourcesCallback.onCreateResourcesFinished(); }
5.场景创建
1.基础场景
Scene mScene = new Scene(); mScene.setBackground(mBackground); if(MultiTouch.isSupported(this)){ this.mEngine.setTouchController(new MultiTouchController()); }
2.创建控制器和动画精灵,在这里有两个控制器,一个是控制移动,一个控制旋转
final MyControlSprite mDragon = new MyControlSprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2,80.0f,80.0f,mDragonRegion, getVertexBufferObjectManager()); mDragon.animate(100); final AnalogOnScreenControl mDerectionControl = new AnalogOnScreenControl(20, CAMERA_HEIGHT-mBaseRegion.getHeight()-20, mCamera, mBaseRegion, mKnobRegion, 0.1f, 200, getVertexBufferObjectManager(), new IAnalogOnScreenControlListener(){ @Override public void onControlChange( BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY) { // TODO Auto-generated method stub mDragon.setVolocityXY(pValueX*100, pValueY*100); } @Override public void onControlClick( AnalogOnScreenControl pAnalogOnScreenControl) { // TODO Auto-generated method stub Log.d("Season","Click on Controller"); } }); final AnalogOnScreenControl mRotationControl = new AnalogOnScreenControl(CAMERA_WIDTH - 20 - mBaseRegion.getWidth(), CAMERA_HEIGHT-mBaseRegion.getHeight()-20, mCamera, mBaseRegion, mKnobRegion, 0.1f, 200, getVertexBufferObjectManager(), new IAnalogOnScreenControlListener(){ @Override public void onControlChange( BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY) { // TODO Auto-generated method stub if(pValueX == 0 && pValueY == 0){ mDragon.setRotation(0.0f); } mDragon.setRotation(MathUtils.radToDeg((float) Math.atan2(pValueY, pValueX))); } @Override public void onControlClick( AnalogOnScreenControl pAnalogOnScreenControl) { // TODO Auto-generated method stub Log.d("Season","Click on Controller"); } }); mDerectionControl.setAlpha(0.65f); mRotationControl.setAlpha(0.65f);
3.将角色加载到场景,在这里需要注意的地方是:
mDragon使用mScene.attachChild(IEntity)的方法
而mDerectionControl和setChildScene只能使用setChildScene(Scene)的方法,不然的话
这两个控制器你怎么点他们都毫无反应,这也是我刚刚开始的时候没仔细看代码犯下的错误
mDerectionControl.setAlpha(0.65f); mRotationControl.setAlpha(0.65f); mScene.attachChild(mDragon); mScene.setChildScene(mDerectionControl); mDerectionControl.setChildScene(mRotationControl); pOnCreateSceneCallback.onCreateSceneFinished(mScene);
5.一切准备就绪,运行代码
终于完成了所有代码,开心呀.hoho~~~~~
转载请注明出处哦,谢谢!本文由Season于2012-11-1 14:18完成的
运行结果如下:
本例子源代码:http://download.csdn.net/detail/cen616899547/4710222
现在经常需要在Mac和Ubuntu两个不同的系统来回切换, vim和emacs在Mac上面又不好用,自己也进行了尝试,不太满足自己的要求。
原来在windows下就使用Sublime, 感觉很棒,下面是一些配置。
一、 Ubuntu
到www.sublimetext.com下载最新版本,然后进行解压, 简单的功能就不介绍,可以参考其他文章:
http://www.iplaysoft.com/sublimetext.html (这是对Sublime的总体介绍, 其中关于Package Control的安装不合适,请看下面)
http://www.qianduan.net/essential-to-sublime-the-text-2-plugins.html (推荐使用的插件)
http://www.feelcss.com/sublime-text-2-settings.html(Sublime配置文件介绍)
http://wbond.net/sublime_packages/community(Packages Control里面所有package的介绍, 如果你安装的package无效,可以参照文档来进行配置)
http://www.technoreply.com/how-to-install-sublime-text-2-on-ubuntu-12-04-unity/(这是一篇Ubuntu的配置文档,很不错)
在安装Package Control的时候, 必须要在Sublime Text 2的文件夹下新建目录 Data\Installed Packages ,这样当我们运行:
import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'命令时,才能正确的获得packages信息。
Package Control的官方地址为:http://wbond.net/sublime_packages/package_control/installation ,建议下载最新的版本。