0

我用框架 cocos2d android 做动画。我有系列图像相同的打击图像,我想将图像中的 tvi 裁剪为 android 中的 cocos2d。请帮帮我? 链接图片

公共类 MainActivity 扩展 Activity {

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

}

}

4

2 回答 2

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

尝试简化这段代码..使用 cocs2d-android 1 中的最新示例系列 ..它具有处理动画的基本概念.....您将从示例系列中得到答案

于 2013-04-14T18:25:05.330 回答
0

海,我希望这会对你有所帮助。我用过CCSprite。它可以扩展以支持所有带有动画的屏幕。以 480x800 缩放 CCSprite

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

}

于 2013-08-02T21:55:57.057 回答