0

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 (e.g. 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);
joeelectricity
  • 239
  • 1
  • 2
  • 13

2 Answers2

0

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.

Twister1002
  • 559
  • 1
  • 9
  • 26
  • Yes, but, how do I make it render automatically? If it is in the update statement, I need to add bullet.update(); while I program it. Also, you can only render in the render() method. How can I make it so just declaring this will render it and add the bullet.update() to the update statement? Maybe an array? – joeelectricity Jun 02 '13 at 15:53
  • Okay, but how do I make it so I don't have to add bullet.paint() to the render statement? Should I use an array of images? – joeelectricity Jun 09 '13 at 16:13
  • @user2441712 You could set images you would like to render into an array and just loop through them. However, you would have to get the X and Y position of where the object is located. That is why I suggested to put a paint method into the class. You only need to call the object and let that object do the magic. – Twister1002 Jun 10 '13 at 04:56
  • Okay, but can't you only render in the render statement? **EDIT:** Okay, so after testing, this works. But, what if I want to render bullets and arrows (for example) in the same array? An Object array may help if I cast it. – joeelectricity Jun 11 '13 at 02:13
  • @user2441712 An Object array would hold the Objects, but you have to find out what instance the objects are then cast them and render them. In my opinion, its more of a task. Now if there is a common class they all share, you could cast it to the super class, and render it that way. – Twister1002 Jun 14 '13 at 06:31
0

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.