我用框架 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;
}
}
}