Why can't I use another object's method by using dot notation without declaring it with another name

9 views
Skip to first unread message

spar...@gmail.com

unread,
Apr 21, 2015, 3:08:44 PM4/21/15
to greenfoot-dis...@googlegroups.com
Pretty much summerized by the title. Why do I have to declaring the object as something else before using its method? For example:

I have to use:

PeluruUser peluru = new PeluruUser();

               getWorld().addObject(peluru, getX(), getY());

               peluru.setRotation(getRotation());


But I can't use:

getWorld().addObject(peluru, getX(), getY());

               peluru.setRotation(getRotation());



Am I missing something here? This seems stupid to me.

Wen Bei

unread,
Apr 21, 2015, 3:29:43 PM4/21/15
to greenfoot-dis...@googlegroups.com
Well, if you haven't created the object via new, how is it going to find the object?

Wen Bei

unread,
Apr 21, 2015, 3:32:04 PM4/21/15
to greenfoot-dis...@googlegroups.com
Also, in addObject the first parameter should be the class, so you have to declare what peluru is before you use it. Try using PeluruUser.class instead

spar...@gmail.com

unread,
Apr 21, 2015, 3:45:43 PM4/21/15
to greenfoot-dis...@googlegroups.com
I think logically if I declare that the mothod exists in the world class, it should work.
Reply all
Reply to author
Forward
0 new messages