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

2015/9/21

Stocking numbers in an array

Vellyxenya Vellyxenya

2015/9/21

#
Hi, in the following codes, i creates a missile that is fired by an enemy and which must seek my ship, but I want the missile to be destroyed if his rotation angle is too big Here's the code of my missile class:
private int timer;
    public static final int damage = 100;
    private int counter = 0;
    public boolean seeking;
    int[] rotations = new int[1000];
    public int i = 1;

    public Missile(int rotation)
    {
        setRotation(rotation);
        timer = 0;
    }

    public void act() 
    {
        if(isPaused() == false)
        {
            count();
            vanish();
            disappear();
            movements();
            timer++;
            faceGear();
        }
    }

    private void movements()
    {
        move(3);
    }

    public void faceGear()
    {
        
        if(getWorld() != null)
        {
            if(timer % 10 == 0)
            {
                if(Math.abs(rotations[i]-rotations[(i-1)])<90)
                {
                    Actor gear = (Actor)getWorld().getObjects(Gears.class).get(0);
                    int xGear = gear.getX();
                    int yGear = gear.getY();
                    this.turnTowards(xGear, yGear);
                    rotations[i] = this.getRotation();
                    i++;
                }
                else if (Math.abs(rotations[i]-rotations[(i-1)])>=90)
                {
                    getWorld().removeObject(this);
                }
            }
        }
    }
and the code of the enemy class, which fires the missile:
private void fire()
    {
        if(timer>= 100 && attackTimer>= ATTACK_TIMER)
        {
            Missile missile = new Missile(this.getRotation()+180);
            getWorld().addObject(missile, this.getX(), this.getY());
            attackTimer = 0;
        }
    }
The problem is that the missile is destroyed as soon as his getRotation is higher than 90°, but i only want it to be destroyed when the difference between a rotation (i) and the rotation(i-1) is higher than 90°... Could you help me please?^^
danpost danpost

2015/9/21

#
There is no need to track past rotational values. That is, you can acquire any needed values during a single act cycle -- no array or field needs to be declared for holding any rotational values between any consecutive act cycle. All you are trying to do is to determine if the before turn rotation is more than 90 degrees different from the after turn value.
int currentRotation = getRotation();
turnTowards(xGear, yGear);
int newRotation = getRotation();
// if change in rotation passes the zero degree mark
if (Math.abs(currentRotation-newRotation) > 180)
{
    if (currentRotation < 180) currentRotation += 360;
    else newRotation += 360;
}
// if change in rotation is greater than ninety degrees
if (Math.abs(currentRotation-newRotation) >= 90)
{
    // etc.
As for lines 4 through 9, the rotational values need to be checked in case the rotation during turning passes over the zero degree mark where the value changes by 360 degrees (that is, 359 incremented becomes zero and zero decremented becomes 359). One of the two values would need to be adjusted so that the difference between them is calculated to the proper value.
Vellyxenya Vellyxenya

2015/9/21

#
Thank you very much danpost, for all your help :D
You need to login to post a reply.