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

Discussions

You need to login to take part
Rss

Current Discussions

GameCreator

HELP, I can't figure out how to make my actor only jump once in the air!

By GameCreator, with no replies.
//here is my code, please help import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Welcome to the Walker (Robot) class! * You are the Robot! * * @author (Isaiah Williams) * @version (1.0) */ public class Walker extends Actor { private int vSpeed = 0; // here shows the vertical speed private int acceleration = 1; private int speed = 3; // This shows that when I write speed below it equals 3 private boolean jumping; /* * Here are my images that are for Walker when he walks left only below * Below I tell of synonyms for what I want the images to be called */ private GreenfootImage run0L = new GreenfootImage("tmp-0.png"); // here I show how a soon to be Greenfoot image,tmp-0.png, will be named run0L in Greenfoot language private GreenfootImage run1L = new GreenfootImage("tmp-1.png"); private GreenfootImage run2L = new GreenfootImage("tmp-2.png"); private GreenfootImage run3L = new GreenfootImage("tmp-3.png"); private GreenfootImage run4L = new GreenfootImage("tmp-4.png"); private GreenfootImage run5L = new GreenfootImage("tmp-5.png"); private GreenfootImage run6L = new GreenfootImage("tmp-6.png"); private GreenfootImage run7L = new GreenfootImage("tmp-7.png"); private GreenfootImage run8L = new GreenfootImage("tmp-8.png"); private GreenfootImage run9L = new GreenfootImage("tmp-9.png"); private GreenfootImage run10L = new GreenfootImage("tmp-10.png"); private GreenfootImage run11L = new GreenfootImage("tmp-11.png"); private GreenfootImage run12L = new GreenfootImage("tmp-12.png"); private GreenfootImage run13L = new GreenfootImage("tmp-13.png"); private GreenfootImage run14L = new GreenfootImage("tmp-14.png"); private GreenfootImage run15L = new GreenfootImage("tmp-15.png"); private GreenfootImage run16L = new GreenfootImage("tmp-16.png"); private GreenfootImage run17L = new GreenfootImage("tmp-17.png"); private GreenfootImage run18L = new GreenfootImage("tmp-18.png"); private GreenfootImage run19L = new GreenfootImage("tmp-19.png"); private GreenfootImage run20L = new GreenfootImage("tmp-20.png"); private GreenfootImage run21L = new GreenfootImage("tmp-21.png"); private GreenfootImage run22L = new GreenfootImage("tmp-22.png"); private GreenfootImage run23L = new GreenfootImage("tmp-23.png"); private GreenfootImage run24L = new GreenfootImage("tmp-24.png"); private GreenfootImage run25L = new GreenfootImage("tmp-25.png"); /* * Here are my images that are for Walker when he walks right only below * Below I tell of synonyms for what I want the images to be called */ private GreenfootImage run0R = new GreenfootImage("tmp-0R.png"); // here I show how a soon to be Greenfoot image,tmp-0R.png, will be named run0R in Greenfoot language private GreenfootImage run1R = new GreenfootImage("tmp-1R.png"); private GreenfootImage run2R = new GreenfootImage("tmp-2R.png"); private GreenfootImage run3R = new GreenfootImage("tmp-3R.png"); private GreenfootImage run4R = new GreenfootImage("tmp-4R.png"); private GreenfootImage run5R = new GreenfootImage("tmp-5R.png"); private GreenfootImage run6R = new GreenfootImage("tmp-6R.png"); private GreenfootImage run7R = new GreenfootImage("tmp-7R.png"); private GreenfootImage run8R = new GreenfootImage("tmp-8R.png"); private GreenfootImage run9R = new GreenfootImage("tmp-9R.png"); private GreenfootImage run10R = new GreenfootImage("tmp-10R.png"); private GreenfootImage run11R = new GreenfootImage("tmp-11R.png"); private GreenfootImage run12R = new GreenfootImage("tmp-12R.png"); private GreenfootImage run13R = new GreenfootImage("tmp-13R.png"); private GreenfootImage run14R = new GreenfootImage("tmp-14R.png"); private GreenfootImage run15R = new GreenfootImage("tmp-15R.png"); private GreenfootImage run16R = new GreenfootImage("tmp-16R.png"); private GreenfootImage run17R = new GreenfootImage("tmp-17R.png"); private GreenfootImage run18R = new GreenfootImage("tmp-18R.png"); private GreenfootImage run19R = new GreenfootImage("tmp-19R.png"); private GreenfootImage run20R = new GreenfootImage("tmp-20R.png"); private GreenfootImage run21R = new GreenfootImage("tmp-21R.png"); private GreenfootImage run22R = new GreenfootImage("tmp-22R.png"); private GreenfootImage run23R = new GreenfootImage("tmp-23R.png"); private GreenfootImage run24R = new GreenfootImage("tmp-24R.png"); private GreenfootImage run25R = new GreenfootImage("tmp-25R.png"); private int frame = 1; // Here I show a "frame" equals one private int animationCounter = 0; //Here I show how the animationCounter is zero by itself /** * Act - do whatever the Robot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { /** * Here are constructors that I want activated in my World! */ checkKey(); // checkKey is used to check what keys are pressed animationCounter ++; // animationCounter is used to count the animations know which frame goew where checkFall(); } public void noMidJump() { if (isTouching(Platform.class)) { String key = Greenfoot.getKey(); // this allows the key to be in effect only once even when held in if ("z".equals(key)) { jump(); } } } public void fall() { setLocation( getX(), getY() + vSpeed); // this will allow Walker to fall by changing the Y cordinate by including the number incorporated with vertical speed if (vSpeed <=9) // 9 is the maximum speed Walker can move upwards/downwards { vSpeed = vSpeed + acceleration; // this shows that as Walker falls acceleration will be added to his fall every cycle // if just = 2;this shows that if Walker is falling he will fall 2 pixels per cycle } jumping = true; // this shows that you must be touching something in order to jump } public boolean onPlatform() { int WalkerHeight = getImage().getHeight(); // Distance from middle of Walker to ground int lookForPlatform = (int)(WalkerHeight/2) - 5 ; // (int) is there so there will be no decimal, - 5 is there because I want some less so it may be so many pixels above the character that will be in contat with the othe picture, + would be so many pixels above the platform/image. Actor Platform = getOneObjectAtOffset(0, lookForPlatform, Platform.class); if (Platform == null) { jumping = true;// this helps to control return false; // this shows if we are not on the ground it will return false } else { return true; // this shows if we are not on the ground it will return true } } public void checkFall() { if (onPlatform()) // if the method checkFallis true vSpeed is normal and fallinf stops for Walker { vSpeed = 0; } else // if the method checkFall is not true then the method fall will take action { fall(); } } public void checkKey() { if (Greenfoot.isKeyDown("Right")) // this shows if the gamer holds the right key the method, moveRight will be put to action moveRight(); if (Greenfoot.isKeyDown("Left")) // this shows if the gamer holds the left key the method moveLeft will be put to action moveLeft(); } public void jump() { vSpeed = -15; // allows for motion upwards fall(); // this allows to bring walker down } public void moveRight() { setLocation(getX()+speed, getY()); // we say positive speed, which is 3, so Walker walks left if (animationCounter % 2 == 0) // this allows the animation to slow, this says every 2 frames a slide of the next image will take place animateRight(); // this is the animation so it looks like Walker is walking to the right } public void moveLeft() { setLocation(getX()-speed, getY()); // we say minus speed, which is 3, so Walker walks left if (animationCounter % 2 == 0) // this allows the animation to slow, this says every 2 frames a slide of the next image will take place animateLeft(); // this is the animation so it looks like Walker is walking to the left } public void animateLeft() { /** * each frame will run a specific picture to make it seem like Walker is walking to the left, when the number of pictures run out the animation will start again. */ if (frame == 0) // here says if the frame is zero (the very first frame there is), image run0L, or tmp-0.png, will appear in place of Walker { setImage(run0L); } else if(frame == 1) // this continues the pattern as shown above... the next frame, 1, (the second frame), image run1L will appear in place of Walker { setImage (run1L); } else if(frame == 2) { setImage (run2L); } else if(frame == 3) { setImage (run3L); } else if(frame == 4) { setImage (run4L); } else if(frame == 5) { setImage (run5L); } else if(frame == 6) { setImage (run6L); } else if(frame == 7) { setImage (run7L); } else if(frame == 8) { setImage (run8L); } else if(frame == 19) { setImage (run9L); } else if(frame == 10) { setImage (run10L); } else if(frame == 11) { setImage (run11L); } else if(frame == 12) { setImage (run12L); } else if(frame == 13) { setImage (run13L); } else if(frame == 14) { setImage (run14L); } else if(frame == 15) { setImage (run15L); } else if(frame == 16) { setImage (run16L); } else if(frame == 17) { setImage (run17L); } else if(frame == 18) { setImage (run18L); } else if(frame == 19) { setImage (run19L); } else if(frame == 20) { setImage (run20L); } else if(frame == 21) { setImage (run21L); } else if(frame == 22) { setImage (run22L); } else if(frame == 23) { setImage (run23L); } else if(frame == 24) { setImage (run24L); } else if(frame == 25) { setImage (run25L); frame = 0; // this shows once frame 25 is reached, it equals frame 0 so it may restart this process of animation return; // just in case if there is an error, once frame 25 is reached the animation will go back to the beginning } frame ++; // this tells the frames are increasing numerically, least to greatest } public void animateRight() { /** * each frame will run a specific picture to make it seem like Walker is walking to the right, when the number of pictures run out the animation will start again. */ if (frame == 0) // here says if the frame is zero (the very first frame there is), image run0R, or tmp-0R.png, will appear in place of Walker { setImage(run0R); } else if(frame == 1) // this continues the pattern as shown above... the next frame, 1, (the second frame), image run1R will appear in place of Walker { setImage (run1R); } else if(frame == 2) { setImage (run2R); } else if(frame == 3) { setImage (run3R); } else if(frame == 4) { setImage (run4R); } else if(frame == 5) { setImage (run5R); } else if(frame == 6) { setImage (run6R); } else if(frame == 7) { setImage (run7R); } else if(frame == 8) { setImage (run8R); } else if(frame == 9) { setImage (run9R); } else if(frame == 10) { setImage (run10R); } else if(frame == 11) { setImage (run11R); } else if(frame == 12) { setImage (run12R); } else if(frame == 13) { setImage (run13R); } else if(frame == 14) { setImage (run14R); } else if(frame == 15) { setImage (run15R); } else if(frame == 16) { setImage (run16R); } else if(frame == 17) { setImage (run17R); } else if(frame == 18) { setImage (run18R); } else if(frame == 19) { setImage (run19R); } else if(frame == 20) { setImage (run20R); } else if(frame == 21) { setImage (run21R); } else if(frame == 22) { setImage (run22R); } else if(frame == 23) { setImage (run23R); } else if(frame == 24) { setImage (run24R); } else if(frame == 25) { setImage (run25R); frame = 0; // this shows once frame 25 is reached, it equals frame 0 so it may restart this process of animation return; // just in case if there is an error, once frame 25 is reached the animation will go back to the beginning } frame ++; // this tells the frames are increasing numerically, least to greatest } }