I tried writing this code to my game and i got an error message under the What class with the this(0); it was saying that "call to this must first statement in constructor",
@docinkc, start a new discussion thread and using the 'code' tag below the input box to insert the problem code. Include a brief explanation of the problem.
Is it just me or is the escape button not working with HTML 5? For example in this scenario, I can't leave the code view with escape. (x works). Not sure if this was always the case, but right now, it doesn't seem to work.
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;
}
}
}
@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.
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.
2014/4/15
2014/4/15
2014/4/15
2014/4/15
2014/6/8
2014/6/8
2014/9/15
2015/2/22
2017/12/19
2017/12/19
2017/12/20
2017/12/20
2020/2/4
2022/10/5
2022/10/5
2023/2/19
2023/2/19
2023/2/19
2023/5/19
2023/5/19