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

2017/10/9

I am having some trouble with tracking objects in the world

B_rob1 B_rob1

2017/10/9

#
I am trying to code object1 and object2 to where object1 moves in the world and object2 points to the object1 using object1's x & y coordinates My idea was making the object1 have public int 1x = getX(); and call that in object2 which is a subclass of object1
danpost danpost

2017/10/9

#
The first issue is that you have Object2 as a subclass of Object1. As such, any Object2 object IS also of type Object1, which I doubt you intended. The next is 'public int 1x = getX();'. This has a couple of problems: (a) the given name of the field begins with a numeric character, which is illegal in java; and (b) you cannot use 'getX()' while the actor is not in a world, which would be the case when the field is declared and the value is attempted to be assigned to it. It should be quite obvious how to fix the first issue. To fix the other one, you will need a different idea. However, how to depends largely on the use of Object1 type objects -- (a) is there only one object of its type ever created (for one running of the scenario); (b) is there always one Object1 type object in the world (the world is never void of one); and (c) is there any other behavior of the world or of Object1 type objects that might interfere with the Object2 type object pointing toward any Object1 type object. Knowing these would greatly influence what code you might use for this.
You need to login to post a reply.