This site requires JavaScript, please enable it in your browser!
Greenfoot back
timothytitus
timothytitus wrote ...

2017/3/19

java.lang.Class<?> cls arguments

timothytitus timothytitus

2017/3/19

#
I want, for these arguments, to be able to put multiple classes in. For example, maybe I want my player to detect collision with a couple specific classes but those classes aren't subclasses of another class. Therefore, I can't just put one class for the argument, I can't put "null" either coz there's some classes I don't want to count in the collision.
Nosson1459 Nosson1459

2017/3/19

#
Either you could make them all be a subclass of one class or you'll have to put this line of code multiple times once for each class. I don't think there is a way of writing multiple classes as one.
timothytitus timothytitus

2017/3/19

#
alright thanks!
MrBradley MrBradley

2017/3/24

#
A common way to do this is to tag those classes with an interface then check against that interface "class" in your method signature.
danpost danpost

2017/3/24

#
There are methods in my XWorld XActor Classes scenario in the XActor class that allow multiple classes for collision checking.
MrBradley MrBradley

2017/3/25

#
Using Dan's classes is good, but you should look at how it is done. The interface tagging post above is a common practice and worth knowing even if you chose not to use it. Its a common design pattern called Marker/Tagging Interface Pattern. ( see also: https://en.wikipedia.org/wiki/Marker_interface_pattern )
Nosson1459 Nosson1459

2017/3/26

#
That's the same as setActOrder(java.lang.Class... classes) and setPaintOrder(java.lang.Class... classes), but this won't help in this discussion since this is for creating a method, what timothytitus want(s)/(ed) to do (is)/(was) have an isTouching method with multiple classes. But I guess you technically can do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * Checks whether this actor is touching any other objects
 * of the given classes.
 *
 * @param classes Classes of objects to look for.
 * @return True if there is an object of the given class types that intersects with the
 *            current object, false otherwise.
 */
public boolean isTouching(Class... classes) {
    for (Class classInstance : classes) if (isTouching(classInstance)) return true;
    return false;
}
danpost danpost

2017/3/27

#
Nosson1459 wrote...
That's the same as setActOrder(java.lang.Class... classes) and setPaintOrder(java.lang.Class... classes), but this won't help in this discussion since this is for creating a method, what timothytitus want(s)/(ed) to do (is)/(was) have an isTouching method with multiple classes. But I guess you technically can do it like this: < Code Omitted >
For 'isTouching', you could easily enough just do this:
1
if (isTouching(Obj1.class) || isTouching(Obj2.class) || isTouching(Obj3.class))
The difference is the 'isTouching' method returns a boolean value. The purpose of using multiple class arguments in those methods in my XActor and XWorld classes is to compile a single complete List object of all the various objects returned by the various classes.
Nosson1459 Nosson1459

2017/3/27

#
danpost wrote...
For 'isTouching', you could easily enough just do this:
1
if (isTouching(Obj1.class) || isTouching(Obj2.class) || isTouching(Obj3.class))
The difference is the 'isTouching' method returns a boolean value. The purpose of using multiple class arguments in those methods in my XActor and XWorld classes is to compile a single complete List object of all the various objects returned by the various classes.
We are having this discussion in a discussion of a specific topic. Read the first/main/original post/question.
timothytitus wrote...
I want, for these arguments, to be able to put multiple classes in. For example, maybe I want my player to detect collision with a couple specific classes but those classes aren't subclasses of another class. Therefore, I can't just put one class for the argument, I can't put "null" either coz there's some classes I don't want to count in the collision.
So my method let's him be able to put in multiple classes for the parameter (so to have the whole method is more code, but calling it like:
1
if (isTouching(Obj1.class, Obj2.class, Obj3.class))
is better than all those '|| isTouching(').
danpost danpost

2017/3/27

#
You are assuming that the number of intersectors and their types are not important -- that as long as one is touching, then the check is satisfied. That, however, was not specified and may not be the case. In fact, I doubt this thread would have even been started if it were the case, as easy as it is to write it "long-hand". True, it may have; but, then again, was not specified. I will no more discuss this unless further input from timothytitus is given, if it is given.
Nosson1459 Nosson1459

2017/3/27

#
danpost wrote...
You are assuming that the number of intersectors and their types are not important -- that as long as one is touching, then the check is satisfied. That, however, was not specified and may not be the case.
All of it is right there, and shows that is the case.
timothytitus wrote...
...for these arguments, to be able to put multiple classes in. ...maybe I want my player to detect collision with a couple specific classes but those classes aren't subclasses of another class. Therefore, I can't just put one class for the argument, I can't put "null" either coz there's some classes I don't want to count in the collision.
In fact, I doubt this thread would have even been started if it were the case, as easy as it is to write it "long-hand". True, it may have; but, then again, was not specified.
The thread is right here, and I thinks it makes sense to be. I agree '||' can just be used but it could be that wasn't timothytitus's train of thought. The train of thought was probably that collision detection for multiple classes is needed, and there is no method for that.
I will no more discuss this unless further input from timothytitus is given, if it is given.
I'm not forcing you to reply, but the question makes sense and has been answered.
You need to login to post a reply.