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

2013/1/3

Throwing a weapon at something else

Minion1 Minion1

2013/1/3

#
So I'm about three steps away from finishing my first sample game. But I need to figure out one last thing, and perhaps people here can help with it. I need my boss monster to throw his weapon at the hero. This involves two possible solutions: 1. Find the necessary rotation of the weapon in relation the the starting point and the hero's X, Y, which seems very difficult. or 2. Set the hero's current X, Y (at the time of the creation of the enemy's weapon) as the direction to move. Honestly, I remember trying to do this sort of thing when I first started out and it was a bust. I have no idea how to go about this. Anyone done anything like this?
danpost danpost

2013/1/3

#
Of use might be the Actor method 'turnTowards(int, int)' which sets the rotation of an object to point toward the world coordinates (x, y).
Minion1 Minion1

2013/1/3

#
OH! I'd forgotten about that. Yes, that might be exactly what I need. Let me check.
Minion1 Minion1

2013/1/3

#
Hm.. it's saying that the Hero's X and Y coordinates are non static and can't be referenced from a static context. I'm a real noob, so I'm going to try to translate that.. What it's saying is that the X and Y coordinates of the hero can't be applied to anything outside of the hero. Right? Probably because they change so often..
danpost danpost

2013/1/3

#
No. That is not what it is saying. What it is saying is that 'Hero' is a class name and the class does not have an X and Y coordinate (an actor created from the class and added to the world will, however). You probably are trying to use 'Hero.getX()' and 'Hero.getY()', when what you need is a reference to the Hero object that is in the world:
Hero hero = (Hero) getWorld().getObjects(Hero.class).get(0);
// then
int heroX = hero.getX();
int heroY = hero.getY();
Minion1 Minion1

2013/1/3

#
Ah. Thank you , trying now.. Will this stuff ever sink in? I know I've come a long way and all.. but sometimes it doesn't feel like it.
Minion1 Minion1

2013/1/3

#
Working like a charm now. Thanks danpost.
Minion1 Minion1

2013/1/3

#
I will be sure to post it soon, it's going to be small, but hopefully people will enjoy it.
You need to login to post a reply.