Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By jim829

Thread: Method not find

  1. #1
    dominikremes is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Method not find

    Hello. I am working on a 2D game and I do not know why but from Class1 it cant find method in Class2 but from Class2 can find from Class1.
    Here is the code:
    Class1:
    Java Code:
    import java.util.ArrayList;
    
    import org.newdawn.slick.opengl.Texture;
    
    import static me.domi.towerdefense.helpers.Clock.*;
    import static me.domi.towerdefense.helpers.Artist.*;
    
    public abstract class Tower implements Entity {
    
    	private float x, y, timeSinceLastShot, firingSpeed, angle;
    	private int width, height, damage, range;
    	private Enemy target;
    	private Texture[] textures;
    	private ArrayList<Enemy> enemies;
    	private boolean targeted;
    	private ArrayList<Projectile> projectiles;
    
    	public Tower(TowerType type, Tile startTile, ArrayList<Enemy> enemies) {
    		this.textures = type.textures;
    		this.damage = type.damage;
    		this.range = type.range;
    		this.x = startTile.getX();
    		this.y = startTile.getY();
    		this.width = startTile.getWidth();
    		this.height = startTile.getHeight();
    		this.enemies = enemies;
    		this.targeted = false;
    		this.timeSinceLastShot = 0f;
    		this.projectiles = new ArrayList<Projectile>();
    		this.firingSpeed = type.firingSpeed;
    		this.angle = 0f;
    	}
    
    	private Enemy acquireTarget() {
    		Enemy closest = null;
    		float closestDistance = 10000;
    		for (Enemy e : enemies) {
    			if (isInRange(e) && findDistance(e) < closestDistance)
    				closestDistance = findDistance(e);
    			closest = e;
    		}
    		if (closest != null)
    			targeted = true;
    		return closest;
    	}
    
    	private boolean isInRange(Enemy e) {
    		float xDistance = Math.abs(e.getX() - x);
    		float yDistance = Math.abs(e.getY() - y);
    		if (xDistance < range && yDistance < range)
    			return true;
    		return false;
    	}
    
    	private float findDistance(Enemy e) {
    		float xDistance = Math.abs(e.getX() - x);
    		float yDistance = Math.abs(e.getY() - y);
    		return xDistance + yDistance;
    	}
    	
    	public void shoot() {
    		timeSinceLastShot = 0;
    		projectiles.add(new Projectile(QuickLoad("IceShoot"), target, x + TILE_SIZE / 2 - TILE_SIZE / 4,
    				y + TILE_SIZE / 2 - TILE_SIZE / 4, 32, 32, 900, 10));
    	}
    
    	private float calculateAngle() {
    		double angleTemp = Math.atan2(target.getY() - y, target.getX() - x);
    		return (float) Math.toDegrees(angleTemp) - 90;
    	}
    
    	public void update() {
    		if (!targeted) {
    			target = acquireTarget();
    		}
    
    		if (target == null || target.isAlive() == false)
    			targeted = false;
    
    		timeSinceLastShot += Delta();
    		if (timeSinceLastShot > firingSpeed)
    			                                               /*Here is the error*/ shoot();
    		for (Projectile p : projectiles)
    			p.update();
    
    		angle = calculateAngle();
    		draw();
    
    	}
    And now Class2:
    Java Code:
    import java.util.ArrayList;
    
    public class TowerIce extends Tower {
    
    	public TowerIce(TowerType type, Tile startTile, ArrayList<Enemy> enemies) {
    		super(type, startTile, enemies);
    	}
    	
    	@Override
    	public void shoot() {
    		System.out.println("Shoot");
    		super.shoot();
    		super.getTarget().setSpeed(1);
    	}
    }
    Thanks for help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method not find

    If you are getting error messages, please copy the full text and paste it here.

    What is the name of the method(s) that are the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dominikremes is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Re: Method not find

    No there is no error message, bu do not find that Overrided shoot from class2 in class1 in line 82 but it find just hoot method in class1 (line 61)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method not find

    Can you post the program's output that shows what you are asking about?
    Add some comments to the output showing where the output is not what you expected.

    shoot from class2 in class1 in line 82 but it find just hoot method in class1
    Please use the real names of the classes when describing the problem, not class1 and class2 so that there isn't any confusion.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method not find

    Line 81 is in the update() method. From where are you calling that method()? The update() method will only call the overridden shoot() method if the containing class of update() is extended and shoot() is overridden there.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    dominikremes is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Re: Method not find

    Yes update needs to call shootfrom TowerIce but it calls from Tower.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method not find

    Add some print statements in all the methods that are called, run the program, copy the print out and paste it here.
    Add some comments to the print out describing what you are asking about.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method not find

    Actually, it would be more helpful if you could construct and post a much simpler example, calling the overridden methods in the same fashion as you do in the actual code. Doing so would better help us help you and it might allow you to discover an error in your program.

    Regards,
    Jim
    Last edited by jim829; 02-05-2017 at 04:23 PM. Reason: grammar
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Creating a find method to find objects in an array?
    By Vetements in forum New To Java
    Replies: 3
    Last Post: 01-28-2017, 03:11 AM
  2. Cannot find symbol method
    By gusten in forum New To Java
    Replies: 12
    Last Post: 04-10-2014, 09:01 PM
  3. HELP! can't find symbol-method
    By Jack9333 in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 01:48 AM
  4. Can't find method
    By 01allenh in forum New To Java
    Replies: 1
    Last Post: 03-25-2009, 07:46 PM
  5. Can't find out how to get this method working :)
    By Shadaw in forum Java Applets
    Replies: 2
    Last Post: 12-29-2008, 05:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •