Re: Generalising Code....

jon klein <[email protected]> Mon, 23 May 2005 10:34:01 -0400
Newsgroups gmane.comp.breve
Message-ID <[email protected]>
On May 20, 2005, at 12:30 PM, Cavan Fyans wrote:

> Hi,
>
> In the code listed below if you look as the methods Shape1, Shape2,
> Shape3 etc they are all pretty much the same appart from the .png file
> that is loaded onto each shape...in 'Shape1' I have the menu items for
> changing the colour of it....i want this feature for every one of the
> different shapes, but obviously copying the same code into each one is
> alot of code there...is there a way i can refine this so i only have
> to list the menus once yet they still apply to each object
> individually?

Yes -- right now you're creating a bunch of subclasses of Mobile, and
adding menu items for each one.  What you'll want to do is create a
single subclass of Mobile (called, say, "MyMobile") and put the menu
code in there.  Then, make all of your other classes subclasses of
MyMobile (instead of Mobile).

So the class structure will look a bit like this:

Mobile : MyMobile {
     # menu code in here
}

MyMobile : Shape1 { ... }

MyMobile : Shape2 { ... }

etc.

> Also i would like to be able to change the colours of the objects
> independant of each other but through key presses rather then the
> cumbersome right click...the way i thought may work was knowing which
> object is selected and then only altering the colour of that selected
> object...but i could not manage to call a colour change through this
> system...

The Control class method "get-selection" will give you the selected
object, so you can use that along with your keyboard callbacks to
change the color of the selected objects.  For example:

+ to catch-key-r-down:
     if (self get-selection): {
         (self get-selection) set-color to (1, 0, 0).
     }

- jon klein
_______________________________________________
breve mailing list
[email protected]
http://lists.spiderland.org/mailman/listinfo/breve