游戏问题-拖动时向左或向右移动Drawable位图

时间:2018-11-24 14:56:15

标签: java android

我正在尝试为一个大学项目构建一个Android Studio游戏,到目前为止,我已经可以按照它很好地学习了。但是,我到了混凝土墙。我的想法是在屏幕上向左或向右拖动并按住时,将当前位于屏幕中间的字符水平移动。我对Java和android开发的知识非常有限,因此我真的不知道如何处理这种情况。目前,当我点击角色将他的炮弹射出他的炮弹时,我有一个if(action == MotionEvent.ACTION_DOWN)。有问题的位图在代码中被称为“驱逐舰”。

这是我的GameView类的代码

package com.example.moostauche.reindeerrage;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

//constructor
public class GameView extends View
{
    //in the GameView set the background to the gameBackground
    //variables
    Bitmap background, deerstroyer;
    Rect rect;

    static int displayWidth, displayHeight;
    ArrayList<Enemy1> enemies;
    ArrayList<Enemy2> enemies2;
    ArrayList<Santa> santas;
    ArrayList<Snow> snowing;
    ArrayList<Cloud> clouds;
    ArrayList<Cloud2> clouds2;
    ArrayList<Bullet> bullets;
    //import the handler
    Handler handler;
    //import runnable
    Runnable runnable;
    //how often do we want to update frames in ms
    final long UPDATE_MILLIS = 80;
    long start = System.currentTimeMillis();
    long end = start + 10 * 1000;

    static int deerstroyerWidth, deerstroyerHeight;
    Context context;
    int count = 0;

    public GameView( Context context )
    {
        super( context );
        this.context = context;
        //setting the background as our background image from drawable
        background = BitmapFactory.decodeResource( getResources(), R.drawable.christmasbackground );
        deerstroyer = BitmapFactory.decodeResource( getResources(), R.drawable.deerstroyer );

        //All the code below is to set the background image to fit the screen of the phone
        Display display = ( ( Activity ) getContext() ).getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize( size );
        displayWidth = size.x;
        displayHeight = size.y;
        rect = new Rect( 0, 0, displayWidth, displayHeight );
        enemies = new ArrayList<>();
        enemies2 = new ArrayList<>();
        santas = new ArrayList<>();
        snowing = new ArrayList<>();
        clouds = new ArrayList<>();
        clouds2 = new ArrayList<>();
        bullets = new ArrayList<>();

        for ( int i = 0; i < 1; i++ )
        {
            Enemy1 enemy = new Enemy1( context );
            enemies.add( enemy );
            Enemy2 enemy2 = new Enemy2( context );
            enemies2.add( enemy2 );
        }

        for ( int i = 0; i < 1; i++ )
        {
            Snow snow = new Snow( context );
            snowing.add( snow );
        }

        for ( int i = 0; i < 1; i++ )
        {
            Cloud cloud = new Cloud( context );
            clouds.add( cloud );
            Cloud2 cloud2 = new Cloud2( context );
            clouds2.add( cloud2 );
        }

        for ( int i = 0; i < 1; i++ )
        {
            Santa santa = new Santa( context );
            santas.add( santa );
        }

        //instantiate the handler
        handler = new Handler();

        //instantiate runnable - for redrawing the view
        runnable = new Runnable()
        {
            @Override
            public void run()
            {
                invalidate();
            }
        };

        deerstroyerWidth = deerstroyer.getWidth();
        deerstroyerHeight = deerstroyer.getHeight();
    }

    @Override
    protected void onDraw( Canvas canvas )
    {
        super.onDraw( canvas );

        //draw the background on the canvas
        canvas.drawBitmap( background, null, rect, null );

        for ( int i = 0; i < enemies.size(); i++ )
        {
            //draw enemy 1 and count the frames, if the enemy frames are greater than 3, reset the frames
            canvas.drawBitmap( enemies.get( i ).getBitmap(), enemies.get( i ).enemyX, enemies.get( i ).enemyY, null );
            enemies.get( i ).enemyFrame++;
            if ( enemies.get( i ).enemyFrame > 3 )
            {
                enemies.get( i ).enemyFrame = 0;
            }

            //enemy 1 moves from right to left
            enemies.get( i ).enemyX -= enemies.get( i ).movementSpeed;
            if ( enemies.get( i ).enemyX < -enemies.get( i ).getWidth() )
            {
                enemies.get( i ).resetPosition();
            }

            //draw enemy 2 and count the frames, if the enemy frames are greater than 3, reset the frames
            canvas.drawBitmap( enemies2.get( i ).getBitmap(), enemies2.get( i ).enemyX, enemies2.get( i ).enemyY, null );
            enemies2.get( i ).enemyFrame++;
            if ( enemies2.get( i ).enemyFrame > 3 )
            {
                enemies2.get( i ).enemyFrame = 0;
            }

            //enemy 2 moves from Up to Down
            enemies2.get( i ).enemyY += enemies2.get( i ).movementSpeed;
            if ( enemies2.get( i ).enemyY > ( displayHeight + enemies2.get( i ).getHeight() ) )
            {
                enemies2.get( i ).resetPosition();
            }
        }

        for ( int i = 0; i < santas.size(); i++ )
        {
            //draw Santa and count the frames, if the santa frames are greater than 3, reset the frames
            canvas.drawBitmap( santas.get( i ).getBitmap(), santas.get( i ).enemyX, santas.get( i ).enemyY, null );
            santas.get( i ).enemyFrame++;
            if ( santas.get( i ).enemyFrame > 3 )
            {
                santas.get( i ).enemyFrame = 0;
            }

            //Santa 1 moves from left to right
            santas.get( i ).enemyX -= santas.get( i ).movementSpeed;
            if ( santas.get( i ).enemyX < -santas.get( i ).getWidth() )
            {
                santas.get( i ).resetPosition();
            }
        }

        for ( int i = 0; i < snowing.size(); i++ )
        {
            //draw enemy 2 and count the frames, if the enemy frames are greater than 3, reset the frames
            canvas.drawBitmap( snowing.get( i ).getBitmap(), snowing.get( i ).enemyX, snowing.get( i ).enemyY, null );
            snowing.get( i ).enemyFrame++;
            if ( snowing.get( i ).enemyFrame > 3 )
            {
                snowing.get( i ).enemyFrame = 0;
            }
        }

        for ( int i = 0; i < clouds.size(); i++ )
        {
            //draw enemy 1 and count the frames, if the enemy frames are greater than 3, reset the frames
            canvas.drawBitmap( clouds.get( i ).getBitmap(), clouds.get( i ).enemyX, clouds.get( i ).enemyY, null );
            clouds.get( i ).enemyFrame++;
            if ( clouds.get( i ).enemyFrame > 3 )
            {
                clouds.get( i ).enemyFrame = 0;
            }

            //enemy 1 moves from right to left
            clouds.get( i ).enemyX -= clouds.get( i ).movementSpeed;
            if ( clouds.get( i ).enemyX < -clouds.get( i ).getWidth() )
            {
                clouds.get( i ).resetPosition();
            }
        }

        for ( int i = 0; i < clouds2.size(); i++ )
        {
            //draw enemy 1 and count the frames, if the enemy frames are greater than 3, reset the frames
            canvas.drawBitmap( clouds2.get( i ).getBitmap(), clouds2.get( i ).enemyX, clouds2.get( i ).enemyY, null );
            clouds2.get( i ).enemyFrame++;
            if ( clouds2.get( i ).enemyFrame > 3 )
            {
                clouds2.get( i ).enemyFrame = 0;
            }

            //enemy 1 moves from right to left
            clouds2.get( i ).enemyX += clouds2.get( i ).movementSpeed;
            if ( clouds2.get( i ).enemyX > ( displayWidth + clouds2.get( i ).getWidth() ) )
            {
                clouds2.get( i ).resetPosition();
            }
        }

        //bullet logic and destruction of different enemies
        for ( int i = 0; i < bullets.size(); i++ )
        {
            if ( bullets.get( i ).y > -bullets.get( i ).getBulletHeight() )
            {
                bullets.get( i ).y -= bullets.get( i ).bulletSpeed;
                canvas.drawBitmap( bullets.get( i ).bullet, bullets.get( i ).x, bullets.get( i ).y, null );
            }
            else
            {
                bullets.remove( i );
            }
        }

        //draw the deerstroyer elf and center him
        canvas.drawBitmap( deerstroyer, ( displayWidth / 2 - deerstroyerWidth / 2 ), displayHeight - deerstroyerHeight, null );

        //update the milliseconds
        handler.postDelayed( runnable, UPDATE_MILLIS );
    }
    //on touch event, shoot the bullet
    **

    @Override
    public boolean onTouchEvent( MotionEvent event )
    {
        float touchX = event.getX();
        float touchY = event.getY();
        int action = event.getAction();
        if ( action == MotionEvent.ACTION_DOWN )
        {
            //if you tap on the deerstroyer elf, he will shoot a bullet
            if ( touchX >= ( displayWidth / 2 - deerstroyerWidth / 2 ) 
                 && touchX <= ( displayWidth / 2 + deerstroyerWidth / 2 ) 
                 && touchY >= ( displayHeight - deerstroyerHeight ) )
            {
                //there can only be two bullets present on the screen at a time
                if ( bullets.size() < 3 )
                {
                    Bullet b = new Bullet( context );
                    bullets.add( b );
                }
            }
        }
        return true;
    }
    **
}

0 个答案:

没有答案