Andengine, making a sprite move in random direction - android

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

Related

Android have user draw a scaling circle

I'm trying to make a basic "Paint"-esque app for a class, and part of what I'm trying to implement is the ability to draw a circle that scales in size as the user moves their finger to pick the center, and drags it outward to the desired radius. The problem is, no matter what I try it just ends up repeatedly stamping a small circle where ever the user moves their finger. The idea is the app will get the coordinates for the center (tX, tY) on the press down, draw a circle with a radius r every time the user moves, then finalize it when they lift their finger.
Edit: the commented out sections are parts where I was testing different things.
Code for my onMotionEvent custom class:
#Override
public boolean onTouchEvent(MotionEvent event) {
//detect user touch
float tX = event.getX();
float tY = event.getY();
float rX = 0;
float rY = 0;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(tX, tY);
break;
case MotionEvent.ACTION_MOVE:
if(shapes){
rX = (int) event.getX();
rY = (int) event.getY();
r = (float) Math.sqrt(Math.pow(tX - rX, 2) + Math.pow(tY - rY, 2));
drawCanvas.drawCircle(tX, tY, r, drawPaint);
invalidate();
}else{
drawPath.lineTo(tX, tY);
}
break;
case MotionEvent.ACTION_UP:
/*rX = event.getX();
rY = event.getY();
r = (float) Math.sqrt(Math.pow(tX - rX, 2) + Math.pow(tY - rY, 2));*/
if(shapes){
if(shape_sel == 0){
//drawCanvas.drawCircle(tX, tY, r, drawPaint);
drawPath.reset();
/*}else if(shape_sel == 1){
}
else{
*/}
}else{
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
}
break;
default:
return false;
}
invalidate();
return true;
}
You aren't saving tx and ty on touch down, you're resetting them on every event. You need to save tx and ty in class level variables inside the down case.

Sprite jump to center of sprite on touching it

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

How to drag a sprite within limited circular area using andengine

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

TOuchEvent Moving Two Sprites at the sametime

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

AndEngine draw line onSceneTouchEvent

I'm new to AndEngine and I want to draw a line by using my finger coordinates in the onSceneTouchEvent I did all what I knew and I think its right but there is something that keeps it from working I don't know what, I can't detect the problem :/
private Scene scene;
float startX;
float startY;
float lastX;
float lastY;
int lineUsageCount = 0;
#Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
switch (pSceneTouchEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = pSceneTouchEvent.getX();
startY = pSceneTouchEvent.getY();
lineUsageCount = 0 ;
break;
case MotionEvent.ACTION_MOVE:
lineUsageCount++;
lastX = pSceneTouchEvent.getX();
lastY = pSceneTouchEvent.getY();
break;
case MotionEvent.ACTION_UP:
if (lineUsageCount <= 5) {
final Line line = new Line(startX, startY, lastX,
lastY, this.getVertexBufferObjectManager());
line.setLineWidth(1);
line.setColor(255, 255, 255);
scene.attachChild(line);
}
break;
}
return true;
}
};
it should get the first coordinates startX, startY and then on the Action_Move it gets the lastX, lastY and draw the line but it doesn't seem to work :|
Problem is in this line of code: if (lineUsageCount <= 5) {
While you move your finger, lineUsageCount will be much more then 5. Debug this variable and than change it in "IF" or remove this "IF" from your code.

Categories

Resources