Here's an example of how you could use Greenfoot. And then you can *also* ask it, "Did I press space?" And it will say, yes, you *also* pressed space. Instead of asking Greenfoot, "Which key did I press?" You can ask it, "Did I press this key?" That way, if you're pressing multiple keys, you can ask it, "Did I press left?" And it will say, yes, you did. I think there are many packman examples to be found on this webside and also some with a easy code for beginners. You packman just has to recognice that there is such an object on the field and so dont move there. It process key strokes *between* calls to act(), but not *during* them. This objects dont even have to do anything. If you pressed space in-between calling Walk() and calling Jump(), it's not going to catch it, because it hasn't had time to process any key strokes between those two method calls. And then, later, in the same call to the act() method, if you ask it again, "Which key did I press?" It'll still say you pressed left, because it can't process key presses while it's busy running your code. So, if you press left, and Greenfoot detects that you pressed left, and you ask it, "Which key did I press?" It'll say you pressed left. Your friend Jonas can't Jump() while Walk()ing, because Greenfoot.getKey() can only return one key per frame. I am trying to make it so that whenever I get the guess wrong, the gameover screen appears. Your problem is probably that you're using Greenfoot.getKey(). Please if someone know the problem I would really appreciate the help SetLocation(getX(), getY()+ySpeed) // leave ground If you want that one space is moved every time a key is pressed and released, then you can use the getKey method instead of the isKeyDown one. If ("space".equals(Greenfoot.getKey())) // jump key detected Greenfoot.getKey() // clears any key pressed during jump SetLocation(getX(), groundLevel) // set on ground It all works correctly except even if I only press the keys once, multiple of the same object is added to the same place. the last void doesnt seem to be working so maybe Ive just been putting it in the wrong spot. If they are, it adds an object to the screen. sorry if its messy i did my best to clean it up for you guys i was also wondering how to change my image after cell dies. For half width, just divide by two (which is what I used in both code sets above). I am trying to make a method that checks if two keys are pressed. If (getY()>=groundLevel) // has landed (reached ground level) The code you gave tries to get two-thirds the width, which I would do by way of a fraction (always multiplying first): getImage ().getWidth ()2/3. SetLocation(getX(), getY()+ySpeed) // fall (rising slower or falling faster) Int groundLevel = getWorld().getHeight() - getImage().getHeight()/2 īoolean onGround = (getY() = groundLevel) * the 'Act' or 'Run' button gets pressed in the environment.īackground background = (Background) getWorld() There are several ways to do this heres a simple one just to give you an idea: import greenfoot. * Act - do whatever the Jonas wants to do. I understand from your question that you want different classes that have different images, rather than an option for changing the image within the same class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |