Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

java - Android stop sprite from changing subimages when staying

Currently my sprite animates like it was in movement not only when is in movement but also when it stays in one place. Of course I want to stay still without animation when it stays in one place. How to solve that?

public abstract class GameMovingObject {
    
    private static final int ROW_TOP_TO_BOTTOM = 0;
    private static final int ROW_RIGHT_TO_LEFT = 1;
    private static final int ROW_LEFT_TO_RIGHT = 2;
    private static final int ROW_BOTTOM_TO_TOP = 3;
    
    public boolean justSeen=true;
    protected Bitmap image;
    private final int rowCount, colCount;
    protected final int WIDTH, HEIGHT;
    private final int width, height;
    private int x;
    public int getX() { return this.x; }
    public void setX(int x) { this.x = x; }
    private int y;
    public int getY() { return this.y; }
    public void setY(int y) { this.y = y; }
    
    // Row index of Image are being used.
    private int rowUsing = ROW_LEFT_TO_RIGHT;
    
    private int colUsing;
    
    private Bitmap[] leftToRights;
    private Bitmap[] rightToLefts;
    private Bitmap[] topToBottoms;
    private Bitmap[] bottomToTops;
    
    // Velocity of game character (pixel/millisecond)
    public float velocity = 0.15f;
    
    public int getMovingVectorX() {
        return movingVectorX;
    }
    public int getMovingVectorY() {
        return movingVectorY;
    }
    
    public int movingVectorX = 0;
    public int movingVectorY = 0;
    
    public long lastDrawNanoTime =-1;
    
    public GameSurface gs;
    
    public GameMovingObject(GameSurface gs, Bitmap image, int rowCount, int colCount, int x, int y) {
    
        this.gs = gs;
    
        this.image = image;
        this.rowCount = rowCount;
        this.colCount = colCount;
    
        this.x = x;
        this.y = y;
    
        this.WIDTH = image.getWidth();
        this.HEIGHT = image.getHeight();
    
        this.width = this.WIDTH / colCount;
        this.height = this.HEIGHT / rowCount;
    
        this.topToBottoms = new Bitmap[colCount]; // 3
        this.rightToLefts = new Bitmap[colCount]; // 3
        this.leftToRights = new Bitmap[colCount]; // 3
        this.bottomToTops = new Bitmap[colCount]; // 3
    
        for(int col = 0; col< this.colCount; col++ ) {
            this.topToBottoms[col] = this.createSubImageAt(ROW_TOP_TO_BOTTOM, col);
            this.rightToLefts[col]  = this.createSubImageAt(ROW_RIGHT_TO_LEFT, col);
            this.leftToRights[col] = this.createSubImageAt(ROW_LEFT_TO_RIGHT, col);
            this.bottomToTops[col]  = this.createSubImageAt(ROW_BOTTOM_TO_TOP, col);
        }
    }
    
    public Bitmap[] getMoveBitmaps()  {
        switch (rowUsing)  {
            case ROW_BOTTOM_TO_TOP:
                return  this.bottomToTops;
            case ROW_LEFT_TO_RIGHT:
                return this.leftToRights;
            case ROW_RIGHT_TO_LEFT:
                return this.rightToLefts;
            case ROW_TOP_TO_BOTTOM:
                return this.topToBottoms;
            default:
                return null;
        }
    }
    
    public void setMovingVector(int movingVectorX, int movingVectorY)  {
        this.movingVectorX= movingVectorX;
        this.movingVectorY = movingVectorY;
    }
    
    public Bitmap getCurrentMoveBitmap()  {
        Bitmap[] bitmaps = this.getMoveBitmaps();
        return bitmaps[this.colUsing];
    }
    
    public void draw(Canvas canvas)  {
        Bitmap bitmap = this.getCurrentMoveBitmap();
        canvas.drawBitmap(bitmap,x, y, null);
        // Last draw time.
        this.lastDrawNanoTime= System.nanoTime();
    }

    public void update()  {
        this.colUsing++;
        if(colUsing >= this.colCount)  {
            this.colUsing =0;
        }
        // Current time in nanoseconds
        long now = System.nanoTime();
    
        // Never once did draw.
        if(lastDrawNanoTime==-1) {
            lastDrawNanoTime= now;
        }
        // Change nanoseconds to milliseconds (1 nanosecond = 1000000 milliseconds).
        int deltaTime = (int) ((now - lastDrawNanoTime)/   777777 );
    
        // Distance moves
        float distance = velocity * deltaTime;
    
        double movingVectorLength = Math.sqrt(movingVectorX* movingVectorX + movingVectorY*movingVectorY);
    
        // Calculate the new position of the game character.
        this.x = x +  (int)(distance* movingVectorX / movingVectorLength);
        this.y = y +  (int)(distance* movingVectorY / movingVectorLength);
    
        // When the game's character touches the edge of the screen, then change direction
    
        if(this.x < 0 ) {
            this.x = 0;
            this.movingVectorX = - this.movingVectorX;
        } else if(this.x > this.gs.getWidth() -width) {
            this.x= this.gs.getWidth()-width;
            this.movingVectorX = - this.movingVectorX;
        }
    
        if(this.y < 0 ) {
            this.y = 0;
            this.movingVectorY = - this.movingVectorY;
        }
    
        // rowUsing (obraca posta?)
        if( movingVectorX > 0 ){
            if(movingVectorY > 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
                this.rowUsing = ROW_TOP_TO_BOTTOM;
            }
            else if(movingVectorY < 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
                this.rowUsing = ROW_BOTTOM_TO_TOP;
            }
            else  {
                this.rowUsing = ROW_LEFT_TO_RIGHT;
            }
        }
        else
        {
            if(movingVectorY > 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
                this.rowUsing = ROW_TOP_TO_BOTTOM;
            }
            else if(movingVectorY < 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
                this.rowUsing = ROW_BOTTOM_TO_TOP;
            }
            else if(movingVectorX!=0 || movingVectorY!=0)  {
                this.rowUsing = ROW_RIGHT_TO_LEFT;
            }
        }
    }
    
    protected Bitmap createSubImageAt(int row, int col) {
        // createBitmap(bitmap, x, y, width, height).
        Bitmap subImage = Bitmap.createBitmap(image, col * width, row * height, width, height);
        return subImage;
    }
    
    public int getHeight() {
        return height;
    }
    
    public int getWidth() {
        return width;
    }

I was trying to add

if(movingVectorY!=0) this.topToBottoms[col] = this.createSubImageAt(ROW_TOP_TO_BOTTOM, col);
else this.topToBottoms[1] = this.createSubImageAt(ROW_TOP_TO_BOTTOM, 1);

but then sprite blinks and is without animation even when moving. I think I added everything you need to know. It there is anything you want me to add just ask me. Thank you in advance.

question from:https://stackoverflow.com/questions/65869756/android-stop-sprite-from-changing-subimages-when-staying

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...