简体   繁体   中英

Moving sprite from point to point

I want to move a sprite between two points.

first point is x=0 and y=0 and the second point is a point where the user touched the screen.

To move, I want to use an equation to go through the two points which is y=ax+b .

I have attempted this in the method move() but the sprite does not move.

Please help.

Class Display:

package com.example.name;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Display extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private Sprite sprite;
private long lastClick;
private float x = 0.0F;
private float y = 0.0F;

public Display(Context context) {
    super(context);

    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gonia);
    sprite = new Sprite(this, bmp, x, y, x, y);

    gameLoopThread = new GameLoopThread(this);
    holder = getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);
            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
        }
    });

}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    sprite.onDraw(canvas);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (System.currentTimeMillis() - lastClick > 500) {
        lastClick = System.currentTimeMillis();
        x = (float) sprite.ostatniaWartoscX();
        y = (float) sprite.ostatniaWartoscY();
        float gotox = (float) event.getX();
        float gotoy = (float) event.getY();
        synchronized (getHolder()) {
            sprite = new Sprite(this, bmp, x, y, gotox, gotoy);

        }
    }
    return true;
}

}

Class Sprite:

package com.example.name;

import android.graphics.Bitmap;
import android.graphics.Canvas;


public class Sprite {


private float a;
private float b;

private float x;
private float y;
private float gotox;
private float gotoy;
private int executeMove = 0;
private Display display;
private Bitmap bmp;


public Sprite(Display display, Bitmap bmp, float x, float y, float gotox,
        float gotoy) {
    this.display = display;
    this.bmp = bmp;
    this.x = x;
    this.y = y;
    this.gotox = gotox;
    this.gotoy = gotoy;
}

void update() {

    if (x < gotox) {x++;executeMove = 1;}
    if (x > gotox) {x--;executeMove = 1;}

    if (executeMove == 1) {move();}
    executeMove = 0;


}

void move() {
    float x1 = x;
    float y1 = y;
    float x2 = gotox;
    float y2 = gotoy;

    a = (y2-y1)/(x2-x1);
    b = y1 - x1*a;
    y = x1 * a + b;


}


public float ostatniaWartoscX() {
    return x;
}

public float ostatniaWartoscY() {
    return y;
}

public void onDraw(Canvas canvas) {
    update();
    canvas.drawBitmap(bmp, x, y, null);
}

}

Thank You!

You need Bresenham's line algorithm to move your player. You can even cut it short that you only need to calculate the next movement (not the whole line). It's a simple/easy and low-calorie-algorithm.

You must adjust it to your needs.

public static ArrayList<Point> getLine(Point start, Point target) {
    ArrayList<Point> ret = new ArrayList<Point>();

    int x0 =  start.x;
    int y0 =  start.y;

    int x1 = target.x;
    int y1 = target.y;

    int sx = 0;
    int sy = 0;

    int dx =  Math.abs(x1-x0);
    sx = x0<x1 ? 1 : -1;
    int dy = -1*Math.abs(y1-y0);
    sy = y0<y1 ? 1 : -1; 
    int err = dx+dy, e2; /* error value e_xy */

    for(;;){  /* loop */
        ret.add( new Point(x0,y0) );
        if (x0==x1 && y0==y1) break;
        e2 = 2*err;
        if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
        if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
    }

    return ret;
}

Here is an example of code I use to move an object between two points.

Math.atan2 calculates the theta angle (-pi to +pi) which is the trajectory the object needs to travel. Delta is the time between this this update and the last update and velocity is the desired speed of the object. These all need to be multiplied together and then added to the current position to get the new position.

@Override
protected void update(float delta) {
        double theta = Math.atan2(targetPos.y - pos.y, targetPos.x - pos.x);

        double valX = (delta * velocity) * Math.cos(theta);
        double valY = (delta * velocity) * Math.sin(theta);

        pos.x += valX;
        pos.y += valY;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM