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
I don't know. I don't think it would be hard, considering I want to do almost the exact same thing as the wall() method (just with the Spike.class). And my wall() method works, I can run into a block and stop moving, because there's a piece of ground in the way! i find it weird that the code doesn't work, although i'm basically copying and pasting...
You know what? I'll just add a little challenge to the game and make my code simpler. I'm going to change the image of the spike to be spike all around, and if any contact is made, you'll get hurt.
JetLennit JetLennit

2013/4/2

#
when in doubt.... be creative!
danpost danpost

2013/4/2

#
First, make sure you change line 8 in your 'spike' method to
int width = getImage().getWidth();
Then, remove the '|| spike()' from the last line in the 'wall' method. Finally, call the methods individually from the act method
if (wall() || spike())
If one is working, then they both should work this way.
I had done that while trying to figure it out. Didn't work for some reason. Still baffled why it didn't work. But now I have the code that you just get hurt running into it.
danpost danpost

2013/4/2

#
It is probably code elsewhere that is messing with it.
Maybe, I looked through the code with an tutor (who uses Greenfoot) in my AP Computer Science class, and we couldn't find anything that would be messing with it. Even if there was something messing with it, my spike() method should have returned true if there was a spike right in front of the player. But it doesn't...
danpost danpost

2013/4/2

#
You could upload the scenario to the site including the source code so we could be more armed to help.
JetLennit JetLennit

2013/4/2

#
I was thinking the same thing (include the source to what you have posted)
I'll have it uploaded by tomorrow night. I have some homework to finish first, and I don't know if I'll be able to work some more on my game today.
I have the source code with my project: http://www.greenfoot.org/scenarios/7929 There is a lot of stuff already in there.
Oh forgot to mention, I removed the spike() method. Here it is:
/** 
     * 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().getWidth();  
        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;  
    } 
danpost danpost

2013/4/3

#
The Ground object reaches 10 pixels lower than the Spike object does. So, with the offsets you are using to locate the objects, the Ground object is found while the Spike object is not. My suggestion is the do this in the Spike constructor:
GreenfootImage current = getImage();
GreenfootImage image = new GreenfootImage(current.getWidth(), current.getHeight()+10);
image.drawImage(current, 0, 0);
setImage(image);
and then add five the the y location you add the Spike object into the world at.
You need to login to post a reply.
1
2