Skip to main content
added 188 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 26
  • 57
    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;
    }
    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);
    }
    
}
    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);
}
    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)) * 5;
        bulletPos.x += Math.cos(angle * (Math.PI / 180)) * 5;
    }
    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).draw(canvas);
    }
    
}
    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);
}
    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;
    }
    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);
    }
    
}
    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);
}
added 116 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 26
  • 57
    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)) * 5;
        bulletPos.x += Math.cos(angle * (Math.PI / 180)) * 5;
    }
    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;
        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)) * 5;
        bulletPos.x += Math.cos(angle * (Math.PI / 180)) * 5;
    }
    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)) * 5;
        bulletPos.x += Math.cos(angle * (Math.PI / 180)) * 5;
    }
Source Link
Green_qaue
  • 1.9k
  • 4
  • 26
  • 57

android game-logic for shooter

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;
        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)) * 5;
        bulletPos.x += Math.cos(angle * (Math.PI / 180)) * 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).draw(canvas);
    }
    
}

Code for bullet-class:

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

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!