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

2020/3/25

Make Enemy Shoot Direction it is facing

tcbcRyan tcbcRyan

2020/3/25

#
I am trying to figure out how to make it so that when the Enemy image is set to the one of it facing left, it will shoot left and then when it faces right I want it to shoot right. Here is the code I have so far and currently it does nothing. I'm not sure how to get it working so if anyone could help out then thank you very much! Also I should add that I took this code and changed it a little bit from another that was needing something similar but for my need it is not working out.
private int spawnTimer;

    private GreenfootImage EnemyRight = new GreenfootImage("EnemyRight.png");
    private GreenfootImage EnemyLeft = new GreenfootImage("Enemy.png");

    private int minShotDelay = 40;
    private int maxShotDelay = 160;
    private int shotTimer = minShotDelay;
    // in 'act' method or a 'spawnBullet' method it calls
    private void checkForSpawning() // call from act method
    {
        spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
        if (spawnTimer == 0) // at each timer reset
        {
            { if (getImage().equals(EnemyRight)) {
                    getWorld().addObject(new vomitRight(getRotation()), getX(), getY());
                    shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
                } else if (getImage().equals(EnemyLeft)) {
                    getWorld().addObject(new vomitLeft(getRotation()), getX(), getY());
                    shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
                } 
              else shotTimer--;
            }
        }
    }
danpost danpost

2020/3/25

#
In line 16, replace "getRotation()" with "0" and in line 19 with "180".
tcbcRyan tcbcRyan

2020/3/25

#
I assume that fixed one of the issues I have but the enemies aren't firing on their own. I am trying to get them to shoot at a random time from the minShotDelay and maxShotDelay. Could you please help me out with making it do that?
danpost danpost

2020/3/25

#
tcbcRyan wrote...
I assume that fixed one of the issues I have but the enemies aren't firing on their own. I am trying to get them to shoot at a random time from the minShotDelay and maxShotDelay. Could you please help me out with making it do that?
I assumed that you did not show all the codes in the class, leaving out the act method. You do have an act method -- don't you? ... and it calls this spawning method? (you even commented to "\\ call from act method")
tcbcRyan tcbcRyan

4 days ago

#
Yes I have called it in the act method
danpost danpost

4 days ago

#
tcbcRyan wrote...
Yes I have called it in the act method
Please show entire class codes.
tcbcRyan tcbcRyan

3 days ago

#
Okay, here is the entire code for my character,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class enemy extends Actor
{
    public int steps;
    public int speed = 3;
    public int rSpeed = -3;

    public int enemyRotation;
    /**
     * Act - do whatever the enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        patrolArea();

        checkForSpawning();
        enemyRotation = getRotation();

        die();
    }    

    private void patrolArea()
    {
        move(speed);
        if (speed > 0 && isTouching(invisibleBarrierRight.class))
        {
            speed = -speed;
            setImage(EnemyLeft);
        }
        if (speed < 0 && isTouching(invisibleBarrier.class))
        {
            speed = -speed;
            setImage(EnemyRight);
        }
    }

    private int spawnTimer;
    private GreenfootImage EnemyRight = new GreenfootImage("EnemyRight.png");
    private GreenfootImage EnemyLeft = new GreenfootImage("Enemy.png");

    private int minShotDelay = 40;
    private int maxShotDelay = 160;
    private int shotTimer = minShotDelay;
    // in 'act' method or a 'spawnBullet' method it calls
    private void checkForSpawning() // call from act method
    {
        spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
        if (spawnTimer == 0) // at each timer reset
        {
            { if (getImage().equals(EnemyRight)) {
                    getWorld().addObject(new vomitRight(0), getX(), getY());
                    shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
                } else if (getImage().equals(EnemyLeft)) {
                    getWorld().addObject(new vomitLeft(180), getX(), getY());
                    shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
                } 
                else shotTimer--;
            }
        }
    }

    public void die()
    {
        Actor bullet = getOneIntersectingObject(bullet.class);
        Actor bulletLeft = getOneIntersectingObject(bulletLeft.class);

        if(bullet!=null)
        {
            getWorld().removeObject(bullet);
            getWorld().removeObject(this);
        }else if(bulletLeft!=null)
        {
            getWorld().removeObject(bulletLeft);
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

3 days ago

#
It doesn't fire after hitting a barrier either?
tcbcRyan tcbcRyan

3 days ago

#
No it doesn't fire at all
danpost danpost

3 days ago

#
tcbcRyan wrote...
No it doesn't fire at all
It looks like you are using two different timers. Maybe that is what is causing the issue. Although, I do not see how as shotTimer does not seem to regulate the spawning of vomit shots. The extra set of squiggly brackets does not appear to cause any issues either.
danpost danpost

3 days ago

#
What codes do you have in your vomit classes?
tcbcRyan tcbcRyan

3 days ago

#
The only code I have in my vomit class is this so it moves depending on if it is 'vomitLeft' or 'vomitRight' having speed or -speed
public int speed = 10;

    public vomitLeft(int rotation)
    {
        setRotation(rotation);
    }
   
    public void act() 
    {
        move(speed);
    }   
tcbcRyan tcbcRyan

3 days ago

#
Oh I am so stupid, I managed to fix it. I didn't realise I had the zombies set to 10 seconds per shot
You need to login to post a reply.