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

2017/3/19

Problem

ArctiCicle ArctiCicle

2017/3/19

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author ( ) * @version ( 1.0 3/2/17 ) */ public class Player extends Actor { public void act() { keyControlmove(); } /** * Create a player and initialize its images */ public Player() { * (this is an error IDK why ->)player = new GreenfootImage("heroClassStanding.png"); * this.setImage(playerImg); } /** * */ public void walkLeft(int distance) { this.walk(distance, -10, "HeroWalk.png", "HeroWalk2.png"); } /** * Walk a bit to the right. 'distance' determines how far to walk. Use small numbers (1 to 10). */ public void walkRight(int distance) { this.walk(distance, 10, "HeroWalk.png", "HeroWalk2.png"); } /** * Jump up. 'distance' determines how high to jump. Use small numbers (1 to 10). */ public void jumpUp(int distance) { this.jump(distance, -10, "small-cat-fly.png","small-cat-fly-2.png"); } /** * Jump down. 'distance' determines how far down to jump. Use small numbers (1 to 10). */ public void jumpDown(int distance) { this.jump(distance, 10, "small-cat-fly.png","small-cat-fly-2.png"); } /** * Internal walk method. Walk a given distance into a given direction, using given images. */ private void walk(int distance, int direction, String img1, String img2) { for (int i = 0; i < distance; i++) { super.setImage(img1); this.wait(4); super.setLocation(super.getX( ) + direction, super.getY()); super.setImage(img2); this.wait(4); super.setLocation(super.getX() + direction, super.getY()); } } /** * Internal jump method. Jump a given distance into a given direction, using given images. */ private void jump(int distance, int direction, String img1, String img2) { for (int i = 0; i < distance; i++) { super.setImage(img1); this.wait(4); super.setLocation(super.getX( ) , super.getY()+ direction); super.setImage(img2); this.wait(4); super.setLocation(super.getX( )+ direction , super.getY()); } } //Arrow Keys public void keyControlmove() { if(Greenfoot.isKeyDown("w")) { jumpUp(2); } if(Greenfoot.isKeyDown("A")) { move(-2); } if(Greenfoot.isKeyDown("D")) { move(2); } } }
Super_Hippo Super_Hippo

2017/3/19

#
1: Use code tags. 2: Do not just use 'Problem' as your title and don't describe it. 3: Try to not give unrelated information. Giving the following would have been enough;
public Player() 
{
    player = new GreenfootImage("heroClassStanding.png"); //(this is an error IDK why
    this.setImage(playerImg);
}
You try to set a GreenfootImage to a not created variable 'player' (=error), then you try to set another not created variable 'playerImg' as the image of the Player (=another error). Solution: Create the variable and use same name for both.
public Player()
{
    Greenfoot playerImg = new GreenfootImage("heroClassStanding.png");
    setImage(playerImg);
}
Or just don't create the temporary variable at all:
public Player()
{
    setImage(new GreenfootImage("heroClassStanding.png"));
}
By the way, I am pretty sure that you should not use the 'wait' method whatsoever.
Nosson1459 Nosson1459

2017/3/19

#
Super_Hippo wrote...
1: Use code tags. 2: Do not just use 'Problem' as your title and don't describe it. 3: Try to not give unrelated information. Giving the following would have been enough;
So basically you should read this.
Super_Hippo wrote...
public Player()
{
    setImage(new GreenfootImage("heroClassStanding.png"));
}
You don't need to use a GreenfootImage. You can just do:
public Player()
{
    setImage("heroClassStanding.png");
}
You need to login to post a reply.