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

2013/4/2

What's wrong with my code?

1
2
In my game, I have a Spike actor. When the player runs into the dull side, they should stop as if there was a wall ahead of them. And they should be able to walk on it (but by being damaged) as well. Currently, I'm working on making the player act like it's a wall. But for some reason, my code doesn't work. I just copied my method to find a wall, and changed it to work for Spikes, but it doesn't work. Here's the code:
/**
     * Checks to see if a Upward facing Spike is ahead
     * @return true if there's a spike ahead
     */
    public boolean spike()
    {
        int height = getImage().getHeight();
        int width = getImage().getHeight();
        Actor spike = null;

        if (flipped)
            spike = getOneObjectAtOffset(width/2 + 2, -height/2, Spike.class);
        else
            spike = getOneObjectAtOffset(-width/2 - 2, -height/2, Spike.class);

        return spike != null;
    }
Here's what my wall method looks like:
/**
     * Checks if there is a wall ahead
     * @return true if there's a wall ahead
     */
    public boolean wall()
    {
        int height = getImage().getHeight();
        int width = getImage().getWidth();
        Actor ground = null;
        if (flipped)
            ground = getOneObjectAtOffset(width/2 + 2, -height/2, Ground.class);
        else
            ground = getOneObjectAtOffset(-width/2 - 2, -height/2, Ground.class);

        return ground != null || spike();
    }
Also, I feel I should mention that the Spike object is a subclass of DamageActor. (basically an actor that can damage other actors) I also have code that stops the player when wall() is true. wall() is false becuase spike() is false, and I don't know why. Thanks for the help.
bump
JetLennit JetLennit

2013/4/2

#
what is the height of the image?
62 pixels. Width is 50
JetLennit JetLennit

2013/4/2

#
then why do you have
 int height = getImage().getHeight();
        int width = getImage().getHeight();
instead of
int height =  62;
        int width = 50;
?
The code works for Ground (in the wall method) I just wonder why it doesn't work for the spike method, when it's basically the same thing, but for Spike.class
DOH
JetLennit JetLennit

2013/4/2

#
DOH?
Still didn't work for some reason...
changed width to = getImage().getWidth()
JetLennit JetLennit

2013/4/2

#
I was also wondering why you had said that... although i had assumed that was purposeful (I didnt fully understand the code)
Still not working... When I walk right up next to the spike, it says it's not there...
JetLennit JetLennit

2013/4/2

#
Maybe one of the good programmers are able to help you further.. that was my only idea
Just talked with someone else about this, and he can't seem to figure it out either...
JetLennit JetLennit

2013/4/2

#
maybe you can rewrite the code with something like this ?
There are more replies on the next page.
1
2