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

2014/10/30

Have enum classes and Actor classes in the same array?

Entity1037 Entity1037

2014/10/30

#
I'm making hit detection in Eclipse and I need to check for specific TileType enum classes, AND certain actors in my game, however I can't seem to put them in the same array. What should I do?
danpost danpost

2014/10/30

#
Please show what you do have, so we have something to work with.
Entity1037 Entity1037

2014/11/3

#
Here is my class Matter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package data;
 
import static helpers.Clock.*;
import org.newdawn.slick.opengl.Texture;
import data.World.*;
 
public class Matter extends Actor{
     
    double xmove=0, ymove=0;
     
    public Matter(Texture texture, float x, float y, int width, int height){
        super(texture, x, y, width, height);
    }
     
    //Physics Stuff
     
    static double g = 1; //gravitational acceleration
    static String gravityDirection = "down";
    boolean reaction = false;
    int hw = getWidth()/2; //half of the hit box's width
    int hh = getHeight()/2; //half of the hit box's height
    String collided = "";
      
    public void physics(){
         
        collided = ""; //reset variable collided
         
        //Gravity acceleration
         
        if (gravityDirection.equals("down")){ymove += g;}
        if (gravityDirection.equals("up")){ymove -= g;}
        if (gravityDirection.equals("left")){xmove -= g;}
        if (gravityDirection.equals("right")){xmove += g;}
         
        //Hit detection
         
        float deltaX=(float) (xmove*Delta()); //actual distance moved in the x coordinate
        float deltaY=(float) (ymove*Delta()); //actual distance moved in the y coordinate
         
         
        //if (xmove!=0||ymove!=0){System.out.println(World.getTileAt((int)getX()+getWidth()/2, (int)getY()+getHeight()/2));}
         
        //Collision Detection
         
        TileType[] solids = {TileType.TileB}; //all classes that can be collided with
         
        for (int c = 0; c<solids.length; c++){//This for statement handles all of the tile collisions
             
            //Vertical Collisions
            for (int x=0; x<hw*2-1; x+=hw/2-2){
                 
                if (deltaY==0){break;} //if there is no vertical movement, then don't check for vertical collisions
                 
                //Down
                for (int y=0; y<=deltaY; y++){//If ymove is negative, then the loop wont activate in the first place
                    TileType tile = World.getTileAt((int) getX()+x+1, (int) getY()+hh*2+y);
                    if ((int)getY()+hh*2+y > World.getHeight()-1 || tile!=null && tile.equals(solids[c])){
                        setLocation(getX(),getY()+y);
                        collided += "down";
                        if (reaction){
                            deltaY/=3;
                            ymove/=3;
                        }else{
                            deltaY=0;
                            ymove=0;
                        }
                        break;
                    }
                }
                 
                if (ymove==0){break;} //if there is no vertical movement, then don't check for vertical collisions
                 
                //Up
                for (int y=0; y>=deltaY; y--){
                    TileType tile = World.getTileAt((int) getX()+x+1, (int) getY()+y-1);
                    if ((int)getY()+y-1 < 0 || tile!=null && tile.equals(solids[c])){
                        setLocation(getX(),getY()+y);
                        collided += "up";
                        if (reaction){
                            deltaY/=3;
                            ymove/=3;
                        }else{
                            deltaY=0;
                            ymove=0;
                        }
                        break;
                    }
                }
            }
             
            //Horizontal Collisions
            for (int y=0; y<hh*2-1; y+=hh/2-2){
                 
                if (deltaX==0){break;} //if there is no horizontal movement, then don't check for horizontal collisions
                 
                //left
                for (int x=0; x>=deltaX; x--){
                    TileType tile = World.getTileAt((int) getX()+x-1, (int) getY()+y+1);
                    if ((int)getX()+x-1 < 0 || tile!=null && tile.equals(solids[c])){
                        setLocation(getX()+x,getY());
                        collided += "left";
                        if (reaction){
                            deltaX/=3;
                            xmove/=3;
                        }else{
                            deltaX=0;
                            xmove=0;
                        }
                        break;
                    }
                }
                 
                if (deltaX==0){break;} //if there is no horizontal movement, then don't check for horizontal collisions
                 
                //right
                for (int x=0; x<=deltaX; x++){
                    TileType tile = World.getTileAt((int) getX()+hw*2+x, (int) getY()+y+1);
                    if ((int)getX()+hw*2+x > World.getWidth()-1 || tile!=null && tile.equals(solids[c])){
                        setLocation(getX()+x,getY());
                        collided += "right";
                        if (reaction){
                            deltaX/=3;
                            xmove/=3;
                        }else{
                            deltaX=0;
                            xmove=0;
                        }
                        break;
                    }
                }
            }
        }
        setLocation(getX()+deltaX,getY()+deltaY);
        System.out.println(collided);
    }
     
    static public void setGravityDirection(String s){
        gravityDirection = s;
    }
     
    static public void setGravitationalAcceleration(int i){
        g = i;
    }
}
I want the array "solids" to store classes of type "TileType and type "Actor" (which includes all subclasses of Actor).
danpost danpost

2014/11/3

#
As you have it, 'solids' can only hold TileType objects (objects created by the TileType class or a subclass of it). If you want it to hold other objects, maybe you should use:
1
2
3
4
// this
Object[] solids = ...;
// instead of
TileType[] solids = ...;
Entity1037 Entity1037

2014/11/3

#
Oh! I didn't know Objects was a type you could use. Thank you!
Entity1037 Entity1037

2014/11/3

#
Wait, it doesn't work if I put "Enemy.class" in the array.
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
If you're using enemy.class shouldn't you be using Actors instead of Objects?
Entity1037 Entity1037

2014/11/5

#
If I use Actors, then I can't put TileType enum classes in the array due to enum classes being unable to extend other classes.
danpost danpost

2014/11/5

#
I will look into your issue here. I was not aware that Enum values were not allowed as an Object object (I was under the impression that ALL classes were subclasses of the Object class).
danpost danpost

2014/11/5

#
I have tested it out. You sure can add 'EnumClassName.ENUM_VALUE' as well as 'EnumClassName.class' in an array of type Object. If the Enum class is defined in another class (not the Matter class or a superclass of it), then it must be defined as 'public' and you must include the class it is found in when referencing it in the other class.
danpost danpost

2014/11/6

#
Entity1037 wrote...
Oh! I didn't know Objects was a type you could use.
Just to clarify -- it is not 'Objects', but 'Object' as the name of the class.
Wait, it doesn't work if I put "Enemy.class" in the array.
An array of Object type will accept all values and objects (provided they are defined and referred to properly). I would think it goes without saying that 'void' is not a valid entry as all elements are 'null' (not 'void') to begin with.
You need to login to post a reply.