Skip to main content

Game Development: Construction Method (Java Design Pattern)

If you want to extend a Class and do some stuff, before the super constructor gets called, you can do the following:

Create static methods, which return everything your super constructor needs.
Here is a simple example from LibGDX. I wanted to extend ScrollPane and provide some additional predefined values.

What I wanted to do is:
  • children of my ScrollableWidget should always sit in the middle
  • both scrollbars should always be active
  • children should always sit in the middle of a bigger container (overscrolling)


public class ScrollableWidget extends ScrollPane {

    public ScrollableWidget( 

        int scrollWidth, int scrollHeight, 

        int childWidth, int childHeight, Actor child, Skin skin) {

 

        super(construct(childWidth, childHeight, child), skin);
        setSize(scrollWidth, scrollHeight);
        setScrollBarPositions(true, true);
        layout();
        setScrollPercentX(.5f);
        setScrollPercentY(.5f);

    }

    private static Actor construct( 

        int width, int height, Actor child) {

 

        Group container = new Group();
        container.setSize(width,height);
        child.setPosition(width/2-child.getWidth()/2,height/2-child.getHeight()/2, Align.bottomLeft);
        container.addActor(child);

        return container;
    }
}

Comments

Popular posts from this blog

Newer Super Mario Bros. Wii unter Linux mit Patchimage

Super Mario Bros. ist ein Klassiker. Dazu muss sicher nicht viel gesagt werden. Doch auch die neuesten Ableger der Serie wie New Super Mario Bros. Wii erfreuen sich großer Beliebtheit. Eine Weile lang machte ein Spiel namens Newer Super Mario Bros. Wii großes Aufsehen. Die Forsetzung zum Original von Nintendo wurde inoffiziell vom Newer-Team in 3-jähriger Entwicklerarbeit erstellt, hat 128 neue Level und überzeugt durch die außerordentliche Qualität des Hacks. An dieser Stelle sei gesagt, dass ihr das Spiel nur legal spielen könnt, wenn ihr New Super Mario Bros. Wii selbst besitzt. Falls dies nicht der Fall ist, kauft es bitte zuerst. Illegal heruntergeladene Spiele werden nicht unterstützt und von mir ausdrücklich nicht befürwortet . (Eine Windows-Anleitung findet ihr hier: WiiDatabase .de) Also, los gehts: Dumpt euer originales New Super Mario Bros. Wii z.B. mit USB Loader GX (auf + klicken) kopiert den Ordner New Super Mario Bros. Wii [SMNP01] auf euren PC Nun ...

Ubuntu 16.04 USB-Stick - "Das Ziel ist schreibgeschützt" lösen

Es gibt Dinge, die dürfen in einem nutzerfreundlichen Betriebssystem einfach nicht passieren. Vor allem dürfen Sie aber nicht monatelang bestehen bleiben. Mit Ubuntu 16.04 kann ich Freunden und Bekannten Ubuntu einfach nicht mehr empfehlen, wenn selbst ich an einfachsten Aufgaben scheitere. Gemeint ist hier das Kopieren von Dateien auf USB-Sticks. Trotz jahrelanger Ubuntu/Linux-Erfahrung gelang es mir erst nach gründlicher Recherche das Problem zu beheben. Ein Laie hat hier keine Chance. Damit ihr nicht lange suchen müsst, hier das Problem samt Lösung: Problem Datei oder Ordner auf Fat32-USB-Stick kopieren oder anlegen schlägt fehl mit der Meldung "Das Ziel ist schreibgeschützt". Lösung das Paket fuse-posixovl installieren und Ubuntu neu starten sudo apt-get install fuse-posixovl Viel Erfolg

[Unreal Engine][C++] How to create a simple trigger actor

[Unreal Engine][C++] How to create a simple trigger actor A Simple Trigger Volume in C++ Used Unreal Engine Version: 4.22 This is the first post of a small series of Unreal Engine C++ Tutorials. Keep in mind, that Unreal’s API changes rapidly and often. I still hope, this may be of some use to others. Coming from Unity, programming in C++ for Unreal is rather painful. I hope to give you some assistance and make life a little bit easier. Whay, would you say, should we make our own trigger actor? There is ATriggerVolume , right? Yes, there is, but inheriting from it is difficult and rather undocumented. I tried and failed. Yes, we have to give up some of ATriggerVolume 's functionality, but we learn a lot and at least we know exactly what it’s doing. First, we’ll create a new C++ class, inheriting from Actor , called SimpleTriggerVolume . Let’s add a protected property to hold a reference to our trigger component: /** Shape of the trigger volume componen...