用cocos2dandroid制作动画

用cocos2dandroid制作动画,android,cocos2d-android,Android,Cocos2d Android,我正在用框架cocos2d android制作动画。我有一系列的图像,我想在android中裁剪一个用于cocos2d的tivi图像。能帮我吗? 公共类MainActivity扩展了活动{ private static final boolean DEBUG = true; private CCGLSurfaceView mGLSurfaceView; static Context context; static Resources resources; static AssetManage

我正在用框架cocos2d android制作动画。我有一系列的图像,我想在android中裁剪一个用于cocos2d的tivi图像。能帮我吗?

公共类MainActivity扩展了活动{

private static final boolean DEBUG = true;

private CCGLSurfaceView mGLSurfaceView;
static Context context;
static Resources resources;
static AssetManager assetManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mGLSurfaceView = new CCGLSurfaceView(this);
    setContentView(mGLSurfaceView);
    assetManager = getAssets();

}

@Override
protected void onStart() {
    super.onStart();
    // attach the OpenGL view to a Window
    Director.sharedDirector().attachInView(mGLSurfaceView);
    // set landscape mode
    // Director.sharedDirector().setLandscape(true);
    // show FPS
    Director.sharedDirector().setDisplayFPS(true);
    // frames per second
    Director.sharedDirector().setAnimationInterval(1.0f / 60);

    Scene scene = Scene.node();
    scene.addChild(nextAction());
    // Make the scene active
    Director.sharedDirector().runWithScene(scene);
}

static int sceneIdx = -1;
static Class transitions[] = { Animation1.class, };

static Layer nextAction() {
    sceneIdx++;
    sceneIdx = sceneIdx % transitions.length;
    return restartAction();
}

static Layer restartAction() {
    try {
        Class c = transitions[sceneIdx];
        return (Layer) c.newInstance();
    } catch (Exception e) {
        if (DEBUG)
            e.printStackTrace();
        return null;
    }
}

@Override
protected void onPause() {
    super.onPause();
    Director.sharedDirector().pause();
}

@Override
protected void onResume() {
    super.onResume();
    Director.sharedDirector().resume();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    TextureManager.sharedTextureManager().removeAllTextures();
}

static class Anima extends Layer {

    Sprite tivi;
    Sprite bg2;
    InputStream is;
    Resources resources;
    AssetManager asset;
    Bitmap bf, bitmap, bitmap2;
    CCSize s = Director.sharedDirector().displaySize();

    public Anima() {

        Log.v("MP", "Width: " + s.width + " Height: " + s.height);
        bg2 = Sprite.sprite("Scene02.png");

        bg2.setScaleX(s.width / bg2.getTexture().getWidth());
        bg2.setScaleY(s.height / bg2.getTexture().getHeight());
        bg2.setPosition(s.width / 2, s.height / 2);
        addChild(bg2, 1);

        tivi = Sprite.sprite("TV0000.png");
        tivi.setScaleX(s.width / bg2.getTexture().getWidth());
        tivi.setScaleY(s.height / bg2.getTexture().getHeight());
        //tivi.setPosition(0, 0);
        tivi.setPosition(247 * s.width / bg2.getTexture().getWidth(), 480
                 * s.height / bg2.getTexture().getHeight());
        addChild(tivi, 1);

    }

    protected void centerSprites() {
        // tivi.setPosition(247 * s.width / bg2.getTexture().getWidth(), 480
        // * s.height / bg2.getTexture().getHeight());
    }

}

static class Animation1 extends Anima {

    Animation tvAnimation;
    IntervalAction tvAction;
    CCSize s = Director.sharedDirector().displaySize();
    boolean ck = true;



    public Animation1() {
        // centerSprites();
        isTouchEnabled_ = true;
        tvAnimation = initTVAnimation();
        tvAction = Animate.action(tvAnimation);
    }

    private Animation initTVAnimation() {

        Animation animation = new Animation("abc", 0.2f);
        for (int i = 1; i < 40; i++) {
            try {
                is = assetManager.open(new CCFormatter().format(
                        "TV00%02d.png", i));
                bf = BitmapFactory.decodeStream(is);
                bitmap = Bitmap.createBitmap(bf, 0, 0, bf.getWidth(),
                        bf.getHeight());
                bitmap2 = Bitmap
                        .createBitmap(bitmap, (int) (153 * (s.width / bg2
                                .getTexture().getWidth())),
                                (int) (261.5 * (s.height / bg2.getTexture()
                                        .getHeight())),
                                (int) (87 * (s.width / bg2.getTexture()
                                        .getWidth())),
                                (int) (97 * (s.height / bg2.getTexture()
                                        .getHeight())));
                animation.addFrame(bitmap2);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return animation;
    }

    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        CCSize s = Director.sharedDirector().displaySize();
        CCPoint location = Director.sharedDirector().convertToGL(
                event.getX(), event.getY());
        if ((location.x >= 203.5 * s.width / bg2.getTexture().getWidth() && location.x <= 290
                * s.width / bg2.getTexture().getWidth())
                && (location.y >= 433.5 * s.height
                        / bg2.getTexture().getHeight() && location.y <= 526
                        * s.height / bg2.getTexture().getHeight())) {
            if (tvAction.isDone() == true || ck == true) {
                tivi.runAction(tvAction);
                ck = false;
            }
        }
        // addNew(location);

        return TouchDispatcher.kEventHandled;
    }

}
private static final boolean DEBUG=true;
私有CCGLSurfaceView mGLSurfaceView;
静态语境;
静态资源;
静态AssetManager AssetManager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
mGLSurfaceView=新CCGLSurfaceView(此);
setContentView(mGLSurfaceView);
assetManager=getAssets();
}
@凌驾
受保护的void onStart(){
super.onStart();
//将OpenGL视图附加到窗口
Director.sharedDirector().attachInView(mGLSurfaceView);
//设置景观模式
//Director.sharedDirector().setscape(true);
//显示FPS
Director.sharedDirector().setDisplayFPS(true);
//每秒帧数
Director.sharedDirector().setAnimationInterval(1.0f/60);
Scene=Scene.node();
scene.addChild(nextAction());
//使场景处于活动状态
Director.sharedDirector().runWithScene(场景);
}
静态int sceneIdx=-1;
静态类转换[]={Animation1.Class,};
静态层nextAction(){
sceneIdx++;
sceneIdx=sceneIdx%transitions.length;
返回重新启动();
}
静态层重启(){
试一试{
c类=转换[sceneIdx];
返回(层)c.newInstance();
}捕获(例外e){
如果(调试)
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPause(){
super.onPause();
Director.sharedDirector().pause();
}
@凌驾
受保护的void onResume(){
super.onResume();
Director.sharedDirector().resume();
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
TextureManager.sharedTextureManager().removeAllTextures();
}
静态类Anima扩展层{
雪碧tivi;
雪碧bg2;
输入流为;
资源;
资产管理人资产;
位图bf、位图、位图2;
CCSize s=Director.sharedDirector().displaySize();
公共动物(){
对数v(“MP”,“宽度:+s.宽度+”高度:+s.高度);
bg2=Sprite.Sprite(“Scene02.png”);
bg2.setScaleX(s.width/bg2.getTexture().getWidth());
bg2.setScaleY(s.height/bg2.getTexture().getHeight());
bg2.设置位置(s.宽度/2,s.高度/2);
addChild(bg2,1);
tivi=Sprite.Sprite(“TV0000.png”);
tivi.setScaleX(s.width/bg2.getTexture().getWidth());
tivi.setScaleY(s.height/bg2.getTexture().getHeight());
//tivi.setPosition(0,0);
tivi.setPosition(247*s.width/bg2.getTexture().getWidth(),480
*s.height/bg2.getTexture().getHeight();
addChild(tivi,1);
}
受保护的空心中心精灵(){
//tivi.setPosition(247*s.width/bg2.getTexture().getWidth(),480
//*s.height/bg2.getTexture().getHeight();
}
}
静态类Animation1扩展了Anima{
动画电视动画;
间隙动作;
CCSize s=Director.sharedDirector().displaySize();
布尔ck=true;
公共动画1(){
//中心精灵();
isTouchEnabled=真;
tvAnimation=initTVAnimation();
tvAction=Animate.action(tvamation);
}
私有动画initTVAnimation(){
动画=新动画(“abc”,0.2f);
对于(int i=1;i<40;i++){
试一试{
is=assetManager.open(新CCFormatter().format(
“TV00%02d.png”,i);
bf=BitmapFactory.decodeStream(is);
bitmap=bitmap.createBitmap(bf,0,0,bf.getWidth(),
bf.getHeight());
位图2=位图
.createBitmap(位图,(int)(153*(s.width/bg2
.getTexture().getWidth()),
(int)(261.5*(s.height/bg2.getTexture)
.getHeight()),
(int)(87*(s.width/bg2.getTexture()
.getWidth()),
(int)(97*(s.height/bg2.getTexture()
.getHeight());
addFrame(位图2);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
返回动画;
}
@凌驾
公共布尔CCTouchesBegind(运动事件){
CCSize s=Director.sharedDirector().displaySize();
CCPoint location=Director.sharedDirector().convertToGL(
event.getX(),event.getY());
如果((location.x>=203.5*s.width/bg2.getTexture().getWidth()&&location.x=433.5*s.height
/bg2.getTexture().getHeight()和&location.y
Animation动画=新动画(“abc”,0.2f);
对于(int i=1;i<40;i++){
试一试{
is=assetManager.open(新CCFormatter().format(
“TV00%02d.png”,i);
bf=BitmapFactory.decodeStream(is);
bitmap=bitmap.createBitmap(bf,0,0,bf.getWidth(),
bf.getHeight());
位图2=位图
.createBitmap(位图,(int)(153*(s.width/bg2
.getTexture().getWidth()),
(int)(261.5*(s.height/bg2.getTexture)
.getHeight()),
(int)(87*(s.width/bg2.getTexture()
.getWidth()),
Animation animation = new Animation("abc", 0.2f);
    for (int i = 1; i < 40; i++) {
        try {
            is = assetManager.open(new CCFormatter().format(
                    "TV00%02d.png", i));
            bf = BitmapFactory.decodeStream(is);
            bitmap = Bitmap.createBitmap(bf, 0, 0, bf.getWidth(),
                    bf.getHeight());
            bitmap2 = Bitmap
                    .createBitmap(bitmap, (int) (153 * (s.width / bg2
                            .getTexture().getWidth())),
                            (int) (261.5 * (s.height / bg2.getTexture()
                                    .getHeight())),
                            (int) (87 * (s.width / bg2.getTexture()
                                    .getWidth())),
                            (int) (97 * (s.height / bg2.getTexture()
                                    .getHeight())));
            animation.addFrame(bitmap2);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return animation;
}
public class MainMenuScreen extends CCColorLayer {

private CCSprite newGame1;
private CGSize size; // this is where we hold the size of current display
private float scaleX, scaleY;// these are the ratios that we need to compute

public static CCScene sence() {
    CCScene ccScene = CCScene.node();
    CCLayer layer = new MainMenuScreen(ccColor4B.ccc4(69, 3, 3, 255));
    ccScene.addChild(layer);
    return ccScene;
}

protected MainMenuScreen(ccColor4B color) {
    super(color);

    size = CCDirector.sharedDirector().winSize();

    CCSprite backGround = CCSprite
            .sprite(CandygrabberImage.MENU_BACKGROUND);
    this.setIsTouchEnabled(true);
    // backGround.setContentSize(winSize);
    backGround.setTag(1);
    backGround.setScale(.5f);
    backGround.setPosition(CGPoint.ccp(size.width / 2.0f,
            size.height / 2.0f));
    backGround
            .setScaleX(size.width / backGround.getTexture().getWidth());
    backGround.setScaleY(size.height
            / backGround.getTexture().getHeight());

    addChild(backGround);

    scaleX = size.width / 480f;// assuming that all my assets are available
                                // for a 320X480(landscape) resolution;
    scaleY = size.height / 800f;

    newGame1 = CCSprite.sprite("newgame1.png");
    CCAnimation buttonAnimation = CCAnimation.animation("", 1f);
    buttonAnimation.addFrame("newgame1.png");
    buttonAnimation.addFrame("newgame2.png");
    buttonAnimation.addFrame("newgame3.png");
    buttonAnimation.addFrame("newgame4.png");
    buttonAnimation.addFrame("newgame5.png");
    buttonAnimation.addFrame("newgame6.png");
    buttonAnimation.addFrame("newgame7.png");
    buttonAnimation.addFrame("newgame8.png");

    CCIntervalAction buttonAction = CCAnimate.action(5, buttonAnimation,
            false);
    newGame1.runAction(CCRepeatForever.action(buttonAction));

    newGame1.setScaleX(scaleX);
    newGame1.setScaleY(scaleY);
    newGame1.setScale(aspect_Scale(newGame1, scaleX, scaleY));

    this.addChild(newGame1);

    this.setIsTouchEnabled(true);
}

public float aspect_Scale(CCSprite sprite, float scaleX, float scaleY) {
    float sourcewidth = sprite.getContentSize().width;
    float sourceheight = sprite.getContentSize().height;

    float targetwidth = sourcewidth * scaleX;
    float targetheight = sourceheight * scaleY;
    float scalex = (float) targetwidth / sourcewidth;
    float scaley = (float) targetheight / sourceheight;

    return Math.min(scalex, scaley);
}