mardi 5 mai 2015

Android Development: How to initialise sprite with random direction

Having the following code, how do I initialise the sprite to move in the random direction once it spawns? I basicaly would like it to sometimes head over to the left side after it is spawned instead of always moving to the right, also it could fly up instead of going down all the time. I have absolutely no idea how to do it, I have tried switch/case method but I think I am not completely aware of how and where to use it. I would really appreciate any help with that.

Here is the code I have so far for sprite class:

package cct.mad.lab;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;  
public class Sprite {

//x,y position of sprite - initial position (0,50)
private int x = 0; 
private int y = 0;
private int xSpeed = 35;//Horizontal increment of position (speed)
private int ySpeed = 15;// Vertical increment of position (speed)
private GameView gameView;
private Bitmap spritebmp;
//Width and Height of the Sprite image
private int bmp_width;
private int bmp_height;
// Needed for new random coordinates.
private Random random = new Random();

public Sprite(GameView gameView) {
      this.gameView=gameView;
      spritebmp = BitmapFactory.decodeResource(gameView.getResources(),
              R.drawable.duck1);
      this.bmp_width = spritebmp.getWidth();
      this.bmp_height= spritebmp.getHeight();
      //random y coordinate for sprite spawn
      x = gameView.getWidth();
      x = random.nextInt(x);
      y = gameView.getHeight();
      y = random.nextInt(y);

 }
//update the position of the sprite
public void update() {
    x = x + xSpeed;
    y = y + ySpeed;
    wrapAround(); //Adjust motion of sprite.
}

public void draw(Canvas canvas) {

    //Draw sprite image
    canvas.drawBitmap(spritebmp, x , y, null);
}

public void wrapAround(){
    //Code to wrap around   
    if (x < 0) x = x + gameView.getWidth(); //increment x whilst not off screen
    if (x >= gameView.getWidth()){ //if gone of the right sides of screen
        xSpeed=-35;
        ySpeed=ySpeed;
            x = x - gameView.getWidth(); //Reset x
            x = gameView.getWidth();
              x = random.nextInt(x);
            y = gameView.getHeight();
         y = random.nextInt(y);
    }
    if(x + xSpeed < 0){
        xSpeed=35;
        ySpeed=ySpeed;
    }

    if (y < 0) y = y + gameView.getHeight();//increment y whilst not off screen
    if (y >= gameView.getHeight()){//if gone of the bottom of screen
        xSpeed=xSpeed;
        ySpeed=-15;
        y -= gameView.getHeight();//Reset y
    }
    if(y + ySpeed < 0){
        xSpeed=xSpeed;
        ySpeed=-15;

    }

}

/* Checks if the Sprite was touched. */
public boolean wasItTouched(float ex, float ey){
    boolean touched = false; 
    if ((x <= ex) && (ex < x + bmp_width) &&
        (y <= ey) && (ey < y + bmp_height)) {
            touched = true;
    }
    return touched;
}//End of wasItTouched 
}  




Aucun commentaire:

Enregistrer un commentaire