I am developing a game in andengine.i want to move a player sprite by onScreenDigitalControl on an autoparallex background.Can anyone tell me how to do that ???? thanks
Using this code you can make player sprite moving by DigitalOnScreenControl. Also you can find many tutorials on this at http://www.andengine.org/forums/tutorials/
public class TileActivity extends BaseGameActivity {
private TMXTiledMap mTMXTiledMap;
private BoundCamera mBoundChaseCamera;
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
private Scene mScene;
private static final long[] ANIMATE_DURATION = new long[]{200, 200, 200};
private static final int PLAYER_VELOCITY = 2;
private BitmapTextureAtlas mTexturePlayer;
private Body mPlayerBody;
private TiledTextureRegion mPlayerTextureRegion;
private BitmapTextureAtlas mOnScreenControlTexture;
private TextureRegion mOnScreenControlBaseTextureRegion;
private TextureRegion mOnScreenControlKnobTextureRegion;
private DigitalOnScreenControl mDigitalOnScreenControl;
private PhysicsWorld mPhysicsWorld;
private enum PlayerDirection{
NONE,
UP,
DOWN,
LEFT,
RIGHT
}
private PlayerDirection playerDirection = PlayerDirection.NONE;
#Override
public Engine onLoadEngine() {
this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mBoundChaseCamera));
}
#Override
public void onLoadResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
// Control texture
this.mOnScreenControlTexture = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
// Player sprite texture
this.mTexturePlayer = new BitmapTextureAtlas(128, 128, TextureOptions.DEFAULT);
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "hero.png", 0, 0, 3, 4);
// Load the textures
this.mEngine.getTextureManager().loadTextures(this.mTexturePlayer, this.mOnScreenControlTexture);
}
#Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
// Create physics world
this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
// Create the scene and register the physics world
mScene = new Scene();
mScene.registerUpdateHandler(this.mPhysicsWorld);
// Load the TMX map
try {
final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.NEAREST, null);
this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "test.tmx");
} catch (final TMXLoadException tmxle) {
Debug.e(tmxle);
}
// Add the non-object layers to the scene
for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++){
TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i);
if (!layer.getTMXLayerProperties().containsTMXProperty("wall", "true"))
mScene.attachChild(layer);
}
// Read in the unwalkable blocks from the object layer and create boxes for each
this.createUnwalkableObjects(mTMXTiledMap);
// Make the camera not exceed the bounds of the TMXEntity.
final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
this.mBoundChaseCamera.setBoundsEnabled(true);
// Add outer walls
this.addBounds(tmxLayer.getWidth(), tmxLayer.getHeight());
// Calculate the coordinates for the player, so it's centred on the camera.
final int centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight()) / 2;
// Create the player sprite and add it to the scene.
final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion);
this.mBoundChaseCamera.setChaseEntity(player);
final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, playerFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false){
#Override
public void onUpdate(float pSecondsElapsed){
super.onUpdate(pSecondsElapsed);
mBoundChaseCamera.updateChaseEntity();
}
});
mScene.attachChild(player);
// Add the control
this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IOnScreenControlListener() {
#Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
// Set the correct walking animation
if (pValueY == 1){
// Up
if (playerDirection != PlayerDirection.UP){
player.animate(ANIMATE_DURATION, 0, 2, true);
playerDirection = PlayerDirection.UP;
}
}else if (pValueY == -1){
// Down
if (playerDirection != PlayerDirection.DOWN){
player.animate(ANIMATE_DURATION, 9, 11, true);
playerDirection = PlayerDirection.DOWN;
}
}else if (pValueX == -1){
// Left
if (playerDirection != PlayerDirection.LEFT){
player.animate(ANIMATE_DURATION, 3, 5, true);
playerDirection = PlayerDirection.LEFT;
}
}else if (pValueX == 1){
// Right
if (playerDirection != PlayerDirection.RIGHT){
player.animate(ANIMATE_DURATION, 6, 8, true);
playerDirection = PlayerDirection.RIGHT;
}
}else{
if (player.isAnimationRunning()){
player.stopAnimation();
playerDirection = PlayerDirection.NONE;
}
}
// Set the player's velocity
mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
}
});
this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setAlpha(0.5f);
this.mDigitalOnScreenControl.refreshControlKnobPosition();
mScene.setChildScene(this.mDigitalOnScreenControl);
return mScene;
}
#Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
private void createUnwalkableObjects(TMXTiledMap map){
// Loop through the object groups
for(final TMXObjectGroup group: this.mTMXTiledMap.getTMXObjectGroups()) {
if(group.getTMXObjectGroupProperties().containsTMXProperty("wall", "true")){
// This is our "wall" layer. Create the boxes from it
for(final TMXObject object : group.getTMXObjects()) {
final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
mScene.attachChild(rect);
}
}
}
}
private void addBounds(float width, float height){
final Shape bottom = new Rectangle(0, height - 2, width, 2);
bottom.setVisible(false);
final Shape top = new Rectangle(0, 0, width, 2);
top.setVisible(false);
final Shape left = new Rectangle(0, 0, 2, height);
left.setVisible(false);
final Shape right = new Rectangle(width - 2, 0, 2, height);
right.setVisible(false);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottom, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, top, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(bottom);
this.mScene.attachChild(top);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
}
}
Related
Andegine error load map when output have :horizontal stripes
i have load map, but have horizontal stripes in maps. i dont know. please help me.i am new developer,i use andegine. Can anyone help me with this problem? Any idea?
the image :
the code
public class TileActivity
extends SimpleBaseGameActivity {
private TMXTiledMap mTMXTiledMap;
private BoundCamera mBoundChaseCamera;
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
private Scene mScene;
private static final long[] ANIMATE_DURATION = new long[] { 200, 200, 200 };
private static final int PLAYER_VELOCITY = 2;
private BitmapTextureAtlas mTexturePlayer;
private Body mPlayerBody;
private TiledTextureRegion mPlayerTextureRegion;
private BitmapTextureAtlas mOnScreenControlTexture;
private TextureRegion mOnScreenControlBaseTextureRegion;
private TextureRegion mOnScreenControlKnobTextureRegion;
private DigitalOnScreenControl mDigitalOnScreenControl;
private PhysicsWorld mPhysicsWorld;
private TMXLayer layer;
#Override
protected void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
// Control texture
this.mOnScreenControlTexture = new BitmapTextureAtlas(getTextureManager(), 256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
// Player sprite texture
this.mTexturePlayer = new BitmapTextureAtlas(getTextureManager(), 96, 128, TextureOptions.DEFAULT);
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "hero.png", 0, 0, 3, 4);
// Load the textures
this.mTexturePlayer.load();
this.mOnScreenControlTexture.load();
}
#Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
// Create physics world
this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
// Create the scene and register the physics world
mScene = new Scene();
mScene.registerUpdateHandler(this.mPhysicsWorld);
// Load the TMX map
try {
final TMXLoader tmxLoader = new TMXLoader(this.getAssets(),
this.mEngine.getTextureManager(),
TextureOptions.BILINEAR_PREMULTIPLYALPHA,
getVertexBufferObjectManager(),
new TMXLoader.ITMXTilePropertiesListener() {
#Override
public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {}
});
this.mTMXTiledMap = tmxLoader.loadFromAsset("test.tmx");
} catch (final TMXLoadException tmxle) {
Debug.e(tmxle);
}
// Add the non-object layers to the scene
for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++) {
layer = this.mTMXTiledMap.getTMXLayers().get(i);
if (!layer.getTMXLayerProperties().containsTMXProperty("wall", "true")) {
mScene.attachChild(layer);
}
}
// Read in the unwalkable blocks from the object layer and create boxes for each
this.createUnwalkableObjects(mTMXTiledMap);
// Add outer walls
this.addBounds(layer.getWidth(), layer.getHeight());
this.mBoundChaseCamera.setBounds(0, layer.getWidth(), 0, layer.getHeight());
// Calculate the coordinates for the player, so it's centred on the camera.
final int centerX = (int) (CAMERA_WIDTH - this.mPlayerTextureRegion.getWidth()) / 2;
final int centerY = (int) (CAMERA_HEIGHT - this.mPlayerTextureRegion.getHeight()) / 2;
// Create the player sprite and add it to the scene.
final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion, getVertexBufferObjectManager());
this.mBoundChaseCamera.setChaseEntity(player);
final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyDef.BodyType.DynamicBody, playerFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false) {
#Override
public void onUpdate(float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
mBoundChaseCamera.updateChaseEntity();
}
});
mScene.attachChild(player);
// Add the control
this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, getVertexBufferObjectManager(),
new BaseOnScreenControl.IOnScreenControlListener() {
#Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
// Set the correct walking animation
if (pValueY == 1) {
// Up
if (playerDirection != PlayerDirection.UP) {
player.animate(ANIMATE_DURATION, 0, 2, true);
playerDirection = PlayerDirection.UP;
}
} else if (pValueY == -1) {
// Down
if (playerDirection != PlayerDirection.DOWN) {
player.animate(ANIMATE_DURATION, 9, 11, true);
playerDirection = PlayerDirection.DOWN;
}
} else if (pValueX == -1) {
// Left
if (playerDirection != PlayerDirection.LEFT) {
player.animate(ANIMATE_DURATION, 3, 5, true);
playerDirection = PlayerDirection.LEFT;
}
} else if (pValueX == 1) {
// Right
if (playerDirection != PlayerDirection.RIGHT) {
player.animate(ANIMATE_DURATION, 6, 8, true);
playerDirection = PlayerDirection.RIGHT;
}
} else {
if (player.isAnimationRunning()) {
player.stopAnimation();
playerDirection = PlayerDirection.NONE;
}
}
// Set the player's velocity
mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
}
});
this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setAlpha(0.5f);
this.mDigitalOnScreenControl.refreshControlKnobPosition();
mScene.setChildScene(this.mDigitalOnScreenControl);
return mScene;
}
#Override
public EngineOptions onCreateEngineOptions() {
this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.mBoundChaseCamera);
}
private enum PlayerDirection {
NONE,
UP,
DOWN,
LEFT,
RIGHT
}
private PlayerDirection playerDirection = PlayerDirection.NONE;
private void createUnwalkableObjects(TMXTiledMap map) {
// Loop through the object groups
for (final TMXObjectGroup group : this.mTMXTiledMap.getTMXObjectGroups()) {
if (group.getTMXObjectGroupProperties().containsTMXProperty("wall", "true")) {
// This is our "wall" layer. Create the boxes from it
for (final TMXObject object : group.getTMXObjects()) {
final Rectangle rect = new Rectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), getVertexBufferObjectManager());
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyDef.BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
mScene.attachChild(rect);
}
}
}
}
private void addBounds(float width, float height) {
final Rectangle bottom = new Rectangle(0, height - 2, width, 2, getVertexBufferObjectManager());
bottom.setVisible(false);
final Rectangle top = new Rectangle(0, 0, width, 2, getVertexBufferObjectManager());
top.setVisible(false);
final Rectangle left = new Rectangle(0, 0, 2, height, getVertexBufferObjectManager());
left.setVisible(false);
final Rectangle right = new Rectangle(width - 2, 0, 2, height, getVertexBufferObjectManager());
right.setVisible(false);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottom, BodyDef.BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, top, BodyDef.BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyDef.BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyDef.BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(bottom);
this.mScene.attachChild(top);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
}
}
This is generally caused by pixel blending, which happens in the following situations:
If the texture is rendered smaller than native size, the pixels are averaged and values from neighboring pixels affect the edges of the tiles.
If the texture is rendered larger than native size, pixel values are interpolated, which also causes values from neighboring pixels to affect the edges of the tiles.
If the texture is not rendered exactly aligned to a pixel, interpolation also occurs.
So to avoid it, either make sure none of the above happens or disable texture filtering (putting it on nearest neighbor).
Nearest neighbor filtering of course doesn't look nice when the map gets scaled, so if you want to support this case you can alternatively make sure that each tile is surrounded by pixels that match the color of its edge pixels. This avoids colors from other tiles affecting its borders.
i'm trying to create something like Newton's cradle. But when one ball hits another, all balls move simultaneously in same direction. How can I decide this problem? Should i create another physical options of balls in createFixtureDef? Or I need to use some specific algorithm to transfer impuls between balls?
public class MainActivity extends SimpleBaseGameActivity implements IAccelerationListener,
IOnAreaTouchListener
{
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private BitmapTextureAtlas mBitmapTextureAtlas;
final String TAG = "States";
private Scene mScene;
protected ITiledTextureRegion mBoxFaceTextureRegion;
protected ITiledTextureRegion mCircleFaceTextureRegion;
protected PhysicsWorld mPhysicsWorld;
#Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
protected void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32
this.mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32
this.mBitmapTextureAtlas.load();
}
#Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
this.mScene.setBackground(new Background(0, 0, 0));
//this.mScene.setOnSceneTouchListener(this);
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
this.initJoints(mScene);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
this.mScene.setOnAreaTouchListener(this);
return this.mScene;
}
private void initJoints(final Scene pScene) {
final float centerY = CAMERA_HEIGHT / 2;
final float spriteWidth = this.mBoxFaceTextureRegion.getWidth();
final float spriteHeight = this.mBoxFaceTextureRegion.getHeight();
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(30, 0.2f, 0.2f);
for(int i = 0; i < 10; i++) {
final float anchorFaceX = 100 + i * spriteWidth ;
final float anchorFaceY = centerY;
final AnimatedSprite anchorFace = new AnimatedSprite(anchorFaceX, anchorFaceY,
this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
final Body anchorBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,
anchorFace, BodyType.StaticBody,
objectFixtureDef);
final AnimatedSprite movingFace = new AnimatedSprite(anchorFaceX, anchorFaceY + 150,
this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager()) ;
// movingFace.setScale(1.2f);
final Body movingBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
movingFace, BodyType.DynamicBody, objectFixtureDef);
movingFace.setUserData(movingBody);
// anchorFace.setScale(1.2f);
anchorFace.animate(200);
anchorFace.animate(200);
final Line connectionLine = new Line(anchorFaceX + spriteWidth / 2,
anchorFaceY + spriteHeight / 2,
anchorFaceX + spriteWidth / 2,
anchorFaceY + spriteHeight / 2,
this.getVertexBufferObjectManager());
pScene.registerTouchArea(movingFace);
pScene.attachChild(connectionLine);
pScene.attachChild(anchorFace);
pScene.attachChild(movingFace);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(anchorFace,
anchorBody, true, true){
#Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
final Vector2 movingBodyWorldCenter = movingBody.getWorldCenter();
connectionLine.setPosition(connectionLine.getX1(), connectionLine.getY1(),
movingBodyWorldCenter.x * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
movingBodyWorldCenter.y * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
}
});
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(movingFace, movingBody, true, true));
final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(anchorBody, movingBody, anchorBody.getWorldCenter());
this.mPhysicsWorld.createJoint(revoluteJointDef);
}
}
#Override
public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
}
#Override
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
public boolean onAreaTouched( final TouchEvent pSceneTouchEvent,
final ITouchArea pTouchArea,final float pTouchAreaLocalX,
final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionMove())
{
float touchX = pSceneTouchEvent.getX();
float touchY = pSceneTouchEvent.getY();
Log.d(TAG, "move to in X" +touchX + "n Y " +touchY);
final AnimatedSprite anchorFace = (AnimatedSprite) pTouchArea;
final Body tochedBody = (Body)anchorFace.getUserData();
//move sprite to xy
final float x = pSceneTouchEvent.getX();
final float y = pSceneTouchEvent.getY();
final float widthD2 = anchorFace.getWidth() / 2;
final float heightD2 = anchorFace.getHeight() / 2;
final float angle = tochedBody.getAngle(); // keeps the body angle
final Vector2 v2 = Vector2Pool.obtain((x + widthD2) / 32, (y + heightD2) / 32);
tochedBody.setTransform(v2, angle);
Vector2Pool.recycle(v2);
return true;
}
return false;
}
}
Unfortunately Box2D is not really suitable for this.
In my experience, you can get one or two swing-thrus to work as long as the balls are not touching each other to start with. That is, each ball starts with a very small gap between it and the neighbor on each side. This means that when they collide each collision is solved using just two balls at a time, for a total of 4 separate collisions for the impact to travel to the other side (for 5 balls), instead of solving all 5 balls as a single 'island'.
Trouble is, the positioning needs to be very precise, and after a few swings the balls stray from that, so that you end up with collisions involving more than two balls at a time. I only ever saw at most 2-3 swings work as desired...
I created a racing game in andengine.There is a player which is size 100x30px,like a rectangle.And there is fire() function to shoot bullet, code is below.It works fine.but problem is when player is rotating.player is a car image.so they rotating by directionButton().When I shoot a bullet,it starting 0x0px of car.But i want it must be start to move in front of car like 100x15px of car.How can I do?
public void fire() {
float startBulletX=player.getX();
float startBulletY=player.getY();
Sprite bullet=new Sprite(startBulletX,startBulletY,bulletRegion);
final float xComp = (float) Math.cos(MathUtils.degToRad(this.player.getRotation() + 90));
final float yComp = (float) Math.sin(MathUtils.degToRad(this.player.getRotation() + 90));
final Vector2 velocity = Vector2Pool.obtain(xComp * 10, yComp * 10);
bullet.setRotation(this.player.getRotation());
final FixtureDef bulletFixtureDef1 = PhysicsFactory.createFixtureDef(0, 0, 0);
this.mBulletBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, bullet, BodyType.KinematicBody, bulletFixtureDef1);
this.mBulletBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bullet, this.mBulletBody, true, false));
this.scene.attachChild(bullet);
}
//on screen control button
public void addDirectionButton() {
player = new AnimatedSprite(centerX, centerY, this.playerTextureRegion);
final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 200, new IAnalogOnScreenControlListener() {
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
final Body carBody = DeffenseActivity.this.mCarBody;
final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5);
carBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
final float rotationInRad = (float)Math.atan2(-pValueX, pValueY);
player.setRotation(MathUtils.radToDeg(rotationInRad));
}
public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
}
});
analogOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
analogOnScreenControl.getControlBase().setAlpha(0.5f);
analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
analogOnScreenControl.getControlBase().setScale(1.25f);
analogOnScreenControl.getControlKnob().setScale(1.25f);
analogOnScreenControl.refreshControlKnobPosition();
this.scene.setChildScene(analogOnScreenControl);
this.mBoundChaseCamera.setChaseEntity(player);
a=new Sprite(0,0,bulletRegion);
player.attachChild(a);
final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
this.mCarBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, carFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, this.mCarBody, true, false));
this.scene.attachChild(player);
}
I'm working on a little game/project and I decide to use AndEngine for it because basically the Add/Remove Physics functionality in that engine was all I need. But it seems that I have a little problem with it. I have to add like 36 different pictures on a scene and be able to remove them on select. The problem is that my images are damaged and it's not showing all the images, just the first two. Am I missing something or I'm doing it wrong? Here is what I get on my device :
and here is my code :
public class Game extends BaseExample implements IAccelerometerListener, IOnSceneTouchListener, IOnAreaTouchListener {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 980;
private static final int CAMERA_HEIGHT = 560;
// ===========================================================
// Fields
// ===========================================================
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion firstSprite;
private TextureRegion secondSprite;
private TextureRegion thirdSprite;
private TextureRegion fourthSprite;
private TextureRegion fifthSprite;
private TextureRegion sixSprite;
private TextureRegion sevenSprite;
private TextureRegion eightSprite;
private TextureRegion nineSprite;
private TextureRegion tenSprite;
private Scene mScene;
private PhysicsWorld mPhysicsWorld;
private int mFaceCount = 0;
#Override
public Engine onLoadEngine() {
Toast.makeText(this, "Touch the screen to add objects. Touch an object to remove it.", Toast.LENGTH_LONG).show();
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
engineOptions.getTouchOptions().setRunOnUpdateThread(true);
return new Engine(engineOptions);
}
#Override
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.firstSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "1.png", 0, 0); // 64x32
this.firstSprite.setTextureRegionBufferManaged(false);
this.secondSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "2.png", 0, 32); // 64x32
this.secondSprite.setTextureRegionBufferManaged(false);
this.thirdSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "3.png", 0, 32); // 64x32
this.thirdSprite.setTextureRegionBufferManaged(false);
this.fourthSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "4.png", 0, 32); // 64x32
this.fourthSprite.setTextureRegionBufferManaged(false);
this.fifthSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "5.png", 0, 32); // 64x32
this.fifthSprite.setTextureRegionBufferManaged(false);
this.sixSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "6.png", 0, 32); // 64x32
this.sixSprite.setTextureRegionBufferManaged(false);
this.sevenSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "7.png", 0, 32); // 64x32
this.sevenSprite.setTextureRegionBufferManaged(false);
this.eightSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "8.png", 0, 32); // 64x32
this.eightSprite.setTextureRegionBufferManaged(false);
this.nineSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "9.png", 0, 32); // 64x32
this.nineSprite.setTextureRegionBufferManaged(false);
this.tenSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "10.png", 0, 32); // 64x32
this.tenSprite.setTextureRegionBufferManaged(false);
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}
#Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
this.mScene.setBackground(new ColorBackground(204f, 204f, 204f));
this.mScene.setOnSceneTouchListener(this);
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(ground);
this.mScene.attachChild(roof);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
this.mScene.setOnAreaTouchListener(this);
return this.mScene;
}
#Override
public void onLoadComplete() {
}
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
ITouchArea pTouchArea, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()) {
this.removeFace((Sprite)pTouchArea);
return true;
}
return false;
}
#Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(this.mPhysicsWorld != null) {
if(pSceneTouchEvent.isActionDown()) {
this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
}
return false;
}
#Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
private void addFace(final float pX, final float pY) {
this.mFaceCount++;
Sprite face = null;
Body body = null;
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
/*if(this.mFaceCount % 2 == 0) {
face = new Sprite(pX, pY, this.firstSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
} else {
face = new Sprite(pX, pY, this.secondSprite);
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
}*/
switch(this.mFaceCount){
case 1:
face = new Sprite(pX, pY, this.firstSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 2:
face = new Sprite(pX, pY, this.secondSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 3:
face = new Sprite(pX, pY, this.thirdSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 4:
face = new Sprite(pX, pY, this.fourthSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 5:
face = new Sprite(pX, pY, this.fifthSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 6:
face = new Sprite(pX, pY, this.sixSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 7:
face = new Sprite(pX, pY, this.sevenSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 8:
face = new Sprite(pX, pY, this.eightSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 9:
face = new Sprite(pX, pY, this.nineSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
case 10:
face = new Sprite(pX, pY, this.tenSprite);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
break;
}
if(this.mFaceCount<=9){
this.mScene.registerTouchArea(face);
this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
}
}
#Override
public void onResumeGame() {
super.onResumeGame();
this.enableAccelerometerSensor(this);
}
private void removeFace(final Sprite face) {
final PhysicsConnector facePhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(face);
this.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
this.mPhysicsWorld.destroyBody(facePhysicsConnector.getBody());
this.mScene.unregisterTouchArea(face);
this.mScene.detachChild(face);
System.gc();
}
}
Any ideas where is my mistake?
The main problem is with your onLoadResources method.
A Texture (BitmapTextureAtlas in your case) is simply a large canvas containing all small images.
Now, the method BitmapTextureAtlasTextureRegionFactory.createFromAsset(..) receives 2 numerical parameters (the 2 last parameters). These are used for the image position on the texture.
Now, take a look at what are you doing: Loading all of the images in the same position! So some images (The last image loaded - 10.png) override the others...
For more information, read about Textures & TextureRegions here.
Another problem that you have in your code - In your addFace method, when you call PhysicsFactory.createBoxBody(...) the body is created and it automatically joins your physics world, even if you don't register a physics connector between it and between the sprite, and even if you don't attach the sprite to the scene.
Only create a body when you are sure you'll need it. Otherwise, make sure you destroy it.
this.tenSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "10.png", 0, 32);
this is the declration of ur image now look at the end 0,32 is same for all image you just need to change it 64,64
does anyone know why applyforce only works on one of my sprites? Also when I press the other sprites it also applies force to the one individual sprite. The nextTile method works fine.
enter code herepackage com.martynnorman.jude;
/**
* #author Nicolas Gramlich
* #since 11:54:51 - 03.04.2010
*/
public class MainActivity extends BaseGameActivity implements IOnAreaTouchListener {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
int centerX;
int centerY;
// ===========================================================
// Fields
// ===========================================================
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mFaceTextureRegion;
private BitmapTextureAtlas mBitmapTextureAtlas2;
private TiledTextureRegion mFaceTextureRegion2;
Random random = new Random();
Ball sprite;
int scale;
Scene scene;
private BitmapTextureAtlas mFontTexture;
private Font mFont;
Text textcenter;
int t = 1;
Ball rgSprite[] = new Ball[10];
private PhysicsWorld mPhysicsWorld;
Body body;
int isTouched;
final Vector2 gravity2 = new Vector2(0, 20);
final Vector2 gravity = new Vector2(0, 0);
private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
#Override
public Engine onLoadEngine() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}
#Override
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "gfx/ball.png", 0, 0, 2, 1);
this.mBitmapTextureAtlas2 = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas2, this,"gfx/ball2.png", 0, 0, 2, 1);
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas, mBitmapTextureAtlas2);
this.mEngine.getFontManager().loadFont(this.mFont);
this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
}
#Override
public Scene onLoadScene() {
createAETimeHandler(2);
/* final Text textcenter = new Text(100, 60, this.mFont, "touched", HorizontalAlign.CENTER);
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);*/
final Scene scene = new Scene();
scene.setOnAreaTouchListener(this);
scene.setBackground(new ColorBackground(0.6274f, 0.6274f, 0.8784f));
mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
scene.registerUpdateHandler(mPhysicsWorld);
mPhysicsWorld.setGravity(gravity);
final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
scene.attachChild(ground);
final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
scene.attachChild(left);
scene.attachChild(right);
scene.attachChild(roof);
// scene.setOnSceneTouchListener((IOnSceneTouchListener) this);
//final Ball hit = new Ball(random.nextInt(600)+1, random.nextInt(400)+1, this.mFaceTextureRegion.clone());
//final Ball hit2 = new Ball(random.nextInt(600)+1, random.nextInt(400)+1, this.mFaceTextureRegion2.clone());
//body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, hit2, BodyType.DynamicBody, FIXTURE_DEF);
//scene.attachChild(hit2);
//hit2.setScale(2);
//scene.registerTouchArea(hit2);
//this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(hit2, body, true, true));
for (int i = 0; i < rgSprite.length; i++) {
rgSprite[i] = new Ball(random.nextInt(600)+1, 200, this.mFaceTextureRegion.clone());
}
mPhysicsWorld.setGravity(gravity2);
for (Ball sprite : rgSprite) {
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, sprite, BodyType.DynamicBody, FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true));
scene.registerTouchArea(sprite);
scene.attachChild(sprite);
sprite.setScale(2);
}
//scene.attachChild(hit);
//hit.setScale(2);
//scene.registerTouchArea(hit);
return scene;
}
#Override
public void onLoadComplete() {
}
private void createAETimeHandler(float mEffectSpawnDelay)
{ TimerHandler spriteTimerHandler2;
this.getEngine().registerUpdateHandler(spriteTimerHandler2 = new TimerHandler(mEffectSpawnDelay, true, new ITimerCallback()
{
#Override
public void onTimePassed(final TimerHandler pTimerHandler)
{
/*if (isTouched == 1){
mFaceTextureRegion.setCurrentTileIndex(0);
isTouched = 2;
} */
}
}));
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,float pTouchAreaLocalY) {
if(this.mPhysicsWorld != null) {
if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
this.onOff((AnimatedSprite)pTouchArea);
}
} return false;
}
private void onOff(final AnimatedSprite ball) {
ball.nextTile();
body.applyForce(new Vector2(200,-1500), new Vector2(body.getWorldCenter()));
}
}
It is because you only have one Body-variable, and it gets overwritten by the loop. When all sprites have been created you only know the body of the last sprite.
You must apply the force to the body associated with the sprite you click.