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

Apps add parameter (not only .width or .height)

androgaga

Newbie
Hi Guys,

I draw a space ship :
Code:
	private void spawnSpaceship() {
		
		Rectangle spaceship = new Rectangle();
		
                spaceship.x = MathUtils.random(0, screenWidth-32);
		spaceship.y = screenHeight;
		
                spaceship.width = 32;
		spaceship.height = 32;

                // spaceship.energy = 100; <= this line won't compile
		
		spaceships.add(spaceship);
    
	    lastDropTime = TimeUtils.nanoTime();

	}

As you can see, spaceship has parameters : x position, y position, width, and height.

I'm a noob and I would like to add an "energy" parameter but this line won't compile :
Code:
spaceship.energy = 100; <= ERROR

Thanks for your help !
 
spaceship is an instance of the Rectangle class... The Rectangle class isn't a standard java class, so I am going to make some assumptions. This class only has x, y, width and height as data members. You can't change this. What you CAN do is create a new class, like so:

[high]
public class Spaceship {
public Rectangle aabb;
public int energy;

public Spaceship() {
aabb = new Rectangle();
}
public Spaceship(int x, int y, int w, int h, int energy) {
aabb = new Rectangle();
aabb.x = x;
aabb.y = y;
aabb.width = width;
aabb.height = height;
this.energy = energy;
}
}
[/high]

Then, you can modify your code as follows:

[high]
private void spawnSpaceship() {

int x = MathUtils.random(0, screenWidth-32);
int y = screenHeight;

int width = 32;
int height = 32;

int energy = 100;

Spaceship spaceship = new Spaceship(x, y, width, height, energy);

spaceships.add(spaceship);

lastDropTime = TimeUtils.nanoTime();

}
[/high]

Note also that your spaceships list will now need to be a list of spaceships (List<Spaceship>) instead of a list of Rectangles.
 
Hi jonbonazza thanks !

The new class is ok ( notice "Spaceship" has been renamed to "Enemy") :
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;
    }
}

but the main file reports errors here " LINE 13 : enemies can't be resolved" :
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);   //    <--- line won't compile ( "enemies can't be resolved" )

        lastDropTime = TimeUtils.nanoTime();

    }

and when modifying my array of Rectangles by a List :
Code:
List<Enemy> enemies;
"The Type list is not generic it can not be parameterized with arguments <Enemy>"
 
yes it was the case, so i added your class and removed this one :
Code:
import com.badlogic.gdx.scenes.scene2d.ui.List;
and now it compiles ! a BIG thanks, I would never had found it alone !
 
Back
Top Bottom