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

2012/1/8

Moving an object to another object

1
2
Oneshot Oneshot

2012/1/8

#
//
Oneshot Oneshot

2012/1/8

#
PS: My hole source code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class Zombie here. * * @author (your name) * @version (a version number or a date) */ public class Zombie extends Actor { /** * Act - do whatever the Zombie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act(){ Actor coll = getOneIntersectingObject(Player.class); if(coll != null){ pointAtObject(coll); } } }
kiarocks kiarocks

2012/1/8

#
you need to make your
Actor coll = getOneIntersectingObject(Player.class);      
        if(coll != null){  
            pointAtObject(coll);  
        }  
line like this
Player coll = (player)getOneIntersectingObject(Player.class);      
        if(coll != null){  
            pointAtObject(coll);  
        }  
This casts the Actor so it returns a Player
Oneshot Oneshot

2012/1/8

#
//
davmac davmac

2012/1/8

#
Well, you're calling a method that doesn't exist. There's no "pointAtObject" method in Actor, and you haven't defined one in Zombie; you'd need to write your own (or copy/paste it from another scenario).
Oneshot Oneshot

2012/1/8

#
//
davmac davmac

2012/1/8

#
Where did you get the idea that such a method existed? I figured you were looking at another scenario, in which case, you should look at the same scenario for a definition of the method.
Oneshot Oneshot

2012/1/9

#
//
Builderboy2005 Builderboy2005

2012/1/9

#
From the looks of that code, there should be no range limit. The reason you are experiencing the 'range limit' is because you are using getOneIntersectingObject() in your code, which only returns an object that is intersecting you. If you want the player to be able to point at objects that are not touching him, you will need a different way to detect the objects.
Oneshot Oneshot

2012/1/9

#
Ahh, okay i understand. But im a newbie and i just learn the basics for a school project. Maybe you a function i can use for my problem? thanks and best regards from Germany
Builderboy2005 Builderboy2005

2012/1/9

#
It all depends on where you want your player to point to? How many players are going to be on the field? Do you want the Player to point towards the closest player or a specific player?
Oneshot Oneshot

2012/1/9

#
//
Duta Duta

2012/1/9

#
public void pointAtObject(Actor o) {
        setRotation((int)(180*Math.atan2(o.getY()-getY(),o.getX()-getX())/Math.PI));
}
I take no credit for the code (although I'd worked a similar algorithm myself, which I use instead of this). I just found the code in an old topic kicking around the discussion boards: http://www.greenfoot.org/topics/101
Oneshot Oneshot

2012/1/9

#
//
Builderboy2005 Builderboy2005

2012/1/9

#
Ah I see, I misread your original source as being for the player! Silly me, in this case the answer should be easy.
Player p = (Player) getWorld().getObjects(Player.class).get(0);
pointTo(p);
This will first call getObjects(Player.class) to generate a list of all players in the world, and then use get(0) to select the first (and only). We can then use the pointTo() method to successfully point to the player. Note that this will give an error if there is no player in the world, so you should introduce a check for that if that ends up posing an issue.
There are more replies on the next page.
1
2