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

2015/1/25

Interactive block

1
2
Jellyfish Jellyfish

2015/2/8

#
nvm fixed it
Jellyfish Jellyfish

2015/2/8

#
actually I didn't fix it
Jellyfish Jellyfish

2015/2/8

#
sorry
danpost danpost

2015/2/8

#
Please post the class code. Oh, and use code tags when posting code:
Jellyfish Jellyfish

2015/2/8

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class player extends Actor
{
    final int jSpeed = 20; //jump speed
    int ySpeed = 0, xSpeed = 0;
    boolean aboutFace;
    boolean onGround;

    /**
     * Act - do whatever the player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
    }    
    private void move()
    {
        int dx = 0;
        if (Greenfoot.isKeyDown("a")) dx--;
        if (Greenfoot.isKeyDown("d")) dx++;
        for (int i=0; i<speed; i++)
        {
            setLocation(getX() + dx, getY());
            if (getOneIntersectingObject(blocks.class) == null) continue;
            setLocation(getX(), getY() + 1);
            if (getOneIntersectingObject(blocks.class) == null) continue;
            setLocation(getX(), getY() - 2);
            if (getOneIntersectingObject(blocks.class) == null) continue;
            setLocation(getX() - dx, getY() + 1);
            return;
            setLocation(getX() + dx, getY());
            if (getOneIntersectingObject(hostiles.class) == null) continue;
            getWorld.removeObject(this);
            return;
        }
    }
    /**
     * Determines any changes in horizontal and vertical speeds for the actor.
     */
    private void getDirection()
        {
        //if (!onGround) return; // if not mid-air changes allowed
        // sets requested direction of move, or continues in current direction
        
        if (Greenfoot.isKeyDown("up") && onGround) // check jump
        {
            ySpeed -= jSpeed; 
        }
   

    }
}
Super_Hippo Super_Hippo

2015/2/8

#
The variable 'speed' doesn't exist. So you can't use it in line 29. You have 'jSpeed', 'xSpeed' and 'ySpeed', but no 'speed'. Also I don't see the reason for both of the "return" calls in the for loop. Line 39 will give you an unreachable statement like this, too.
Jellyfish Jellyfish

2015/2/9

#
how do I put in speed? What would the edited code be???
danpost danpost

2015/2/9

#
Line 12 appears to declare fields for the horizontal and vertical speeds ('xSpeed' and 'ySpeed'). The 'xSpeed' field apparently is not being used at all. You could replace 'speed' with 'xSpeed' in the 'move' method; however, its value was initialized to zero and there are no statements assigning it any other value.
davmac davmac

2015/2/9

#
Jellyfish wrote...
how do I put in speed? What would the edited code be???
How about this: what value did you intend 'speed' to represent? How would that value be determined?
You need to login to post a reply.
1
2