i am trying to study andengine. When i am touching on center of sprite(tree) everything works fine. But when i am touching on top of the sprite it jump to the center of the sprite.
hope my question is clear.
this is the on touch event code i am using
final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
I think you want your sprite to center of the Scene when you touch it on any part of sprite,
But you are using onAreaTouched for that use should use IOnSceneTouchListener for that like below :-
#Override
public boolean onSceneTouchEvent(final Scene pScene,
final TouchEvent pSceneTouchEvent) {
this.setPosition(pSceneTouchEvent.getX() - face.getWidth() / 2, pSceneTouchEvent.getY() - face.getHeight() / 2);
return false;
}
Related
I have stone sprite class in which I have defined touch, drag and throw actions. My stone sprite move well when I touch and drag it around screen. My expectation is sprite should move only within limited circular area (visible/invisible).
This is my code below
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
{
if (pSceneTouchEvent.isActionMove() || pSceneTouchEvent.isActionDown() || pSceneTouchEvent.isActionOutside())
{
this.setX(pSceneTouchEvent.getX() - this.getWidth() / 2);
this.setY(pSceneTouchEvent.getY() - this.getHeight() / 2);
}
else if (pSceneTouchEvent.isActionUp())
{
mPhysicsHandler.setVelocityX((originX - getX()) * 5);
mPhysicsHandler.setVelocityY((originY - getY()) * 5);
mPhysicsHandler.setAccelerationY(100);
}
return true;
}
Please help.
Before applying the move when dragging, check to see if the touch event falls within the circle. To do this, measure the distance from the center of the circle. If the distance is less than the radius of the circle, then move the object. If not, do nothing.
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
{
if (pSceneTouchEvent.isActionMove() || pSceneTouchEvent.isActionDown() ||
pSceneTouchEvent.isActionOutside())
{
// Use Pythagorean theorem to get distance between 2 points;
// center is a point you declare
// radius is a value you declare that is the radius of the circular area
Float distance = (pSceneTouchEvent.getX() - center.x)*(pSceneTouchEvent.getX() - center.x) + (pSceneTouchEvent.getY() - center.y) * (pSceneTouchEvent.getY() - center.y);
distance = Math.sqr(distance);
if(distance < radius){
// Only move if touch event is inside the circle area
this.setX(pSceneTouchEvent.getX() - this.getWidth() / 2);
this.setY(pSceneTouchEvent.getY() - this.getHeight() / 2);
}
}
else if (pSceneTouchEvent.isActionUp())
{
mPhysicsHandler.setVelocityX((originX - getX()) * 5);
mPhysicsHandler.setVelocityY((originY - getY()) * 5);
mPhysicsHandler.setAccelerationY(100);
}
return true;
}
I have 2 sprites, I would like to move both of them, when I touched sprite A and moved it, sprite B will be moved also without touching sprite B at the same time. thanks for the help
here is the code
I have 2 sprites, I would like to move both of them, when I touched sprite A and moved it, sprite B will be moved also without touching sprite B at the same time. thanks for the help
here is the code
center = new AnimatedSprite(0, 0, resourcesManager.interact, vbom) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,final float pTouchAreaLocalX, final float pTouchAreaLocalY)
{
//SPrite A
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
pSceneTouchEvent.getY() - this.getHeight() / 2);
//Sprite B
return true;
}
};
If you can post some code it would help in creating a more helpful answer.
In essence, when you handle the touch event and apply the movement to sprite A all you need to do is also apply the movement to sprite B.
If you post your code I will be able to edit and add an example.
You can change position of sprite B at the same moment with sprite A, inside spriteA's onAreaTouched method:
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,final float pTouchAreaLocalX, final float pTouchAreaLocalY)
{
//SPrite A
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
pSceneTouchEvent.getY() - this.getHeight() / 2);
spriteB.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
pSceneTouchEvent.getY() - this.getHeight() / 2);
};
I am new to andengine and even android game development. i have created sprite as a box. this box is now draggable by using this coding. it works fine.
but i want multitouch on this which i want to rotate a sprite with 2 finger in that box and even it should be draggable. .... plz help someone...
i am trying this many days but no idea.
final float centerX = (CAMERA_WIDTH - this.mBox.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mBox.getHeight()) / 2;
Box= new Sprite(centerX, centerY, this.mBox,
this.getVertexBufferObjectManager()) {
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,
pSceneTouchEvent.getY() - this.getHeight() / 2);
float pValueX = pSceneTouchEvent.getX();
float pValueY = CAMERA_HEIGHT-pSceneTouchEvent.getY();
float dx = pValueX - gun.getX();
float dy = pValueY - gun.getY();
double Radius = Math.atan2(dy,dx);
double Angle = Radius * 360 ;
Box.setRotation((float)Math.toDegrees(Angle));
return true;
}
Make sure you enabled multi touch in your game. You can use the same code used in the MultiTouchExample in the onLoadEngine method.
The algorithm is quite simple, similar to what you've posted here.
Keep track of up to 2 pointer IDs you get in onAreaTouched method. (You can get the pointer ID by calling pSceneTouchEvent.getPointerID()).
Keep track of the pointers' state (Currently touching/not touching) and location (pTouchAreaLocalX and pTouchAreaLocalY).
Whenever 2 pointers are touching (You received ACTION_DOWN for both), save the initial angle. (Math.tan2(pointer1Y - pointer2Y, pointer1X - pointer2X)).
As long as ACTION_UP is not called for the pointers, update the new angle in every ACTION_MOVE event of the pointers, and get the angle delta (delta = currentAngle - initialAngle). Then call setRotation(Math.toDegrees(delta)).
To make the sprite dragable with 2 pointers, you need to move your sprite the lesser of the distance each pointer has moved. For example, if:
pointer1.dX = 50;
pointer1.dY = -20;
pointer2.dX = 40;
pointer2.dY = -10;
the sprite should move +40 units in the X axis, and -10 units in the Y axis.
i want to make a sprite move when i click on it, but i have a problem... The sprite always goes to the upper left corner, i tried to put random values, but it gives no result
ballITexture = TextureRegionFactory.extractFromTexture(BaseActivity.getSharedInstance().ballTexture);
final Random rand = new Random();
sprite = new Sprite(0, 0, ballITexture, BaseActivity.getSharedInstance().getVertexBufferObjectManager()){
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
randomMove(rand.nextInt(4), pTouchAreaLocalX, pTouchAreaLocalY, this);
};
mCamera = BaseActivity.getSharedInstance().mCamera;
sprite.setPosition(mCamera.getWidth() / 2 - sprite.getWidth() / 2, mCamera.getHeight() / 2 - sprite.getHeight() / 2);
moveable = true;
and this is randomMove method:
private void randomMove(int direction, float pTouchAreaLocalX, float pTouchAreaLocalY, Sprite sprite){
switch(direction){
case 0:
sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, 1, pTouchAreaLocalY, 1));
break;
case 1:
sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, 1, pTouchAreaLocalY, -1));
break;
case 2:
sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, -1, pTouchAreaLocalY, 1));
break;
case 3:
sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, -1, pTouchAreaLocalY, -1));
break;
}
}
my question is, why my sprite goes always to up-left corner when I touch it, instead of moving in random direction? How to solve it?
forget this, i passed incorrect params to MoveModifier, where i put 1 and -1 values should be any pixel of the camera
private void randomMove(int direction, float pTouchAreaLocalX, float pTouchAreaLocalY, Sprite sprite){
sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, rand.nextInt(mCamera.getWidth()), pTouchAreaLocalY, rand.nextInt(mCamera.getHeight())));
I want to limit the area to move the sprite object only on this area (for example a area dimensions 200x200).
I would to create a box2D 200x200 where the sprites can moved only on this area
How do you do that please?
#Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegionLetterOne
.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegionLetterOne
.getHeight()) / 2;
final Sprite letterOne = new Sprite(centerX - centerX / 2, centerY
- centerY / 2, this.mFaceTextureRegionLetterOne,
this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
final Sprite letterTwo = new Sprite(centerX - centerX / 2, centerY,
this.mFaceTextureRegionLetterTwo,
this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
pSceneTouchEvent.getY() - this.getHeight() / 2);
//int count = scene.getChildCount();
//for(int i = 0; i < count; i++) {
IEntity entity = scene.getChildByIndex(1);
if (entity instanceof Sprite) {
if (entity.getUserData().equals("sprite"))
if (((Sprite) entity).collidesWith(letterOne))
Log.v("colission", "face_box is collised on google plus -> letterTwo on letterOne");
}
//}
return true;
}
};
letterTwo.setUserData("sprite");
final Sprite boxArea = new Sprite(centerX, centerY,
this.mFaceTextureRegionBox, this.getVertexBufferObjectManager());
letterOne.setScale(2);
scene.attachChild(letterOne);
scene.registerTouchArea(letterOne);
letterTwo.setScale(2);
scene.attachChild(letterTwo);
scene.registerTouchArea(letterTwo);
boxArea.setScale(2);
scene.attachChild(boxArea);
scene.setTouchAreaBindingOnActionDownEnabled(true);
return scene;
}
Thank you.
I don't know box2D, but normally you would check the edges of the sprite on each movement, if they do not overlap the edges of the area.
If your sprite is represented by a Rectangle, and the area where you can move also represented by a Rectangle, this could be done easy.
But first, check the box2D API, maybe it has already some helper methods to ease this task, something like:
obect.overlaps(object)
You should create a Rectangle of 200X200 and then you should use create a body of the rectangle using the Box2D Physics. And for Fixture definition for rectangle you can set density, Elasticity and Friction so that your sprites will be treated accordingly when they collide with the boundary of the Rectangle.
To create a Rectangle you can refer the Examples of the Andengine.
With your code, you seem to try to do collision checking with your sprites ?!
It would be complicated and not nice when you try to do things without physics bodies. So lets use the physics bodies with your sprites.
But note that, DO NOT create a solid box body for your area (containing your sprites), lets use 4 separated body walls (left, top, right, bottom) to form a closed box; because game engine can only check collision with solid shapes.
The following is the code for reference:
/**
* #param pScene
* Sence of the game, get from class member
* #param pWorld
* physics world of the game, get from class member
*/
public void CreateSprites(final Scene pScene, final PhysicsWorld pWorld)
{
final FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(10, 1.1f, 0.0f);//should be placed as member of class
final Sprite letterTwo = new Sprite(centerX - centerX / 2, centerY,
this.mFaceTextureRegionLetterTwo,
this.getVertexBufferObjectManager());
final Body letterTwoBody = PhysicsFactory.createBoxBody(pWorld, letterTwo, BodyType.DynamicBody, mFixtureDef);
letterTwo.setUserData(letterTwoBody); // for later sprite-body attachment access
pScene.attachChild(letterTwo);
pWorld.registerPhysicsConnector(new PhysicsConnector(letterTwo, letterTwoBody, true, true));
}
/** Create the walls, in these boudaries sprites will move */
public void InitBoxWalls(Scene pScene, PhysicsWorld pWorld)
{
final float WALL_MARGIN_WIDTH = 5f;
final float WALL_MARGIN_HEIGHT = 10f;
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
mWallGround = new Rectangle(-WALL_MARGIN_WIDTH, mCameraHeight + WALL_MARGIN_HEIGHT - 2, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager);
mWallRoof = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager);
mWallLeft = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager);
mWallRight = new Rectangle(mCameraWidth + WALL_MARGIN_WIDTH - 2, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager);
PhysicsFactory.createBoxBody(pWorld, mWallGround, BodyType.StaticBody, mWallFixtureDef);
PhysicsFactory.createBoxBody(pWorld, mWallRoof, BodyType.StaticBody, mWallFixtureDef);
PhysicsFactory.createBoxBody(pWorld, mWallLeft, BodyType.StaticBody, mWallFixtureDef);
PhysicsFactory.createBoxBody(pWorld, mWallRight, BodyType.StaticBody, mWallFixtureDef);
pScene.attachChild(mWallGround);
pScene.attachChild(mWallRoof);
pScene.attachChild(mWallLeft);
pScene.attachChild(mWallRight);
}
And the last thing you should do is to look up example of Physics/MouseJoint in AndEngine examples.