So i'm quite new to Greenfoot and i'm having difficulty understanding why my character is falling through the ground and how to fix it. The fixes i've tried so far have led to some interesting outcomes (the character flying was my favorite).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | import greenfoot.*; /** * Write a description of class Hero here. * * @author (your name) * @version (a version number or a date) */ public class Hero extends Mover { private int vSpeed = - 1 ; private static final int acceleration = 2 ; private static final int speed = 7 ; /** * Act - do whatever the Hero wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. checkMove(); exit(); checkFall(); celing(); heroDeath(); } //end method act /** * Veritifi if the hero is on ground. */ public boolean onGround() { // Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 + 2, Ground.class); Object under = getOneIntersectingObject(Ground. class ); return (under != null ); } /** * This method is handeling movement of the hero. */ private void checkMove() { if (Greenfoot.isKeyDown( "left" )) { setImage( "Character 2 - left.png" ); moveLeft(); } // moveRight(); if (Greenfoot.isKeyDown( "right" )) { setImage( "Character 2.png" ); moveRight(); } if (onGround() && Greenfoot.isKeyDown( "up" )) { jump(); } } //end method checkMove /** * This method allow our character to jump. */ public void jump() { vSpeed = - 22 ; Gravity(); } /** * Move the main character to right */ public void moveRight() { setLocation(getX() +speed, getY()); } public void moveLeft() { setLocation(getX() -speed, getY()); } /** * Set a speed for a vertical movement. Positive values go down, negative values * go up. */ public void setVSpeed( int speed) { vSpeed = speed; } /** * Apply gravity and fall downwards until we hit ground or the bottom of the screen. */ private void Gravity() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } /** * checck if the chacracter is on ground, if not then he will fall. */ public void checkFall() { if (onGround() ) { vSpeed = 0 ; } else { Gravity(); } } public void celing(){ Actor block = getOneIntersectingObject(Ground. class ); if (block!= null ){ vSpeed = 1 ; } else { Gravity(); } } public void exit() // If Player has the key, they can open the door. { if (canSee(Exit. class )) { Greenfoot.setWorld( new Level2( this )); } } /** * Return true if we can see an object of class 'clss' right where we are. * False if there is no such object here. */ public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset( 0 , 0 , clss); return actor != null ; } /** * Try to grab an object of class 'clss'. This is only successful if there * is such an object where we currently are. Otherwise this method does * nothing. */ public void get(Class clss) { Actor actor = getOneObjectAtOffset( 0 , 0 , clss); if (actor != null ) { getWorld().removeObject(actor); } } private void heroDeath(){ Actor block = getOneIntersectingObject(Enemy. class ); if (block!= null ) { //getWorld().removeObject(this); Greenfoot.stop(); } } } |