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

2018/2/3

Strange behavior - addedToWorld method

TheSlorrow44 TheSlorrow44

2018/2/3

#
I made a class named Blue1 with 3 methods : constructor , addedToWorld , and a method named set_Image . The problem was that even if I used addObject(new Blue1(some parameters),x,y), the actor was added at 0 0. I discovered that If I cut the code from addedToWorld and paste it in the constructor , the actor will appear at the correct coordinates . my code from addedToWorld() :
for(int i=1; i<images.length; i++)
        {
            images[i] = new GreenfootImage("blue"+i+".png");
        }
        setImage(images[1]);
Can someone explain this "behavior"?
danpost danpost

2018/2/3

#
Setting the image of an actor has nothing to do with the location of the actor. The only way to change an actor's loccation is (1) using the Actor class method 'move(int)'; (2) using the Actor class method 'setLocation(int, int)'; or (3) using the World class methods 'removeObject' and 'addObject' on the actor.
TheSlorrow44 TheSlorrow44

2018/2/3

#
And this is the reason why I don't understand why the actor didn't appeared at given coordinates. For example I used addObject(new Blue1(),120,0); but the actor was added at 0 0 .And , If I cut the code from addedToWorld and paste in constructor , and delete addedToWorld , the actor will appear at the correct location.
Yehuda Yehuda

2018/2/4

#
The position of the addObject call shouldn't change the way it acts unless you used variables for parameters, and it's for sure not the reason why you did or didn't understand something ("this is the reason why I don't understand").
danpost danpost

2018/2/4

#
TheSlorrow44 wrote...
And this is the reason why I don't understand why the actor didn't appeared at given coordinates. For example I used addObject(new Blue1(),120,0); but the actor was added at 0 0 .And , If I cut the code from addedToWorld and paste in constructor , and delete addedToWorld , the actor will appear at the correct location.
Please show the class where you are using the addObject call for inspection. If it extends any class other than World, show that class as well. Before that, please show the entire Blue1 class code.
You need to login to post a reply.