开发者

Java - Statements / Conversions | Pulpcore

[please note this may require AS3 + Java knowledge]

Background Information:

I'm trying to build a game using Java + Pulpcore, but I'm fairly new to the scene. The game I'm building could possibly be more performance intensive than I thought, and I know Java would solve my problems with this, but there are a couple questions I have dealing with strict-types, etc.

Here is my code in AS3:

Main.as3

import org.tbmb.champions.Container;
import org.tbmb.zombies.ZContainer;

public class Main extends MovieClip {

    // ******* temporary properties ******* /
    private var blunder:Container = null;
    // *******                      ******* /

    public function Main() {

        init(); // initialize objects

    }

    public function init():void {

        blunder = new Container(Blunder as Class);

    } // end of class

}

Container.as3

 package org.tbmb.champions {

     import flash.display.MovieClip;

     public class Container extends MovieClip {

          public function Container(champ:*) {

          } // end of constructor

     } // end of class

 } // end of package

Blunder.as3

package org.tbmb.champions.blunder {

    import flash.display.MovieClip;

    public class Blunder extends MovieClip {

        public function Blunder() {

        } // end of constructor

    } // end of class

} // end of constructor

1.) How would I rewrite in Java?

blunder = new Container(Blunder as Class);

2.) How would I be able to accept any Classes in Java for the above line within my Container class?

public function Container(champ:*) {

I need to do this because I'm sending different champion classes (depending on what the user picks) to a containing class that will hold all their other classes (health, etc). I need my Container class to accept any Class rather than just one; what type would I use?

Here is what I have in Java so far:

ZomboPulp.java (Main Class)

import pulpcore.scene.Scene2D;

import org.tnpfk.champions.Container;
import org.tnpfk.champions.blunder.Blunder;

import pulpcore.sprite.FilledSprite;
im开发者_JAVA百科port pulpcore.image.Colors;

public class ZomboPulp extends Scene2D {

    FilledSprite background = new FilledSprite(Colors.WHITE);

    Container container = null; // Container that contain's blunder, 
    // and all his objects (health, mana, etc)

    public void load() {

        this.initScreen(); // initialize main screen.
        this.initObjects(); // initialize our objects.

    } // end of load();

    public void initScreen() {

        add(background);

    } // end of initScreen();

    public void initObjects() {

        container = new Container(Blunder as Class); // ERROR HERE

    } // end of initObjects();

}

Container.java

package org.tnpfk.champions;

public class Container {

    public Container(Object champ) {

    } // end of constructor

} // end of class

Sorry for the lengthy post, and thanks for any help. By the way, I did check StackOverflow; and Google, but I was unable to find anything about this.

Thanks, jvmpulp


Alrighty! I have no experience with PulpCore, but I do know both AS3 and Java, so I think I can answer your question. First off, I guess I don't 100% understand what you need to do with the champ object in the Container class, and I really don't understand why you were doing Blunder as Class instead of just passing an instance of Blunder. Either way, here's what I'd recommend with what you have as of now:

public void initObjects() {

    container = new Container(Blunder.class);

}

As you can see, you can get a Class instance just by getting the class property of any class. Now, you have the right idea with using Object as the type for the Container constructor for any type. However, using Object is bad practice (use method overloading/more specific types instead), and it's not even required here. Getting the class property will always be of type Class, even though they represent different classes. So, rewrite the constructor as this:

public Container(Class champ) {

}

Then, do whatever you need to do with the class.

Now, that's basically a direct port, but it seems some of the things you're doing are bad practice. The whole system of passing a Class object seems irrelevant and unnecessary; why not just pass an instance of the object? For example, like so:

public class Container {

    protected Champion champ;

    public Container(Champion champ) {

        this.champ = champ;

    }

}

Now, make Champion an abstract class that contains the common methods for all the champions:

public abstract class Champion {

    protected Something something;

    abstract Something getSomething();

}

(Obviously, the variable/method shown here are just examples.) Then, have your individual Champion classes subclass Champion:

public class Blunder extends Champion {

    public Something getSomething() {
        return this.something;
    }

}

Etc. Then, finally, do this:

public void initObjects() {

    container = new Container(new Blunder());

}

Obviously, this is a basic example, and you don't have to take my advice. But it would probably be easier to do than the system you already had in AS3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜