I am developing game using andengine. I want to add a sprite as a child to another sprite so that sprite will rotate along with another sprite. Since I am new to andengine I didn't know how to add sprite as a child. By rotating main sprite the child has to rotate with it.
Some of them suggest to call sprite.attachChild() method to add as child but I can't get this method. I am extending BaseGameActivity.
Edited: Some of them says it's due to old version of andengine. Would anyone give me link for download new version of andengine?
AnimatedSpriteHelicopter mSpriteHelicopter = new AnimatedSpriteHelicopter(0, 0, this.mTRHelicopter);
mSpriteHelicopter.animate(50);
this.mScene.getChild(GameLayers.HELICPTER_LAYER).attachChild(mSpriteHelicopter);
this.mScene.registerTouchArea(mSpriteHelicopter);
this.mScene.setTouchAreaBindingEnabled(true);
// ==========================
// Missile
// ===========================
AnimatedSpriteMissile mMissile = new AnimatedSpriteMissile(0, 0, this.mTRMissile);
mMissile.animate(100);
mSpriteHelicopter.attachChild(mMissile);
return this.mScene;
Just a sample code. now missile will move with helicopter sprite :) very simple.
I have my own classes extended with AnimatedSprite.
The source code is located at http://code.google.com/p/andengine/ and indeed sprite composition is a feature in the most recent version of AndEngine as verified in this forum thread.
I'm using it and can personally verify that it's functional.
Related
I want to create a map from some opengl code that I wrote :
In order to do that I though about taking a screen shot of the upperview of the gl screen.
Yet I cant seem to find how to do that...
any suggestions?
Similar problem has been solved in OSG example code here.
First you need to set your view such that you are looking at center from the TOP VIEW.
osg::Vec3 center = scene->getBound().center();
double radius = scene->getBound().radius();
view->getCamera()->setViewMatrixAsLookAt( center - lookDir*(radius*3.0), center, up );
view->getCamera()->setProjectionMatrixAsPerspective(
30.0f, static_cast<double>(width)/static_cast<double>(height), 1.0f, 10000.0f );
Then, you need to use some OS specific API to do similar to logic below:
osgViewer::ScreenCaptureHandler* scrn = new osgViewer::ScreenCaptureHandler();
osgViewer::ScreenCaptureHandler::WriteToFile* captureOper = new osgViewer::ScreenCaptureHandler::WriteToFile(tmpStr.m_szBuffer, "png");
scrn->setCaptureOperation(captureOper);
scrn->captureNextFrame(*_viewer);
_viewer->frame();
Of course if you are not using OSG then you need to find equivalent APIs (of library you are using) to achieve the same task.
I'm writing android apps with Andengine. Sorry for my question, but I'm new to andengine. What does the function setOffsetCenter() do in andengine?
I remember to use setOffsetCenter here :
mCamera.offsetCenter(10, 10)
offsetCenter simply adds the parameters to the current center coordinates. Adding 0 has no influence at all, so the call will be useless.
Android AndEngine two circles collision perfectly. I have two circle and a collision method for them, I want when they touch each other the collision happens, currently when they near each other the collision happens.
I think that it is because of the transparent free space in the .png file of each circle.
In the picture you can see that now they collide from a distance, I want when both touch each other.
My collision method:
if (circle1.collidesWith(circle)){
Score += 1;
}
I am almost sure you are right that transparent places in png causes it. You probably creating BoxBody. In your case you should use circle body like this:
Body circleBody = PhysicsFactory.createCircleBody(pWorld, pSprite, BodyType.StaticBody, FixtureDef);
If it doesn't help there is method overload where you can provide position and size of the body. I can recommend you using DebugRender which you only have to attach to scene:
new DebugRenderer(physicsWorld, vbom)
When u use this you will see how helpful it can be:) Just remember that it may slowdown your phone when you have a lot of bodies on the scene.
PS. You didn't give us a lot of information but you should use contactListener to check colisions. There are plenty of tutorials in the internet for it
PS2. If you don't use Box2D extension - do it. This is great feature of AndEngine and it's pointless to implement that for yourself. It will be hard to detect circle shape collision of 2 objects without Box2D.
If you are not in Box2d , You must use Pixel-Perfect Collision library. Well default AndEngine Library, Does not support pixel perfect collision. To get this support, you need to import this library in eclipse and add this to your project uses library.
Here, I Demonstrate how to use this library. When you define Texture and Atlas for your sprite write as below.
private BitmapTextureAtlas lifeAtlas;
public PixelPerfectTiledTextureRegion life_Texture;
PixelPerfectTextureRegionFactory.setAssetBasePath("gfx/game/");
lifeAtlas = new BitmapTextureAtlas(textureManager, 1280, 128,
TextureOptions.BILINEAR);
life_Texture = PixelPerfectTextureRegionFactory.createTiledFromAsset(
lifeAtlas, activity, "heart_tiled.png", 0, 0, 10, 1, 0);
lifeAtlas.load();
For your custom sprite class,
public class Plane extends PixelPerfectAnimatedSprite {
public Plane(float pX, float pY,
PixelPerfectTiledTextureRegion pTiledTextureRegion,
VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
setColor(Color.GREEN);
}
}
You also need some adjustment with your AndEngine library to use it. Follow this thread to go.
I'm developping a game which implements gif sprites...
After start with gif I used png image and for change the TextureRegion of my sprite I followed this guide: Sprite.setImage() in andengine and it worked very well.
Now I want to do the same thing with AnimatedSprite. Can someone tell me How i can edit the org.anddev.andengine.entity.sprite.AnimatedSprite.java to create:
sprite.setAnimatedTextureRegion(PixelPerfectTiledTextureRegion textureRegion)
P.S I use also PixelPerfect
The AnimatedSprite class uses the same texture region declared in BaseSprite, so the code in the link you gave will work - just paste it in the AnimatedSprite.java file:
public void setTiledTextureRegion(TiledTextureRegion textureRegion) {
this.mTextureRegion = textureRegion;
}
Note: If the different tiled textures have different number of tiles (So each has a different animation parameters associated with it) you should handle it in your code. It may get complicated, so I'd just create a new AnimatedSprite instead of replacing the texture region.
I have an AnimatedSprite that after it finishes the animation I want to reverse animate it. I want to do that continuously. Once the reverse animation is complete I want to play the original one. This may be easy one but I am new to Android and AndEngine.
mFlower1Sprite = new AnimatedSprite(20, 800, this.mFlower1);
mFlower1Sprite.setScale((float) 1.5);
mFlower1Sprite.animate(500, 0, new IAnimationListener () {
public void onAnimationEnd(final AnimatedSprite pAnimatedSprite) {
// reverse animation
}
});
mScene.attachChild(mFlower1Sprite);
return mScene;
Use animate method:
public AnimatedSprite animate (long[] pFrameDurations, int[] pFrames,
int pLoopCount, AnimatedSprite.IAnimationListener pAnimationListener)
Animate specifics frames.
Parameters:
pFrameDurations must have the same length as pFrames.
pFrames indices of the frames to animate.
Simply list the indices in reverse order.
I'm working with sprites myself atm and I came across this jQuery plugin. Check it out.
http://spritely.net/
I'm not aware of any support to reverse the frames of an AnimatedSprite, although it's probably a useful feature so I'd love to find out if I'm wrong.
Your best bet would probably be to create another sprite sheet with the frames reversed and another instance of an AnimatedSprite for that sheet. Then define one or two private IAnimationListeners inside your activity (rather than on the fly when you call .animate()), which alternately detach and attach the two sprites at the end of each animation.