# android basic Movement for array-objects

I was adviced to start a seperate question for this so here I go. Been stuck so long with this do if you can help, please do.

Lets get to it! Im trying to set the speed for my bullets, but since Im moving my sprite using a joystick Im finding it really hard. Ive tried about a hundred things but nothing rly works. Ill post code with comments below.

code for my controls:

        if (dragging) {

// get the pos
touchingPoint.x = (int) event.getX();
touchingPoint.y = (int) event.getY();

angle = Math.atan2(touchingPoint.y - inity,
touchingPoint.x - initx) / (Math.PI / 180);
spriteAngle = Math.atan2(touchingPoint.y - inity,
touchingPoint.x - initx) / (Math.PI / 2) + 2;

bulletSpeedX = (Math.sin(angle * (Math.PI / 180)) * 5);
bulletSpeedY = (Math.cos(angle * (Math.PI / 180)) * 5);

pointerPosition.y += Math.sin(angle * (Math.PI / 180)) //my Sprites position, gets update when Im dragging joystick, works fine.
* (touchingPoint.y / 90);
pointerPosition.x += Math.cos(angle * (Math.PI / 180))
* (touchingPoint.y / 90);
bulletPos.y = pointerPosition.y; //Since this updates every time Im dragging that means that the bullets that are alrdy shot stop moving when I drag, but I need this position.
bulletPos.x = pointerPosition.x;

}

else if (!dragging) {
touchingPoint.x = (int) initx;
touchingPoint.y = (int) inity;
}

if (event.getAction() == MotionEvent.ACTION_DOWN && (int) event.getX() > 650 && (int) event.getX() < 750
&& (int) event.getY() > 300 && (int) event.getY() < 400){

shooting = true; //when I press my button shooting turns on
}


Some problems with this code: if you read the comment about my bulletPos-variable you will see a problem. Also, If I set shooting to false when I stop pressing this button, then all my bullets dissapear, do I need some sort of exists-boolean for each object in my array, that turns false when my bullets reaches one of the borders of my world/hits an enemy?

Code for my bullet:

    public Bullets(Bitmap bitmap, int x, int y) {
this.bitmap=bitmap;
this.X=x;
this.Y=y;

}

public void draw(Canvas canvas){
canvas.drawBitmap(bitmap, X, Y, null);
}


problem with this code: Obviously I need some sort of update method for my position here, but if I want that I have to import and initialize my controls-class here, and if I do that the game crashes after a while for some reason.

Code for my drawing:

    private void shootingControl(Canvas canvas){ //gets called in onDraw()-method
if(controls.getShooting() == true){
if(System.currentTimeMillis() - lastShot > delay){
lastShot=System.currentTimeMillis();
bullets.add(new Bullets(bullet, controls.bulletPos.x, controls.bulletPos.y)); //adds a bullet in my arraylist at the position of my sprite
}
}

for(int i = 0; i < bullets.size(); i++){
bullets.get(i).draw(canvas);
updateBullet();
}

}
public void updateBullet(){ //this was one of my attempts at setting speed for each bullet, didnt work out to well
for(int i = 0; i < bullets.size(); i++){
bullets.get(i).setX((int)controls.getBulletSpeedX());
bullets.get(i).setY((int)controls.getBulletSpeedY());
if(i==bullets.size()){
i=0;
}
}
}


problems with this code: I tried putting my update method here instead of in my bullet-class since the game crashes if I import my controls into that one. But I cant get it to work anyway so..

As I understand it you get movement like this:

    newPosition = oldPosition + (speed * delta)


But since my bulletPosition gets updated everytime I drag I cant get that to work, cus whenever do it like the pseudo-code above the bullets thats alrdy been shot stops moving when my bulletPosition gets updated.

Im sorry for the long post but I rly want to be clear since Ive been stuck here for so so long, and I cant find anything on the webs that describes my problem, just similar problems, and Obviously cant get those to work so..

Ill add a link to a video of my game, so you can see how my sprite moves, just so I can get a clear answer, cus all this is very new to me, video: http://www.youtube.com/watch?v=nejaMtug09A&feature=youtube_gdata .

You shouldn't calculate the delta time within the player itself( I think it's the player because it isn't clear on where the pieces reside). The delta time should be passed on from the main loop onto your objects. This way every object receives an equal delta value.

You could move the movement to the bullet class Just for a simple illustration:

public class Bullet{
private float x; // x position
private float y; //y position

private float velX; // velocity X
private float velY; // velocity Y
private Bitmap bitmap;

public Bullet(Bitmap bitmap, float x, float y,float velX, float velY){
this.bitmap = bitmap;
this.x = x;
this.y = y;
this.velX = velY;
this.velY = velY;
}

public void update(float deltaTime){
x += velX*deltaTime; //Makes sure that we move velX pixels per second
y += velY*deltaTime; //Makes sure that we move velY pixels per second
}

public void draw(Canvas canvas){
canvas.drawBitmap(bitmap, x, y, null);
}
}


//Somewhere where you update your bullets
for(int i = 0; i < bulletList.size(); i++){
bulletList[i].update(deltaTime); //where deltaTime is your calculated delta.
}
//Afterwards draw them by calling the draw method.


The idea here is simple. The bullet class handles the movement internally. You just need to create one, set it's position and velocity. When the update method is called the velocity is added to the position. Afterwards when you call the draw function. The x and y of the calculated position are passed as the drawing position. Have a central point where delta time is calculated and passed on to the objects that need it.

bullets.add(new Bullets(bullet, controls.bulletPos.x, controls.bulletPos.y,controls.bulletSpeedX,controls.bulletSpeedY)); //adds a bullet in my arraylist at the position of my sprite