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

2013/12/31

Crowding at the left corner

chrl98 chrl98

2013/12/31

#
Hi, For some reason, my EnemyTruck objects are all crowding at the top left corner of the game world. I have programmed it so that the EnemyTruck objects will follow the Soldier when the soldier is moving. *Note: This is not my own project. I am modifying it solely for school-related purposes.* Code for EnemyTruck-

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.lang.Object;
import java.lang.Class;
import java.util.List;


public class EnemyTruck extends Truck implements LandUnit
{
    private static final int ROTATION_SPEED = 3;
    private static final double ACCELERATION = .1;
    private static final double MAX_SPEED = 4;
    private GreenfootImage background;
    private boolean Startgame = false;
    private boolean clicked = false;

    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    public void fire()
    {
        getWorld().addObject(new Projectile(getRotation(), speed, getRotation()), getX(), getY());
        Greenfoot.playSound("BM2.mp3");
         if (isPlayer())
            getWorld().addObject(new Projectile(Greenfoot.isKeyDown("a") ? -1 : Greenfoot.isKeyDown("d") ? 1 : 0, Greenfoot.isKeyDown("w") ? -1 : Greenfoot.isKeyDown("s") ? 1 : 0, MAX_SPEED, getRotation()), getX() + (int)(Math.cos(Math.toRadians(getRotation())) * (-14 + 26) + Math.cos(Math.toRadians(getRotation() + 90)) * (-14 + 19)), getY() + (int)(Math.sin(Math.toRadians(getRotation() + 90)) * (-14 + 19) + Math.sin(Math.toRadians(getRotation())) * (-14 + 26)));
        else
            getWorld().addObject(new Projectile(getRotation(), speed, getRotation()), getX() + (int)(Math.cos(Math.toRadians(getRotation())) * (-14 + 26) + Math.cos(Math.toRadians(getRotation() + 90)) * (-14 + 19)), getY() + (int)(Math.sin(Math.toRadians(getRotation() + 90)) * (-14 + 19) + Math.sin(Math.toRadians(getRotation())) * (-14 + 26)));
    }
    protected boolean hitsSomething()
    {
        return super.hitsSomething() || hitsFence() || hitsTree() || hitsLandUnits();
    }
    protected void go()
    {
        slowDown();
    }
    protected int getRotationSpeed()
    {
        return ROTATION_SPEED;
    }
    protected double getAcceleration()
    {
        return ACCELERATION;
    }
    protected double getMaxSpeed()
    {
        return MAX_SPEED;
    }
    public void act()
    {
        moveAround();     
        turnTowards(Soldier.playerX,Soldier.playerY);
        lookForObject();
    }
    public void moveAround()
    {
       if (Greenfoot.getRandomNumber(100) < 5)
       {
            turn(Greenfoot.getRandomNumber(20) - 5 ); 
            move(20);
            turn(56);
       }
       if ( Greenfoot.getRandomNumber(100) < 5 )
       {
            move(10);
            turn(45);
            move(35);
       }
       
       if (getY() <= 3 || getY() >= getWorld(). getHeight() - 3)
       {
            move(20);
       }
    }
    public void lookForObject()
    {
        if(canSee(Soldier.class))
        {
           fire();
        }
        if(canSee(Helicopter.class))
        {
          fire();
        }
        if(canSee(Tank.class))
        {
          fire();
        }
    }
    
    public void turnTowards (Actor Soldier)
    {
       turnTowards(Soldier.getX(),Soldier.getY());
    }
}

chrl98 chrl98

2013/12/31

#
BTW, my EnemyTruck Objects are supposed to follow the soldier by the turnTowards method and lookForObject method (lines 77-96)
chrl98 chrl98

2013/12/31

#
Someone please help? Thanks. :)
danpost danpost

2013/12/31

#
The problem probably resides in the Soldier class (or a super class of it) where the 'playerX' and 'playerY' fields are and what code you have to set the value of these fields and keep those values current.
chrl98 chrl98

2014/1/1

#
Fixed. Thanks!
You need to login to post a reply.