简体   繁体   中英

Java - Slick2D - Dynamically Creating Objects

Okay, so I understand that Images can be rendered in the render statement. I do have one question, though. Is there a way I can create an object dynamically (eg Plane class) and have an Image be created and rendered via a String called texture? For example, if I have a class called Bullet, how can I dynamically create the Image as I run

Bullet myBullet = new Bullet();

? I would really appreciate some help.

Example:

class Bullet
{
    public float x, y = 0;
    public float rotation = 0;
    public void bullet(posX, posY)
    {
         x = posX;
         y = posY;
    }

Also, how can I make it loop a method automatically (I already have a loop running in main class, but how do I append this to the block?)?

public void update() {
    x += 2 * Math.cos((Math.PI / 180) * rotation);
    y += 2 * Math.sin((Math.PI / 180) * rotation);
}
}

Thanks,

Joe

EDIT: By create Image, I mean to also render it.

Or, for a game I am working on that behaves somewhat like Frogger, how do I make this class's Image called texture render when I declare it and add it's update statement to the update() loop in the BasicGame file? package misc;

import mobile.MobileOctopus;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Current {
    public Image texture;
    public float x, y = 0;
    MobileOctopus player;
    public Current(int posY, MobileOctopus character) throws SlickException
    {
        texture = new Image("res/current.png");
        x = 0;
        y = posY;
        player = character;
    }
public void update()
{
    x -= 3;
    if(x < -380)
    {
        x = 0;
    }
    if(player.y + 32 > y && player.y + 32 < y + 32)
    {
        player.x -= 3;
    }
}
}

The Current class moves the player left when he is inside it. But, how can I do said above by calling

Current myCurrent = new Current(100, player);

In my opinion, when you load the Bullet object, load the image inside the constructor and save it in a variable. That is if it isn't too big of a file to keep in memory.

public Bullet(){
    //Load image here
}

From my knowledge, the update method is called in a loop. Asumming your issue is within the bullet class just do this:

@Override
public void update(GameContainer gc, int delta) throws SlickException {
    bullet.update()
}

If you call a loop within your update method then it will only loop the bullet before anything of the game can execute, update, or apply. You want to make sure the whole game can update fairly.

EDIT 1

I understand what you mean now. In order to render the object's image, this is what I have done. I don't know if this is the best way, but this is how I've done it.

First I you should add a paint method to the Object; make sure you add the Slick's implementation of graphics.

public void paint(Graphics g){
    g.drawImage(image, X, Y);
}

Inside the render method you would call your object and paint it.

public void render(GameContainer gc, Graphics g){
    bullet.paint(g);
}

This will allow you to render the object onto the screen. Now keep in mind, when you are needing to draw multiple things to the screen, you need to put them in order, otherwise they will overlap.

When you make a game, Slick2D will AUTOMATICALLY call your game's update methods and render methods. All you have to do is make your game's update/render call the object's update/render.

Here is an example:

import org.newdawn.slick.*;

public class ExampleGame extends BasicGame {
    public static void main(String[] args) throws SlickException {
        //creates and starts the game
        AppGameContainer g = new AppGameContainer(new ExampleGame("Title"));
        g.start();
    }
    public ExampleGame(String title) {
        super(title);
    }

    public void render(GameContainer container, Graphics g)
            throws SlickException {
        // This code automatically runs on a loop
        System.out.println("render");
    }

    public void init(GameContainer container) throws SlickException {
        // This code runs once
        System.out.println("init");
    }

    public void update(GameContainer container, int delta)
            throws SlickException {
        // This code automatically runs on a loop:
        System.out.println("update");

    }

}*

This is what outputs to the log:

init
render
update
render
update
render
render
render
update
render
...

As you can see, the example code has no loop calling render or update. Slick2D has the loop built in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM