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

Report as inappropriate.

danpost
danpost presents ...

2014/4/15

Jump and Run Demo w/Moving Platform

I tried to make the code as simple as possible for what it does. It has well documented code which can be viewed by clicking the button obstacles across the bottom edge of the world. Use the escape (ESC) key to return to the demo.

Left (<) and right (>) arrows to move.
Up (^) arrow to jump.

11705 views / 51 in the last 7 days

11 votes | 0 in the last 7 days

Tags: demo jump platform danpost moving_platform source-text

This scenario is a member of: Demos by danpost


Your browser does not support the canvas tag.
danpostdanpost

2017/12/20

@Hippo, escape works for me in HTML5.
Super_HippoSuper_Hippo

2017/12/20

Okay, maybe that's only a problem with Edge and Internet Explorer then.
Nice Game! I like how the code is as simple as possible for a very common game type. Thank you!
saw444saw444

2022/10/5

Why is he not jumping? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Dante here. * * @author (your name) * @version (a version number or a date) */ public class Dante extends Actor { /** * Act - do whatever the Dante wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ static final int gravity = 2; static final int jumpForce = 30; int xSpeed = 4; int ySpeed = 0; public void act() { // Add your action code here. moveHorizontal(); } public void moveHorizontal() { int worldWidth = getWorld().getWidth(); int myWidth = getImage().getWidth(); int dx = 0; if(Greenfoot.isKeyDown("a")) dx--; if(Greenfoot.isKeyDown("d")) dx++; setLocation(getX()+dx*xSpeed, getY()); } private void moveVertically() { int worldHeight = getWorld().getHeight(); int myHeight = getImage().getHeight(); boolean onGround = false; ySpeed += gravity; setLocation(getX(), getY()+ySpeed); if(getY() > worldHeight-myHeight/2) { ySpeed = 0; onGround = true; } if(onGround && Greenfoot.isKeyDown("w")) { ySpeed =-jumpForce; } } }
saw444saw444

2022/10/5

ah i know why
WkotWkot

2023/2/19

hey could you please explain to me what this: worldHeight-myHeight/2 does, thanks
danpostdanpost

2023/2/19

@Wkot, to prevent the Who actor from sinking below the bottom of the screen, its y-coordinate must be kept below a point in the world that is half the actor's image height less than the world height. That expression gives that y-coordinate of the line that the center of the actor is not allowed to go beyond in the downward direction.
WkotWkot

2023/2/19

ok, thank you very much
maxminkymaxminky

2023/5/19

I have implemented the same exact movement code in my game, however the actor floats on either end of the platform even though the plat form isn't that big. Its as if the platform is wide and the image is too small in either direction. Do you know what the fix could be.
danpostdanpost

2023/5/19

@maxminky, it is possible the your actor has excess transparency as well.

See all comments

Want to leave a comment? You must first log in.

Who likes this?

objetos2223 Wkot AbdulrazaqAS Dlantz JBanyer JoyBajwa madflac fejfo JeppDev herusaputra Arthur114