简体   繁体   English

Java-Slick2D-动态创建对象

[英]Java - Slick2D - Dynamically Creating Objects

Okay, so I understand that Images can be rendered in the render statement. 好的,所以我知道可以在render语句中渲染Images。 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? 有没有一种方法可以动态创建对象(例如Plane类),并通过名为Texture的字符串创建和渲染Image? For example, if I have a class called Bullet, how can I dynamically create the Image as I run 例如,如果我有一个名为Bullet的类,如何在运行时动态创建图像

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? 或者,对于我正在开发的游戏,其行为类似于Frogger,当我声明该类的Image并将其update语句添加到BasicGame文件中的update()循环时,如何使该类的Image称为纹理渲染? 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. 我认为,当您加载Bullet对象时,请在构造函数中加载图像并将其保存在变量中。 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. 据我所知,update方法被循环调用。 Asumming your issue is within the bullet class just do this: 假设您的问题在bullet类中,只需执行以下操作:

@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. 如果您在update方法中调用一个循环,那么它只会在游戏的任何内容可以执行,更新或应用之前循环子弹。 You want to make sure the whole game can update fairly. 您要确保整个游戏都能公平更新。

EDIT 1 编辑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; 首先,我应该向Object添加一个paint方法; make sure you add the Slick's implementation of graphics. 确保添加了Slick的图形实现。

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

Inside the render method you would call your object and paint it. 在render方法内部,您将调用对象并将其绘制。

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. 制作游戏时,Slick2D会自动调用游戏的更新方法和渲染方法。 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. 如您所见,示例代码没有循环调用render或update。 Slick2D has the loop built in. Slick2D具有内置的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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