Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 .

Thanks for your time!

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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:


Your bullet class

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.

So in your case:

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

Funny thing is you have already been doing it with pointerPosition in a way. Look at it. You use the gamepads position to move the players position by adding to it.

I suggest looking at libgdx, as it provides a sophisticated library for gamedevelopment on Android.

share|improve this answer
    
thank you so much, I finally actually understand. <3<3<3 I was looking at libgdx but I kinda wanted to learn the basics in this game. Everything I do here is new to me. then for the next one I thought about trying libgdx :) –  iQue Aug 22 '12 at 0:17
    
Actually gamdevelopment on android was new for me too. So I jumped right away to libgdx. If it's the internals of android you would like to understand keep at it. If you simply want to create games. I say just jump to libgdx immediately. Eitherway, keep learning, keep doing what you do. –  Sidar Aug 22 '12 at 0:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.