Why i can't do operations for arrays to arrays of custom classes

shorter, there is the code we need

class creature {
 float xp,yp;
 PImage ifa;
}

class animal extends creature {
 animal(float sxp,float syp,PImage sifa) {
   xp = sxp;
   yp = syp;
   ifa = sifa;
 }
 
 void NP(float nxp,float nyp) {
   xp = nxp;
   yp = nyp;
 }
 
 void nims(PImage nifa) {
   ifa = nifa;
 }
 
}

suppose we need a append an array of animals

animal[] a = new animal[0];
a = append(a, new animal(0,0, somePImage))

but, we have an error

Type mismatch: cannot convert from Object to somefile.animal[]

how to fix it?
p.s its processing 4.3 java
p.p.s don’t ask why I need this im creating games on processing

2 Likes

cause is not mine (its noting there in comment)

Hello,

instead of

better

a = (animal[]) append(a, new animal(0,0, somePImage))

see: append() / Reference / Processing.org

When using an array of objects, the data returned from the function [append] must be cast to the object array’s data type.
For example: SomeClass[] items = (SomeClass[]) append(originalArray, element).

or in your case: items = (SomeClass[]) append(originalArray, element)

  • with (SomeClass[]) being the crucial part that tells (casts) the Object to be of kind animal[]

Remark

Instead of just “a” it might be wise to name it listOfAnimals or arrAnimals or so.

Chrisir

2 Likes

it works, thanx Chrisir :grin:

1 Like