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

2014/9/12

How do you make an Actor jump onto groun or platform

coder04 coder04

2014/9/12

#
This is my coding, can you tell me what I have done wrong because I can stand on the road but I can't stand on the Platform- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CommandoPig here. * * @author (your name) * @version (a version number or a date) */ public class CommandoPig extends Actor { private int speed = 4; private int vSpeed = 0; private int acceleration = 1; private int jumpStrength = 7; /** * Check keyboard input and act accordingly */ public void act() { checkKeys(); checkFall(); } private void checkKeys() { if (Greenfoot.isKeyDown("right")) { setImage("Commandopigtrans1.gif"); move(4); } if (Greenfoot.isKeyDown("left")) { setImage("Commandopigtrans0.gif"); move(-4); } if (Greenfoot.isKeyDown("up")) { setImage("Commandopigtrans2.gif"); jump(); Greenfoot.playSound("jump.wav"); } } public void jump() { vSpeed = -jumpStrength; fall(); } public void checkFall() { if(onRoad()) { vSpeed = 0; } else { fall(); } } public boolean onRoad() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight() / 2-8, Road.class); return under !=null; } public boolean onPlatform() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight() / 2-8, Platform.class); return under !=null; } public void fall() { setLocation ( getX(), getY() + vSpeed ); vSpeed = vSpeed + acceleration; } public void moveRight() { setLocation ( getX() - speed, getY() ); } public void moveLeft() { setLocation ( getX() - speed, getY() ); } }
Super_Hippo Super_Hippo

2014/9/12

#
Use the 'code' tags to post code! Then it looks much better and I could refer to line xy. In your 'checkFall' method, you check for a 'Road', but not for a 'Platform'. Change the if-condition in this method to
1
if( onRoad() || onPlatform() )
You need to login to post a reply.