there is a sprite on the stage that can be moved by touching it. I want to determine the area at the bottom of the screen, touching which, I can move this sprite. In general, the type of point on the screen. I try to do this with the andengine engine, but my searches did not lead to any method in this engine, with which I could at least click on the sprite by clicking on the specified area .. tell me which methods will help to implement it.
Added after 6 hours 1 minute supplement the question. Here is an example https://gyazo.com/39b76d3e992dba4c369f428c9459c8a1 this smile should move when I move my finger over this black area (like a touchpad on the screen). I tried the andengine engine and did not find anything ready, everywhere you need to touch the sprite itself. How to remake the analog control of the joystick, which I also did not understand in the andengine engine, came to the point that the whole construction of the method should be changed there. I decided to write all this myself. The next thing I found on the Internet is the Canvas. Did everything as it is here https://habrahabr.ru/sandbox/27511/ . On the screen there is this smiley that is redrawn where I click. That's where I get the touch coordinates.
public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { touchX = event.getX(); touchY = event.getY(); invalidate(); } return true; } and redraw the smile in the new coordinates. But I need to touch touched only in a certain area of the screen and then I will substitute them and without invalidate () (until I tried to draw without it) to redraw this smiley, I just have to adjust these coordinates somehow. After all, you need to substitute not the coordinates themselves, but the change of coordinates. Those. clicked on the touchpad area and start moving your finger and the difference between the coordinates will be tracked and continuously transmitted for drawing. I will add this difference to the existing smiley coordinates, and an n-fold increase or decrease in these coordinates will allow you to adjust the sensitivity of the touchpad. And this problem, which I saw at the beginning, is solved, if you touch the emoticon with your finger and move it, the emoticon begins to lag behind the finger, or if it does not quickly redraw.
So, we need help in order to determine the area from which the coordinates of the touch are read, or rather, the change of this coordinate from the point of tangency along the motion. Thanks, I will be glad to any instructions to the necessary methods and methods. or can someone advise a more reasonable or effective way to accomplish my task.
it turned out not so hard. I really haven’t done so that the clicks are caught in a certain area. but already progress. when you click on any area of the screen, this smiley starts moving and repeating my movements, but if you put the cursor on the smiley and start moving to notice how far its drawing is lagging behind the movement of the cursor, you will notice that this lag creates discomfort. here is a piece of code
@Override protected void onDraw(Canvas canvas) { // код рисующий графику Bitmap mybitmap = BitmapFactory.decodeResource(getResources(),R.drawable.face_box); // рисуем myBitmap на канве в координатах 10, 10 canvas.drawBitmap(mybitmap, touchX, touchY, null); } public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: korX = touchX; korY = touchY; initX = event.getRawX(); initY = event.getRawY(); return true; case MotionEvent.ACTION_MOVE: touchX = korX + (int) (event.getRawX() - initX); touchY = korY + (int) (event.getRawY() - initY); invalidate(); return true; } return false; } touchX and touchY are new coordinates, according to which the invalidate () method redraws this smile, but this is slowly happening. How can this be accelerated, in what ways to draw? I note that the onTouch method in which it is implemented and what I have done and in general it touches the object and its subsequent movement creates this lag in the drawing (I think so), because this lag is observed everywhere it is used. GestureDetector may have another implementation, I will try to use it .. and what do you suggest? Added later.
In general, I found how to draw on Canvas in a separate thread. The problem is this. I click the mouse, the bitmap appears on the screen, I click the second time, the bitmap should disappear from the old place and appear where I clicked. But it turns out that on one canvas everything is drawn, how to redraw? (Now I attach the code itself
public class MainActivity1 extends Activity { public static float touchX = 10, touchY = 10
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GraphicsView(this)); } public class GraphicsView extends SurfaceView implements SurfaceHolder.Callback { private DrawThread drawThread; public GraphicsView(Context context) { super(context); getHolder().addCallback(this); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder){ drawThread = new DrawThread(getHolder(),getResources()); drawThread.setRunning(true); drawThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; drawThread.setRunning(false); while (retry) { try { drawThread.join(); retry = false; } catch (InterruptedException e) { } } } public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touchX = event.getX(); touchY = event.getY(); surfaceCreated(getHolder()); return true; } return false; } } }
and the class in which the second thread is implemented
public class DrawThread extends Thread { private boolean running = false; private SurfaceHolder surfaceHolder; public Bitmap bitmap; public DrawThread(SurfaceHolder surfaceHolder,Resources resources){ this.surfaceHolder = surfaceHolder; bitmap = BitmapFactory.decodeResource(resources, R.drawable.face_box); } public void setRunning(boolean running){ this.running = running; } @Override public void run(){ Canvas canvas; while (running){ canvas = null; try { canvas = surfaceHolder.lockCanvas(null); if (canvas == null) continue; canvas.drawBitmap(bitmap, MainActivity1.touchX, MainActivity1.touchY, null); } finally { if (canvas != null){ surfaceHolder.unlockCanvasAndPost(canvas); } } } } }