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

2017/6/12

Can't get my Mouse player to appear on screen or work properly with keys

Karol45 Karol45

2017/6/12

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * * * @author Karol * @version 1.0 */ public class Mouse extends Actor { private int speed = 7; // running speed (sideways) private int vspeed = 0; private int acceleration = 1; /** * Check keyboard input and act accordingly */ public void act() { checkKeys(); checkfall();// to check if I should fall //Fall(); } // @param receives no paramenters // @return void private void checkKeys() { if (Greenfoot.isKeyDown("left") ) { setImage("Mouse_left.png"); moveLeft(); } if (Greenfoot.isKeyDown("right") ) { setImage("Mouse_right.png"); moveRight(); } if (Greenfoot.isKeyDown("space")) { Jump(); } } public void checkfall() { if(onGround()) { vspeed = 0; } else { Fall(); } } // @param receives no parameters // returns a boolean public boolean onGround() { //Actor ground = getOneObjectAtOffset ( 0, 0, Ground.class); Actor ground = getOneObjectAtOffset ( 0, getImage().getHeight() / 2, Platform.class); return ground != null;//!null means there is ground } // @param receives no parameters // @return void public void moveRight() { setLocation (getX() + speed, getY() ); } // @param receives no parameters // @return void public void moveLeft() { setLocation (getX() - speed, getY() ); } public void Jump() { vspeed = -6; Fall(); } public void Fall() { setLocation (getX(), getY() + vspeed); vspeed = vspeed + acceleration; } }
Super_Hippo Super_Hippo

2017/6/12

#
You need to add a mouse object in the world.
//in your world constructor or called from it, e. g. prepare method
addObject(new Mouse(), 50, 50); //change 50, 50 to the x/y coordinates where it should appear.
Karol45 Karol45

2017/6/12

#
I do have it in my World. I have public void prepare() { addObject(new Mouse(), 300, 50); } Is there anything else I should be doing. I am also calling the method: public World1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 500, 1); prepare();
Super_Hippo Super_Hippo

2017/6/12

#
That's all? Is it the active world? Then there is nothing wrong with it. Maybe you have to click Run and then left or right if the mouse doesn't have a default image.
Karol45 Karol45

2017/6/13

#
I got it to work I had to take the code inside of prepare() and jus place it in the World. Thank you. Like this public World1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 500, 1); addObject(new Mouse(), 300, 50); Thank you.
You need to login to post a reply.