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

2020/11/10

Method won't get called.

ItzLukeStorm ItzLukeStorm

2020/11/10

#
So I have an actor that has a method called "shot()" that won't get called for some reason. Does anyone see something wrong with my code?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pirahna here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pirahna extends Actor
{
    private int speed = 4;
    private int leftTurn = 910;
    private int rightTurn = 1020;

    private int shotTime = 20;

    public void act() 
    {
        shot();
        setLocation(getX() + speed, getY() );

        if (atTP())
        {
            speed = - speed;
        }

        switchPic();
    }
    
    public void shot()
    {
        shotTime--;
        if (shotTime == 0)
        {
            getWorld().addObject(new Orb(), getX() -50, getY());
        }
        shotTime = 20;
    }

    public void switchPic()
    {
        if(speed == 4 && !isTouching(Ketchup.class) || speed == 4 && !isTouching(Musturd.class))
        {
            setImage("pirahna1.png");
        }
        if(speed == -4 && !isTouching(Ketchup.class) || speed == -4 && !isTouching(Musturd.class))
        {
            setImage("pirahna.png");
        }
        if(speed == 4 && isTouching(Ketchup.class) || speed == 4 && isTouching(Musturd.class))
        {
            setImage("pirahna3.png");
        }
        if(speed == -4 && isTouching(Ketchup.class) || speed == -4 && isTouching(Musturd.class))
        {
            setImage("pirahna2.png");
        }
    }

    public boolean atTP()
    {
        return (getX() <= leftTurn || getX() >= rightTurn);
    }
}
danpost danpost

2020/11/10

#
ItzLukeStorm wrote...
So I have an actor that has a method called "shot()" that won't get called for some reason. Does anyone see something wrong with my code? << Code Omitted >>
The method gets called; but, in doing so, the value of shotTime is unconditionally set to '20'; therefore, no orb is ever added to the world.
You need to login to post a reply.