This site requires JavaScript, please enable it in your browser!
Greenfoot back
bangtan
bangtan wrote ...

2020/3/17

shooting

bangtan bangtan

2020/3/17

#
as of now, I have 4 classes wherein each class represents a shot, one for shooting above, one for shooting down, one for shooting left, one for shooting right, and they're ship is the main actor, how can I make all the shots into one class only but can also shoot in all directions using the arrow keys
danpost danpost

2020/3/17

#
Show the codes you are currently using.
bangtan bangtan

2020/3/17

#
this is the code for the main actor
public void shoot(){
        land landWorld = (land) getWorld();
        counter counter = landWorld.getCounter();
        if (counter.totalCount != 0){
            if (shotTimer > 0) {
                shotTimer = shotTimer - 1;
            } else if (Greenfoot.isKeyDown("W")) {
                getWorld().addObject(new bullet(this), getX(), getY());
                shotTimer = 30; 
                counter.bumpCount(-1);
            } else if (Greenfoot.isKeyDown("A")){
                getWorld().addObject(new bullet2(this), getX(), getY());
                shotTimer = 20; 
                counter.bumpCount(-1);
            } else if (Greenfoot.isKeyDown("S")){
                getWorld().addObject(new bullet3(this), getX(), getY());
                shotTimer = 20; 
                counter.bumpCount(-1);
            } else if (Greenfoot.isKeyDown("D")){
                getWorld().addObject(new bullet4(this), getX(), getY());
                shotTimer = 20; 
                counter.bumpCount(-1);
            } 
        }
    }

this is my only code for all the bullet classes, but different coordinates
public shot(main myShip)
    {
        this.myShip = myShip;
    }



public void act() 
    {

        int ypos = getY();
        if((this.getY() < 5 || this.getY() > this.getWorld().getHeight() - 5) && (this != null))  
        {  
            this.getWorld().removeObject(this);  
            return;
        }  
 
        if (ypos > 0) {
            ypos = ypos - 5;
            setLocation(getX(), ypos);
            Actor enemy = getOneIntersectingObject(enemy.class);
            if (enemy != null) {
                getWorld().removeObject(this);

            }
        }
    }
danpost danpost

2020/3/17

#
So as not to repeat codes, get the direction (dependent on all keys) first:
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("W")) dy--;
if (Greenfoot.isKeyDown("A")) dx--;
if (Greenfoot.isKeyDown("S")) dy++;
if (Greenfoot.isKeyDown("D")) dx++;
if (dx*dy != 0 || dx+dy == 0) return;
The last line ensure one is zero and one is non-zero. The next line can be the following:
int rot = ((1-dx)+(dy-1)*dy)*90;
This will be the rotation the new bullet should take on:
Bullet bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(rot);
You may want to re-position the bullet so it does not start at the center of the ship with something like:
bullet.move(30);
In class of bullet, move with:
move(5);
bangtan bangtan

2020/3/18

#
should I put this in the class of the main actor?
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("W")) dy--;
if (Greenfoot.isKeyDown("A")) dx--;
if (Greenfoot.isKeyDown("S")) dy++;
if (Greenfoot.isKeyDown("D")) dx++;
if (dx*dy != 0 || dx+dy == 0) return;
int rot = ((1-dx)+(dy-1)*dy)*90;
Bullet bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(rot);
danpost danpost

2020/3/18

#
bangtan wrote...
should I put this in the class of the main actor? << Code Omitted >>
In your shoot method.
bangtan bangtan

2020/3/23

#
so ill put this in shoot and remove the shoot method in the main actor?
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("W")) dy--;
if (Greenfoot.isKeyDown("A")) dx--;
if (Greenfoot.isKeyDown("S")) dy++;
if (Greenfoot.isKeyDown("D")) dx++;
if (dx*dy != 0 || dx+dy == 0) return;
int rot = ((1-dx)+(dy-1)*dy)*90;
Bullet bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(rot);
danpost danpost

2020/3/23

#
In the shoot method you already have, replacing all codes after the first "else" except the last 2 lines.
You need to login to post a reply.