• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps [OOP Newbie] How to create a class to spawn enemies ?

androgaga

Newbie
Hi Guys,

I'm trying to program a clone of this game :
Battle Squadron AMIGA - First levels - YouTube

I'm a newbie with Java, LibGdx and Object Oriented Programming however at this point I managed to implement several features successfully : keyboard controls, tiled background, scrolling, sprite animations and musics are ok.

At this point I spawn the enemies directly in the main file :
Code:
[...]

public class Drop<type> implements ApplicationListener {

    [...]

    public void create() {

        [...]

        if(TimeUtils.nanoTime() - lastDropTime > delayDrop) spawnEnemy();

        [...]

    }

    private void spawnEnemy() {
		
	Rectangle raindrop = new Rectangle();
	raindrop.x = MathUtils.random(0, screenWidth-tileW);
	raindrop.y = screenHeight;
	raindrop.width = 32;
	raindrop.height = 32;
	raindrops.add(raindrop);
        lastDropTime = TimeUtils.nanoTime();

    }

    [...]

}

The problem is the Rectangle class isn't a standard java class, so this class only has x, y, width and height as data members. I would like that my raindrops have other properties, like "raindrop.energy = 100;" or "raindrop.strength = 20;" but this is not possible with Rectangle.

So I have to create a class to spawn the enemies.

In Eclipse, at the same level in the tree that the main java file "Drop.java" I create a new class in a new file named "Enemy.java" (stop me if I do something wrong) :
Code:
package com.badlogic.drop;

import com.badlogic.gdx.math.Rectangle;

public class Enemy {
    public Rectangle aabb;
    public int energy;
    
    public Enemy() {
        aabb = new Rectangle();
    }
    public Enemy(int x, int y, int w, int h, int energy) {
        aabb = new Rectangle();
        aabb.x = x;
        aabb.y = y;
        aabb.width = 32;
        aabb.height = 32;
        this.energy = energy;
    }
}

And now I have to modify "spawnEnemy()" in the main java file :
Code:
    private void spawnEnemy() {
		
        int x = MathUtils.random(0, screenWidth-32);
        int y = screenHeight;
        int width = 32;
        int height = 32;
        int energy = 100;
        Enemy enemy = new Enemy(x, y, width, height, energy);
        enemies.add(enemy);
        lastDropTime = TimeUtils.nanoTime();

    }

Then I make sure I am importing "import java.util.List;" and I launch the desktop program and I get this output :
Code:
Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/jnigen/NativeCodeGenerator
	at com.badlogic.gdx.utils.GdxBuild.main(GdxBuild.java:55)
Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.jnigen.NativeCodeGenerator
	at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
	... 1 more

Could someone please help ? :)

PS: of course I've tried the following solutions proposed on the following link : 3 ways to resolve NoClassDefFoundError in Java As I'm new with eclipse it's very obscure
 
Back
Top Bottom