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

2014/8/7

Switch on/off collision problem...

AshenPhoenix AshenPhoenix

2014/8/7

#
Hey, Been trying this for a while now and I'm not getting anywhere. Trying to get some platforms that start without collision to check for collision when a switch is flipped. This down below is my current sad attempt for the problem:
private void checkCollisions()
    {   
        onGround = false;

        Class platform = Platform.class;
        Class greenplatform = GreenPlatform.class;
        Class redplatform = RedPlatform.class;

        if(platformState == 0) {
            platformArray[0] = platform;
            platformArray[1] = null;
            platformArray[2] = null; }
            
        if(platformState == 1) {
            platformArray[0] = platform;
            platformArray[1] = greenplatform;
            platformArray[2] = null; }
            
        if(platformState == 2) {
            platformArray[0] = platform;
            platformArray[1] = null;
            platformArray[2] = redplatform; }
            
        if(platformState == 3) {
            platformArray[0] = platform;
            platformArray[1] = greenplatform;
            platformArray[2] = redplatform; }

        for (Class platforms : platformArray)
        {
            while(getOneObjectAtOffset(0, getImage().getHeight()/2-1, platforms)!=null)
            {
                setLocation(getX(), getY()-1); 
                onGround=true; 
                Y_SPEED=0;
            }

            while(getOneObjectAtOffset(0, -getImage().getHeight()/2, platforms)!=null) 
            {
                setLocation(getX(), getY()+1);
                Y_SPEED = 0;
            }

            while(getOneObjectAtOffset(getImage().getWidth()/2, 0, platforms)!=null)
            {
                setLocation(getX()-1, getY());
                X_SPEED = 0;
            }

            while(getOneObjectAtOffset(-getImage().getWidth()/2, 0, platforms)!=null)
            {
                setLocation(getX()+1, getY());
                X_SPEED = 0;
            }
        }
    }
Some help getting in the right direction would be appreciated! :)
AshenPhoenix AshenPhoenix

2014/8/7

#
Might mention that with this code everything has collision, even the switches which should never check for collision...
Super_Hippo Super_Hippo

2014/8/7

#
Few questions: What is this 'platformState' and where is 'platforms' in the while loops defined? This code is in the class of the moveable actor, right? I would suggest to have the on/off state for example as a boolean in the platforms and if you check for them, you also check for this boolean and if it is false, you just ignore it. In your code I don't see anything which is related to these switches.
AshenPhoenix AshenPhoenix

2014/8/7

#
This is just the method for the collision checking. platformState is declared private in the same class and is an int that gets a value added to it when switches are flipped. platforms is declared in the for-loop... I thought it was used to name the current item the for-loop is working with :( I'm a beginner so I'm not sure how things work. This code is in the superclass of the moveable actor, yes. The relation above method has with the switches is the int platformState. I would just upload my project but since it's an assignment I don't want to do that before I've gotten graded. The game is working except for these platforms getting solid.
Super_Hippo Super_Hippo

2014/8/7

#
Ah, there is the 'platforms' variable, I should've used ctrl+f.... I think I still didn't get the switch-thing. It has 4 different states? What exactly does this array of platforms do? Or does the switch make a certain kind of platform invisible?
AshenPhoenix wrote...
This code is in the superclass of the moveable actor, yes.
Are the platforms also a subclass of this? What might be the problem is, that you do this "if(platformState == 0)" (and so on, you could use 'switch(platformState)..." for that by the way), but it looks like you check the platformState of your moving character instead of the state of the platform.
AshenPhoenix AshenPhoenix

2014/8/7

#
There are 2 switches of different colors, with on/off states, and then there's the ordinary platforms that are always solid. platformState is probably named badly, switchState is probably better since it get values added to it when the switches get flipped. The platforms are subclasses of another superclass named SolidObjects, but there's no code anywhere there. All the collision checking is done in this method above. As for my array idea above, I thought I could use it to replace the Class part of getOneObjectAtOffset(), so that I could add in the non-collision platforms when the switches are flipped and so get collision checking. Seems to not be working though :( So the if(platformState) is to check the current switches flipped and setup the array appropriatly, then the for-loop goes through the array per item if it's not null. That was the idea atleast :)
AshenPhoenix AshenPhoenix

2014/8/7

#
Alright, some trial and error has shown me that my for loop sort-of works. It's the platformState that isn't getting it's proper values. It never changes from 0. If I add the other platforms to the array where platformState == 0 then they get collision checking working. In mover superclass:
    public void setSolidState(int state)
    {
        if(state == 1)
            platformState += 1;
        if(state == 2)
            platformState += 2;
    }
in GreenSwitch:
    public void act() 
    {

                if(checkSwitch())
        {
            if(!isSwitched)
            {
                setSolidState(1);
                setImage(switchOn[0]);
                isSwitched = true;
            }
        }
    }
And the above is the same for RedSwitch, except that setSolidState gives the value 2 instead.
Super_Hippo Super_Hippo

2014/8/7

#
It is quite in interesting idea to put everything in one place like this, but I think it doesn't make that much sense. You check the collision for every platform object. What I would do is just check for a platform and look whether this platform is activated or not:
Platform platform = (Platform)getOneObjectAtOffset(0, getImage().getHeight()/2-1, Platform.class) //Platform as a superclass or this platforms, if you want them as different classes
if (platform.state) //have the variable 'state' or however you want to call it as a static variable in the three platform classes.
{
    //move it back
}
If you use the switch, you just set the state of the platforms.
if (clickedSwitch) //...
{
    RedPlatform.state = true;
    GreenPlatform.state = false;
}
I hope you get what I mean. Maybe it is possible like you try to do it, but I don't know how or why it doesn't work. I am not very good with arrays, lists and so on. Btw, this is what I meant with the switch.
platformArray[0] = platform;
switch(platformState)
{
    case 0: platformArray[1] = null; platformArray[2] = null; break;
    case 1: platformArray[1] = greenplatform; platformArray[2] = null; break;
    case 2: platformArray[1] = null; platformArray[2] = redplatform; break;
    case 3: platformArray[1] = greenplatform; platformArray[2] = redplatform; break;
}
Super_Hippo Super_Hippo

2014/8/7

#
Where do you save the platformState? It seems like everything is a subclass of this mover superclass and in this class, you have a private field named platformState. So every object has its own 'platformState'. If you change the variable, then you change a different one than the one you check in the collision code. Change it to 'static', maybe it will work then.
AshenPhoenix AshenPhoenix

2014/8/7

#
platformState is in the Mover class below: -Actor -Mover -MainChar -GreenSwitch -RedSwitch -SolidObjects -Platform -GreenPlatform -RedPlatform -Exit Static did make it somewhat work. For some reason it isn't incrementing platformState properly.
    public void setSolidState(int state)
    {
            platformState += state;
    }
It starts at 0, it gains 1 when green switch is flipped, and 2 if the red switch is flipped. But when I hit the green switch first and get it to 1, then when i hit the red switch it doesn't increment by 2, it overwrites the value :(
Super_Hippo Super_Hippo

2014/8/7

#
'platformState' and 'setSolidState' are in the Mover class and in RedSwitch and GreenSwitch aren't platformState variables, right?
AshenPhoenix AshenPhoenix

2014/8/7

#
Nvm, I just removed setSolidState and set the values directly in the switch classes with Mover.platformState += value; It's working now! Thanks for all the help, learned a lot of new things :) But for your question, no there where no platformState variables there, I tried to send the values with the setSolidState(integer) method and set the values in the mover class in the setSolidState method.
Super_Hippo Super_Hippo

2014/8/7

#
Yes, I only asked because this would be a mistake. ^^ No problem!
You need to login to post a reply.