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

2015/12/12

How to get one object to follow another?

Jnrichards Jnrichards

2015/12/12

#
The actor the user is in control of is called "PlatformJumper" and i want the "enemy" actor to follow the PlatformJumper if its in range. Turning would be helpful.
danpost danpost

2015/12/12

#
For an instantaneous turn toward the PlatformJumper object:
1
2
3
4
5
6
int range = 200; // this line added for set-up
if ( ! getObjectsInRange( range, Enemy.class ).isEmpty() )
{
    Actor pj = getObjectsInRange( range, Enemy.class ).get( 0 );
    turnTowards( pj.getX(), pj.getY() );
}
If you want the enemies to gradually turn toward the platform jumper when in range, you can look at the code of my Zombie Shooter Demo scenario.
Jnrichards Jnrichards

2015/12/12

#
will this enable the enemy to move towards the platform jumper? and on line 4 ".get( 0 );" comes up with an error: Incompatible types java.lang.object cannot be converted to greenfoot.actor
Super_Hippo Super_Hippo

2015/12/12

#
The code lets the actor turn to the other actor. To move to it, just add 'move(speed);' after this (speed should be an 'int' representing the speed of the actor). You need to cast it to an Actor object:
1
Actor pj = (Actor) getObjectsInRange( range, Enemy.class ).get( 0 );
Jnrichards Jnrichards

2015/12/12

#
Enemy isn't turning/moving towards my actor heres the full code you've helped me put in if this helps import greenfoot.*; /** * Write a description of class Enemy here. * * @author (your name) * @version (a version number or a date) */ public class Enemy extends Actor { /** * 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 followHuman() { int range = 200; int speed = 5; if ( ! getObjectsInRange( range, Enemy.class ).isEmpty() ) { Actor pj = (Actor) getObjectsInRange( range, Enemy.class ).get( 0 ); turnTowards( pj.getX(), pj.getY() ); } } }
Super_Hippo Super_Hippo

2015/12/12

#
Read the comment. The 'act' method is called every act-cycle. Not another method like 'followHuman'. So you have to call this method from the 'act' method:
1
2
3
4
public void act()
{
    followHuman();
}
You still don't have the 'move' call there. You can place 'int range=200;' outside the method. (Same with speed.)
danpost danpost

2015/12/12

#
Super_Hippo wrote...
The 'act' method is called every act-cycle. Not another method like 'followHuman'.
To clarify, methods you put into your classes that are not declared in your super classes, World or Actor classes, are not executed automatically. And most of them in those supercalssess are not executed automatically. The act method of the currently active World object and those of all Actor objects in that world are all executed automatically by greenfoot once every act cycle (or frame) while the project is in a running state. The World class methods 'started' and 'stopped' are called on the currently active World object automatically when the project changes its running state. The 'addedToWorld' method of the Actor class is called automatically anytime an Actor object is added into a World object.
Jnrichards Jnrichards

2015/12/12

#
Sorry this is taking so long but im really new to this. Got it moving but as for turning towards my actor "platform jumper still nothing. public void act() { followHuman(); } public void followHuman() { int range = 200; int speed = 5; move(speed); if ( ! getObjectsInRange( range, Enemy.class ).isEmpty() ) { Actor pj = (Actor) getObjectsInRange( range, Enemy.class ).get( 0 ); turnTowards( pj.getX(), pj.getY() ); } } If the code for the actor "PlatformJumper" is also important just ask.
danpost danpost

2015/12/12

#
The code should have the name of the platform jumper class in it -- not 'Enemy.class' (both cases).
fejfo fejfo

2015/12/13

#
danpost wrote...
For an instantaneous turn toward the PlatformJumper object:
1
2
3
4
5
6
int range = 200; // this line added for set-up
if ( ! getObjectsInRange( range, Enemy.class ).isEmpty() )
{
    Actor pj = getObjectsInRange( range, Enemy.class ).get( 0 );
    turnTowards( pj.getX(), pj.getY() );
}
If you want the enemies to gradually turn toward the platform jumper when in range, you can look at the code of my Zombie Shooter Demo scenario.
I aways use :
1
listName.size() > 0
instead of
1
listName.isEmpty()
is one better or worse ?
danpost danpost

2015/12/13

#
fejfo wrote...
is one better or worse ?
Economically speaking (as far as taxing the CPU), it is better to use 'isEmpty'. When using it, as soon as one object is found it returns a 'false' value; when using 'size', all objects must be listed before the size can be determined. At least, I thought I read that about this somewhere; but, after some thought about it, somehow it does not make sense. First, I would think the list would have to be completed before the method could be called on it; and (2) the list holds an int value indicating how many elements it contains and 'isEmpty' would check for a zero value while 'size' would just return the value. The only difference would seem to be where the comparison was made (in your code or within the method called).
fejfo fejfo

2015/12/13

#
so it doesn't matter a lot ? but .isEmpty() is a little bit better
davmac davmac

2015/12/13

#
(2) the list holds an int value indicating how many elements it contains and 'isEmpty' would check for a zero value while 'size' would just return the value. The only difference would seem to be where the comparison was made (in your code or within the method called).
In practice, this is usually true. In theory, a List implementation using a linked structure doesn't actually need to keep the size in a variable - so the size() method might actually have to count the nodes, whereas isEmpty() can just check whether there is any node. (Not storing the list size can make certain other operations more efficient - such as splicing a range of nodes between one list and another - although these operations aren't supported by Java's standard collection classes). I use "isEmpty()" in preference to "size() > 0" because I think it reads a bit better, but that's mostly a personal preference.
You need to login to post a reply.