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

2019/4/6

error in terminal saying n must be positive

Erasedsword Erasedsword

2019/4/6

#
the code resulting in this error is
       if(Greenfoot.getRandomNumber(Math.abs(fireRate)) ==1)
        {
            u_Lazer u_Lazer = new u_Lazer();
            getWorld().addObject(u_Lazer, getX()-40, getY());
            Greenfoot.playSound("shot.wav");
        }
to be more specific its the if statement in this code. the goal of this code it to get the actor to fire at random speeds however the values for this code are in the world class, where this actor is. it is affected with both positive and negative numbers causing this error to appear.
danpost danpost

2019/4/6

#
Try:
if (Greenfoot.getRandomNumber(1+Math.abs(fireRate)) == 1)
Erasedsword Erasedsword

2019/4/6

#
well it runs, however now it won't spawn in the lasers. the code for the class is as fallows
    // sets a timer for zero, and sets a value for moving up and down
    private int timer = 0, ydirection = -3;
    //sets shooting rate
    private int fireRate;
    /**
     * changes the image when is created
     */
    public Ufo(int shotSpeed)
    {
        shotSpeed = fireRate;
        assignImage();
    }

    /**
     * changes the image size
     */
    private void assignImage()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() +25, image.getHeight() +10);
    }

    /**
     * Moves Randomly 
     **/
    public void AIMovement()
    {
        int y=getY();
        timer++;
        if (timer > 50 && Greenfoot.getRandomNumber(20)>=19 || ydirection==-1 && getY() < 50 || getY()>getWorld().getHeight()-10)
        {
            timer=0;
            ydirection *= -1;
        }

        if (y<=75)
        {
            setLocation (getX(), y+5);
        }
        setLocation(getX(),getY()+ydirection);
    }

    /**
     * spawns in bombs and plays shooting sound
     */
    public void fire()
    {
        if (Greenfoot.getRandomNumber(1+Math.abs(fireRate)) == 1)
        {
            u_Lazer u_Lazer = new u_Lazer();
            getWorld().addObject(u_Lazer, getX()-40, getY());
            Greenfoot.playSound("shot.wav");
        }
    }

    /**
     * adds points if the hp reaches 0, and remove the enemy
     */
    public void Destroy()
    {
        Levels levels = (Levels)getWorld();
        if (levels.getHpE() <=0)
        {
            getWorld().removeObject(this);
        }
    }

    /**
     * Shoot and move
     */
    public void act() 
    {
        AIMovement();
        fire();
    }    
the abstraction in the constructors values are 105 for level1, and 95 in level 2. as you can see I'm trying to increase the pass the lasers spawn in(setting minimum so that they don't constantly move across the entire screen). i hope this helps
Erasedsword Erasedsword

2019/4/6

#
i have checked with changing the == operator and the value that comes after it, and i have concluded that it is generating a number somewhere larger than 100, but won't ever hit 1000.
danpost danpost

2019/4/6

#
Line 10 is backward.
You need to login to post a reply.