2013-04-07 118 views
0

我做與框架動畫的cocos2d android.I有一系列的圖像相同的打擊的形象,我想裁剪圖像使用tivi在android系統到cocos2d中。 Plz幫助我? link image動畫與cocos2d的安卓

公共類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; 
    } 

} 

}

+0

???? – 2013-04-13 14:17:31

回答

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,機器人1 ..它有交給動畫基本概念.....你會從一系列示例得到的答案

+0

的開始是好的,但來自雖然沒有人更新了更多的幫助,任何鏈接或任何來源,如果u的hve一些鏈接,然後分享..... – 2013-04-14 18:32:36

0

海我希望這會幫助你很多。我用過CCSprite。它通過動畫支持所有屏幕。在480×800

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

}縮放CCSprite

要裁剪圖像或灰度圖像在動畫