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

2012/5/27

Locations of Actors

wahaj wahaj

2012/5/27

#
I have two problems.
int x;
int y;
getX() = x;
getY() = y;
when I try to compile this code I get an unexpected type error. second, how do I get the location of one actor in the code of another actor. so say I have two actors A and B. how would I get the location of actor B in the code of actor A and store it in a local variable.
danpost danpost

2012/5/27

#
First off, you cannot set a value to a method (so, lines 3 and 4 do not make sense to the compiler). If you are trying to save the current location of the object, lines 3 and 4 should be:
x = getX();
y = getY();
Second: to save the location of actor B in local variables from the actor A class, the code is very similar, except you need a reference to actor B (so somewhere you need to get the reference, or create a reference to B). Is actor B the only instance of its type? and if not, is actor A the only other instance of its type? Do you have a reference to actor B in the world class?
wahaj wahaj

2012/5/27

#
so normal math rules dont apply in programming? like it doesn't matter which expression is on which side of the equal sign. as for the reference I guess I can use Actor B = Greenfoot.getOneintersectingObject(B.class) but I dont know how to use this reference to get the coordinates of B. also this is for the greeps competition. I have an idea but I need the coordinates of the tomatoes (B) to implement it. but I cant figure out how to do that. technically I can only get the coordinates of the tomatoes when a greep is intersecting the tomato pile.
danpost danpost

2012/5/27

#
In progamming: expressions are as such: = ; The expression on the right is evaluated and the result is stored in the field (variable or reference) on the left. You could read the above as "Set variableOrReference to expression". For your location of actor B
Actor b = (Actor) getOneIntersectingObject(B.class);
int bX;
int bY;
bX = b.getX();
bY = b.getY();
wahaj wahaj

2012/5/27

#
ah I didnt know you could use dot notation like this. thanks for the help. new problem. how to make greenfoot round a number? Edit: never mind i figured it out. i dont see any PM function here. im having some problem with my code and i dont want to post it here. so how do i show me code to someone?
danpost danpost

2012/5/27

#
Just as a sidenote, you are always using dot notation. The thing is, most of the time it is omitted (using the default). When you say 'getX()', what you are actually saying is 'this.getX()' ('this' being an Actor sub-class instance); or 'getBackground()' is truly 'this.getBackground()' ('this' being the World sub-class instance). What is a PM function? You need to find a way to share your code privately (e-mail attachment would be one of the safer routes).
davmac davmac

2012/5/27

#
Just wanted to add:
so normal math rules dont apply in programming? like it doesn't matter which expression is on which side of the equal sign.
That's right, programming doesn't equal maths :) In maths, '=' means 'is equal to'. In Java, it means 'assignment', that is 'what's on the left side takes the value of the right side, from now on.'
wahaj wahaj

2012/5/27

#
PM stand for private message. thanks danpost and davmac for clearing your respective things up. in the greeps competition rules it says we can only move one step at a time. so can I use the setLocation method like this? setLocation( getX() + 1, getY() + 1);
danpost danpost

2012/5/27

#
Rule 7: No teleporting. Methods from Actor that cheat normal movement (such as setLocation) may not be used. Using setLocation is a violation of Rule 7 (even if you are only moving by ones). You are restricted to only using the 'move()' statement; and then, only once per act cycle.
wahaj wahaj

2012/5/27

#
ah ok. thanks for all the help. i have my greeps doing just what i wanted. now all thats left is to make the going around water part a bit more effective
wahaj wahaj

2012/5/27

#
ok, I think I've got everything done now to the best of my ability. I want someone to test it before I submit it. whose willing to lend me some of their time? I'll need your email
danpost danpost

2012/5/27

#
If your average 3-level score is above 80, you have done very well (mine averages between 70 and 80 -- something I did on my own; not in a competition).
wahaj wahaj

2012/5/27

#
hmm. the highest i got was 63. I think I know why its not higher. In my scenario, I can have 3 or 4 and even more greeps at a single tomato pile but they dont pick up a tomato. I dont know why? my greeps move up to the center of the pile and keep moving in a circle around it. some of them still end up picking tomatoes but when they get back to the pile not all of them pick another one again
You need to login to post a reply.