Cocos2d-java中Activity控制Controller

前端之家收集整理的这篇文章主要介绍了Cocos2d-java中Activity控制Controller前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有两种办法,一种是用Helper类,提供interface

可参考之前一篇:http://blog.csdn.net/xzongyuan/article/details/39757275

另一种是利用FunctionFactory,这就不需要额外类

先在controller中implements Function接口

public class PlayingSceneController extends NodeController implements Function{

实现函数

@Override
public Object apply(Object o) {


    if(o.toString().equals("isLong")){
        Logger.log("isLong");
        playSprite.runAction(MoveTo.create(0.5f,new Vec2(playSprite.getPositionX(),playSprite.getPositionY()+1000)));
    }else{
        Logger.log("isShort");
        playSprite.runAction(MoveTo.create(0.5f,playSprite.getPositionY()+300)));
    }
    return null;
}
在Android的类中,通过callFunction调用
 public class HeadSetReceiver extends BroadcastReceiver{

        Timer timer = null;

        @Override
        public void onReceive(Context context,Intent intent) {
            String intentAction = intent.getAction() ;
            Log.e(TAG,"onReceive");
            if(Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){
                //获得KeyEvent对象
                boolean isLong = intent.getBooleanExtra("isLong",false);

                if(isLong){
                    FunctionFactory.callFunction("buttonClick","isLong");
                }else{
                    FunctionFactory.callFunction("buttonClick","isShort");
                }
                /*
                if( ListenerHelper.getListener()!=null){

                    ListenerHelper.getListener().onClick(isLong);
                }*/

            }
        }
            //终止广播(不让别的程序收到此广播,免受干扰)
         //   abortBroadcast();
    }

猜你在找的Cocos2d-x相关文章