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.

Im creating a 2d game for android.

I'll just get right to it. I have a sprite controlled by a joystick that needs to shoot, sofar Ive managed to get direction and movement on my bullet, but then I get stuck.

First of I need a delay for my bullet, cus right now its shooting like 10 bullets/sec, and you prolly know how that looks, like a beam of bullets.

Second there is a problem with the way I update the location for my bullet, my bullet position gets updated every time my sprite-position gets updated. This means that every time I move my character the previous bullets I've fired stop moving, so to be clear, the direction and speed of my bullet is fine, but the way it spawns and updates the old bullets when I move my sprite ( dragging gets set to true ) is really buggy. Maybe every element in my arraylist need a boolean for when its created?

Ill post some relevant code here:

Code for sprite/bullet positon, direction and speed:

    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;


        pointerPosition.y += Math.sin(angle * (Math.PI / 180))
                * (touchingPoint.y / 90);
        pointerPosition.x += Math.cos(angle * (Math.PI / 180))
                * (touchingPoint.y / 90);
        bulletPos.y = pointerPosition.y; //this is what messes up my bullet-position, if I move sprite, the existing bullets stop, so I cant move and shoot
        bulletPos.x = pointerPosition.x; 

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

    }

    if(shooting){       
        bulletPos.y += Math.sin(angle * (Math.PI / 180)) //!!!What I removed: * 5;
        bulletPos.x += Math.cos(angle * (Math.PI / 180)) //!!! here to: * 5;
    }

Code for drawing my bullet:

    private void shootingControl(Canvas canvas){ //is called in drawing-method
    if(controls.getShooting() == true){             
         bullets.add(new Bullets(bullet, controls.bulletPos.x, controls.bulletPos.y));
         }

    for(int i = 0; i < bullets.size(); i++){
     bullets.get(i).update(); //!!!!added this
        bullets.get(i).draw(canvas);
    }

}

Code for bullet-class:

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

    //!!!What I tried adding!!!
    public void update(){
    X *=5;
    Y *=5;

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

I've gotten some help from here alrdy regarding some of this, and I kinda feel bad for asking again, but I cant find anything anywhere and I cant figure it out on my own, really tired of being stuck here.

Ive seen some people using gametime in their bullet-positions and alot of people use vectors. Or if you can redirect me to a good tutorial. I just want to get past this stage of my game!

Thank you!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Your game should have a global delta time. If not you can create it yourself by checking the time between the last frame and current frame. Your shooting method should look like:

if(shooting)
{
   delayAcc += delta;
   if(delayAcc > shootDelay){
        delayAcc = 0;
        //Do shooting here.
   }

}
else{
    delayAcc = shootDelay+1; // Makes sure player shoots when button is pressed.
}

I wonder why you aren't just moving the velocity of the bullet to its own class? You could simply create a base object that has the position and velocity. And an update function that adds the velocity to the position. This update method is then called in an update loop ( obviously with an array of bullets) Use an object pool to reuse these objects when you "destroy" them. The update method could probably come from an interface to allow polymorphism.

I have the feeling your velocity is lost once it goes out of scope or is overridden. And therefor your old bullets don't budge because they were depending on it.

Try having a central point, like a factory, that creates objects for you. Don't let the player create objects directly. Instead it should call a different object that returns an object for the player, or directly passes it onto your managers (and by managers I mean the objects that handle things such as your bullets ).

You can do everything with the x and y variables as you could with a vector. The only difference is probably that the Vector class has many functions already implemented. But for a simple movement, adding the velocity to the positions x and y would be sufficient.

Your class name Bullets should be Bullet btw. Bullets implies something else than a single bullet =).

share|improve this answer
    
thanks for the answer! will try this out, can you explain what initial values delayAcc and shootDelay have? never worked with this b4 so. and btw, its named bullets cus at first I wanted more then 1 type of bullet ^^ –  iQue Aug 20 '12 at 22:21
    
delayAcc should start with a higher number than shootDelay. If you set it to 0 the else clause will ensure it anyways. The shootDelay is your time until you can shoot again. It depends on your measurement of your delta time. Some systems take their delta as a float or double. A really small number. Or an int which seems bigger and usually needs to be divided by 1000 ( i think ). Also Bullets is still wrong in that context. The class describes a bullet. No matter the visual context and damage. –  Sidar Aug 20 '12 at 23:25
    
thanks man :) saved me alot of hours –  iQue Aug 20 '12 at 23:50
    
If you are satisfied with my answer please click the the check mark ( under the up or downvote). So that people know that you got your answer. –  Sidar Aug 20 '12 at 23:54
    
I will as soon as I implement it 2morrow :) dont worry! –  iQue Aug 21 '12 at 1:46

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.