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

2012/3/5

Collision

1
2
3
tallyaka tallyaka

2012/3/5

#
I'm trying to figure out how to make it so that when my actor (Odysseus) hits an object (Block) he stops moving. I tried what is said here but every time I reset a terminal came up and broke my game. :( help?
tallyaka tallyaka

2012/3/5

#
I also tried this but it didn't work either :(
    Block b = (Block)getOneIntersectingObject(Block.class);
    public void walk()
    {
        if(b == null)
        {
            if(Greenfoot.isKeyDown("down"))
            {
                setLocation(getX(), getY() + 3);
                setDirection(WEST);
            }

            if(Greenfoot.isKeyDown("right"))
            {
                setLocation(getX() + 3, getY());
                setDirection(SOUTH);
            }

            if(Greenfoot.isKeyDown("left"))
            {
                setLocation(getX() - 3, getY());
                setDirection(NORTH);
            }

            if(Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(), getY() - 3);
                setDirection(EAST);
            }
        }
    }
It came up with the same problem. The terminal said that it was trying to access an object while it wasn't in the world, since I forgot to mention that earlier.
davmac davmac

2012/3/5

#
The problem is that your code, above, is running while the actor isn't in the world. Perhaps you are calling it from the constructor, instead of from the act() method?
tallyaka tallyaka

2012/3/5

#
Nope, it's in the act() method. I just added it onto my move() method, so it was already in the act() method. P.S. Do you know how I could make an image spin while using the mover support class's move(); without making the whole actor run in circle?? e.g. throwing a bone and it continuously moving forward but the image is still spinning. Thanks!
davmac davmac

2012/3/5

#
Then perhaps your act() method removes the object from the world, before it executes the code that you posted above?
tallyaka tallyaka

2012/3/6

#
It's the first thing in the act method. Just changed it. Still getting a terminal :( also... check my P.S. in the previous post of mine
davmac davmac

2012/3/6

#
It's the first thing in the act method. Just changed it. Still getting a terminal :(
Is it the same stack trace? Error happening at the same place? I think you'll have to post more of your code.
Do you know how I could make an image spin while using the mover support class's move(); without making the whole actor run in circle??
Keep track of the direction of movement separately. When you move the bone, do the following steps: 1. get the current rotation, and store it in a variable 2. set the rotation to the desired direction of movement 3. move 4. restore the original rotation
danpost danpost

2012/3/6

#
Line 1 should be inside the method (between line 3 and 4).
tallyaka tallyaka

2012/3/6

#
danpost wrote...
Line 1 should be inside the method (between line 3 and 4).
I tried this and it worked... it's just that now when I run into a block, I don't move at all... in any direction. This is slightly problematic... and also, davmac, I tried what you said and he always threw it to the right. Can you be a little more specific in what you were telling me to do?
davmac davmac

2012/3/6

#
Can you be a little more specific in what you were telling me to do?
I think it's fairly specific already? Is there any particular thing that isn't clear? Have you tried writing some code, can you post it?
davmac davmac

2012/3/6

#
Line 1 should be inside the method (between line 3 and 4).
I don't know how I missed this!!
tallyaka tallyaka

2012/3/6

#
This is my whole Bone.class:
public class Bone extends Mover
{
    private int rotation;
    public Bone()
    {
        move();
        rotation = getRotation();
    }

    public void act() 
    {
        setRotation(rotation);
        move(5);
    }
    private int rotation2;    
    public void spin()
    {
        rotation2 = rotation2 + 10;
        setRotation(rotation2);
    }
}
davmac davmac

2012/3/6

#
So I guess your act() method is where the movement is coded. You seem to be missing step 1 (get current rotation and store it in a variable) and step 4 (restore original rotation). Add code for these around the current code in the act method.
tallyaka tallyaka

2012/3/6

#
so yes, I forgot to add spin into the act() method, but when I did do that I just ended up with a terminal where it's trying to access an actor that's not in the world. This only happened when he actually threw the bone but before that it was fine. I have no idea how to do so much rotation setting all in one act. I think it made my game explode...
davmac davmac

2012/3/6

#
I guess if you post what you wrote, we might be able to help...
There are more replies on the next page.
1
2
3