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

2012/1/21

Image problem in actor

sp33dy sp33dy

2012/1/21

#
Hi, Can anybody confirm whether there is a problem with Actor's and adding a new image to it programmatically? Basically, I'm creating a simple demo app, which adds an actor to the world, overriding it's image. I.E. I'm allocating a new greenfoot image within the actor and replacing the actors reference to the new image. This works well, except that when doing at the initialisation phase and actor is being added, the original addObject(image,x,y) positions x & y are ignored. They appear to be replaced by the new images width and height (halfed). I don't want this behaivour. I would like the x & y to remain where they are and show the image at that position. I think this is linked to handles which appear to be center of images. At the moment, I'm having to add an adjustment method into the class which is called after actor is added to the world (which is a little annoying and rubish in my opinion). Is there a way to override this or is this a bug?? I'd appreciate anyone who can give me an answer. Thanks and kind regards Sp33dy
davmac davmac

2012/1/21

#
The x- and y-position of an actor refer to the center of the actor. That's by design. If you want to specify the position of the top-left corner of the actor, you should add half the width to the x position, and half the height to the y position. If you want to change an Actor's image and have the top-left be in the same position, you need to calculate (or store separately) the position of the top-left before you change the image, and then re-calculate the appropriate center position after you've changed the image.
the original addObject(image,x,y) positions x & y are ignored.
They are not ignored. They control the position of the actor.
sp33dy sp33dy

2012/1/21

#
Hi davmac, I figured this might be the case; however, an exception is thrown when I try to setlocation of the actor to the new position because the actor isn't added to the world at that point. So, it just 'feels' like a hack to having to reposition it. Thanks for response, I'll try to code an elegant approach. I'll also post up the next demo soon. Regards Sp33dy P.S. From the demo, you'll be able to see exactly why it's a little frustrating.
sp33dy sp33dy

2012/1/21

#
Hi davmac, I've just discovered the overridable addedToWorld() method! That's solves my problem. Thanks and kind regards sp33dy
sp33dy sp33dy

2012/1/21

#
Just posted up my Lines demo. Will comment code and publish soon. Thanks for prompt to making me think!
You need to login to post a reply.