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

2012/12/12

How do I detect Objects that are NEAR another?

Entity1037 Entity1037

2012/12/12

#
I'm trying to Detect if a Homing missile is near its target, and then state that as an actor, so I can do: Actor player = getObjectsInRange(50, You.class); if (player.getX()>getX()){ xmove=-1; } ... setLocation(getX()+xmove,getY()+ymove); However when I do this code, it says it cannot recognize getObjectsInRange() or it says its an incompatable type. How Can I Accomplish My homing missle?
danpost danpost

2012/12/12

#
Your first line is trying to assign an Object object to a field that is assigned to hold an Actor object. That is, 'getObjectsInRange' returns an Object class objct. The object returned must be cast to an Actor type before being assigned to 'player'. Actor player = (Actor) getObjectsInRange(50, You.class); Next, you will probably get a NullPointerException error because 'player' is 'null'. Make sure you check for this state before trying to get 'player.getX()' or 'player.getY()'.
Entity1037 Entity1037

2012/12/12

#
It Compiled, but the terminal window is giving me this error: java.lang.ClassCastExcepion: java.util.ArrayList cannot be cast to greenfoot.Actor at SeekingLazer.act(SeekingLazer.java:20) Here's my code, and I can't find anything wrong with it: import greenfoot.*; public class SeekingLazer extends Actor { int start=0; int destroy=0; int remove=0; int blink=0; int xmove=0; int ymove=0; int playerDetected=0; public void act() { //Rotate At Start if (start==0){ start=1; setRotation((Greenfoot.getRandomNumber(45))+22); } //Seek Player and Move Accordingly Actor player2 = (Actor)getObjectsInRange(50, You.class); if (player2!=null){ playerDetected=1; if (player2.getX()>getX()){ xmove=1; } if (player2.getX()<getX()){ xmove=-1; } if (player2.getY()>getY()){ ymove=1; } if (player2.getY()<getY()){ ymove=-1; } }else{ if (playerDetected==0){ move(1); } } setLocation(getX()+xmove,getY()+ymove); //Image Change GreenfootImage frame1 = new GreenfootImage("SeekingProjectileFrame1.png"); GreenfootImage frame2 = new GreenfootImage("SeekingProjectileFrame2.png"); if (blink==0){ setImage(frame1); blink+=1; }else{ if (blink==5){ setImage(frame2); } if (blink!=10){ blink+=1; }else{ blink=0; } } //Hit Detection Actor player = getOneObjectAtOffset(0, 0, You.class); if (player != null){ destroy=1; } //Off Screen Detection if (getY() >= getWorld().getHeight()-1){ remove=1; } if (getY() <= getWorld().getHeight()-600){ remove=1; } if (getX() >= getWorld().getHeight()-1){ remove=1; } if (getX() <= getWorld().getHeight()-600){ remove=1; } //Remove Commands if (remove==1){ getWorld().removeObject(this); }else{ if (destroy==1){ getWorld().addObject(new Explosion(),player.getX(),player.getY()); getWorld().removeObject(player); getWorld().removeObject(this); } } } } Any ideas?
Entity1037 Entity1037

2012/12/12

#
And also, it only does this when the Homing Projectile is on the screen.
danpost danpost

2012/12/12

#
You can do the 'Rotate at start' part of the code in the 'SeekingLazar' constructor (create one and move the code there). There will be no need for the 'start' variable when this is done. And for the error: change Actor player2 = (Actor)getObjectsInRange(50, You.class); to Actor player2 = null; if (!getObjectsInRange(50, You.class).isEmpty()) player2 = (Actor)getObjectsInRange(50, You.class).get(0); The same needs done for 'player' later in the class code.
davmac davmac

2012/12/12

#
Your first line is trying to assign an Object object to a field that is assigned to hold an Actor object. That is, 'getObjectsInRange' returns an Object class objct.
No, it returns a List. That is the problem here. Entity1037: You need to check whether the returned list is empty, and get an actor out of it if not.
List players = getObjectsInRange(50, You.class);
if (! players.isEmpty()) {
    Actor player = (Actor) players.get(0);
    if (player.getX()>getX()) { xmove=-1; }
    // ...
}
You also need to 'import java.util.List;' at the top of your source.
danpost danpost

2012/12/12

#
@davmac, thanks for the correction.
Entity1037 Entity1037

2012/12/12

#
It works! Thank you so much! I shall post my beta version of the game I'm making this for soon.
You need to login to post a reply.