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/6

#
I didn't change anything except adding spin() to the end of the act() method. And that's when it started crashing
davmac davmac

2012/3/6

#
I can't see why that would be the case. What is the stack trace you get in the terminal?
tallyaka tallyaka

2012/3/7

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Mover.move(Mover.java:55) at Mover.move(Mover.java:46) at Bone.<init>(Bone.java:16) at Polyphemus.fire(Polyphemus.java:93) at Polyphemus.shoot(Polyphemus.java:102) at Polyphemus.act(Polyphemus.java:31) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) That's the terminal I got.
danpost danpost

2012/3/7

#
In your Bone class constructor, line 6 (move()) tries to 'move' the object, but it is still being constructed, and not yet in the world. Remove that line and see what happens then. From the terminal output, it is probably line 16 now (see if you can figure out where I saw that).
tallyaka tallyaka

2012/3/7

#
line 16 is public void spin()... what's wrong with that? also,removing move() in the constructor fixed the problem. Also also, can anyone get me the initial help that I needed? About collision? Like I said, what I have works, it's just that once I touch a block.class, it can't move in any direction.
danpost danpost

2012/3/7

#
tallyaka wrote...
line 16 is public void spin()... what's wrong with that?
Not a thing, as far as I am aware. I was not sure if your code was still the same as when you posted the Bone class code or not.
tallyaka wrote...
About collision?
I do not see any of your code relating to this. And, is the actor player controlled or not? and what exactly do you see when it gets stuck on a Block class object (does it wiggle in place? turn left and right when keys pressed? or absolutely nothing? or something else)?
tallyaka tallyaka

2012/3/7

#
Absoloutely nothing. And also, it's in the first code I posted and it's mixed in the the walk() method. Here it is again just for you danpost :P
    public void walk()  
    {   
    Block b = (Block)getOneIntersectingObject(Block.class); 
                if(Greenfoot.isKeyDown("down"))  
            {
        if(b == null){
            setLocation(getX(), getY() + 3);  
                setDirection(WEST);
                }
                }  

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

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

            if(Greenfoot.isKeyDown("up"))  
        {  
        if(b == null){
                setLocation(getX(), getY() - 3);  
                setDirection(EAST); 
                }
                }  

    }  
tallyaka tallyaka

2012/3/7

#
Also, here is my current bone code...
public class Bone extends Mover
{
    private int rotation;
    public Bone()
    {
        rotation = getRotation();
    }

    public void act() 
    {
        setRotation(rotation);        
        move(5);
        spin();
        disappear();
    }
    private int rotation2;    
    public void spin()
    {
        rotation2 = rotation2 + 10;
        setRotation(rotation2);
    }
    
    public void disappear()
    {
        if(atWorldEdge())
        {
            getWorld().removeObject(this);
        }
    }
}
davmac davmac

2012/3/7

#
Please, please, fix the indentation for the walk() method and re-post it. It's so hard to read without proper indentation. Use the 'auto-layout' function in the editor.
davmac davmac

2012/3/7

#
And: you can't move when you're touching a block, because that's how it's coded. Line 3 finds an intersecting block, and all the movement is surrounded with "if (b == null) { ... }" and so can only happen if you're not touching a block. If you want to prevent walking into a block, you need to look ahead in the direction of movement - use getOneObjectAtOffset(...), with a different offset depending on the direction (i.e. depending on which key is pressed) - instead of getOneIntersectingObject(...). OR, check for an intersecting object after moving, and if there is a collision, move back to the original position immediately.
tallyaka tallyaka

2012/3/8

#
Ok, I get what your saying. I'm sorry about the walk indentation, it was an accident. Usually I like it indented correctly it's just that when I copied it it messed up. Sorry
tallyaka tallyaka

2012/3/8

#
This is my new Walk() method...
    public void walk()  
    {   
        Block b1 = (Block)getObjectsAtOffset(0, -2, Block.class); 
        if(Greenfoot.isKeyDown("down"))  
        {
            if(b1 == null){
                setLocation(getX(), getY() + 3);  
                setDirection(WEST);
            }
        }  
        Block b2 = (Block)getObjectsAtOffset(2, 0, Block.class);
        if(Greenfoot.isKeyDown("right"))  
        {  
            if(b2 == null){
                setLocation(getX() + 3, getY());  
                setDirection(SOUTH); 
            }
        }  
        Block b3 = (Block)getObjectsAtOffset(-2, 0, Block.class);
        if(Greenfoot.isKeyDown("left"))  
        {  
            if(b3 == null){
                setLocation(getX() - 3, getY());  
                setDirection(NORTH);  
            }
        }  
        Block b4 = (Block)getObjectsAtOffset(0, 2, Block.class);
        if(Greenfoot.isKeyDown("up"))  
        {  
            if(b4 == null){
                setLocation(getX(), getY() - 3);  
                setDirection(EAST); 
            }
        }  

    }
java.lang.ClassCastException: java.util.ArrayList cannot be cast to Block
	at Odysseus.walk(Odysseus.java:107)
	at Odysseus.act(Odysseus.java:37)
	at greenfoot.core.Simulation.actActor(Simulation.java:507)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
	at greenfoot.core.Simulation.runContent(Simulation.java:204)
	at greenfoot.core.Simulation.run(Simulation.java:194)

This is the terminal that I got. I have no idea what I did wrong but this came up as soon as I tried to run the Scenario.
davmac davmac

2012/3/8

#
My suggestion from previous post: ... use getOneObjectAtOffset(...) ... versus your code: Block b1 = (Block)getObjectsAtOffset(0, -2, Block.class); The exception actually gives you a good hint in this case. "ArrayList cannot be cast to Block" - you are taking a List (which is what getObjectsAt(...) returns) and casting it to a Block, which isn't possible.
tallyaka tallyaka

2012/3/8

#
Ok, I implemented this and now I just walk up to a Block and when I get close enough to the point at which I stop moving, I can't move in any direction. This is the same result as before.
davmac davmac

2012/3/8

#
Try a larger offset.
There are more replies on the next page.
1
2
3