Game problem - Moving the Drawable Bitmap Left or Right on dragging












0















I'm trying to build an Android Studio game for a college project and so far I was able to follow a nice tutorial on it. However, I've reached a concrete wall. My idea is to move the character that's currently positioned in the middle of the screen - horizontally when dragging and holding left or right on the screen. My knowledge on java and android development is very limited and therefore I really don't know how to handle this situation. Currently I have a if(action == MotionEvent.ACTION_DOWN) when tapping the character he shoots a cannon ball out of his cannon. The bitmap drawable in question is called "deerstroyer" in the code.



Here's the code of my GameView class



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









share|improve this question





























    0















    I'm trying to build an Android Studio game for a college project and so far I was able to follow a nice tutorial on it. However, I've reached a concrete wall. My idea is to move the character that's currently positioned in the middle of the screen - horizontally when dragging and holding left or right on the screen. My knowledge on java and android development is very limited and therefore I really don't know how to handle this situation. Currently I have a if(action == MotionEvent.ACTION_DOWN) when tapping the character he shoots a cannon ball out of his cannon. The bitmap drawable in question is called "deerstroyer" in the code.



    Here's the code of my GameView class



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









    share|improve this question



























      0












      0








      0








      I'm trying to build an Android Studio game for a college project and so far I was able to follow a nice tutorial on it. However, I've reached a concrete wall. My idea is to move the character that's currently positioned in the middle of the screen - horizontally when dragging and holding left or right on the screen. My knowledge on java and android development is very limited and therefore I really don't know how to handle this situation. Currently I have a if(action == MotionEvent.ACTION_DOWN) when tapping the character he shoots a cannon ball out of his cannon. The bitmap drawable in question is called "deerstroyer" in the code.



      Here's the code of my GameView class



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









      share|improve this question
















      I'm trying to build an Android Studio game for a college project and so far I was able to follow a nice tutorial on it. However, I've reached a concrete wall. My idea is to move the character that's currently positioned in the middle of the screen - horizontally when dragging and holding left or right on the screen. My knowledge on java and android development is very limited and therefore I really don't know how to handle this situation. Currently I have a if(action == MotionEvent.ACTION_DOWN) when tapping the character he shoots a cannon ball out of his cannon. The bitmap drawable in question is called "deerstroyer" in the code.



      Here's the code of my GameView class



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






      java android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 16:39









      Fantômas

      32.6k156389




      32.6k156389










      asked Nov 24 '18 at 14:56









      V.J. NagyV.J. Nagy

      12




      12
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459400%2fgame-problem-moving-the-drawable-bitmap-left-or-right-on-dragging%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459400%2fgame-problem-moving-the-drawable-bitmap-left-or-right-on-dragging%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python