I have a problem with mousePressed(). I made a game for Android and ofc it'll need a button. So I made 4 buttons for moving, made them as static, and placed them in MyWorld class. Nothing wrong, no errors. Here's the code for assuring you:
And in the Coa class (which is equals to hero class), I wrote code as below:
I don't know why, but when I try to run, the button won't work. There's really no errors. I tried compiling for many times, still nothing wrong. Can you help me find what's wrong with my code? Thanks so much
import greenfoot.*;
public class MyWorld extends World
{
static Up up = new Up();
static Down down = new Down();
static Right right = new Right();
static Left left = new Left();
static Jump jump = new Jump();
static Slide slide = new Slide();
static Coa coa = new Coa();
public MyWorld()
{
super(500, 300, 1);
setBackground("lv1.png");
addObject(coa, 275, 275);
addObject(up, 450, 250);
addObject(down, 450, 280);
addObject(right, 475, 265);
addObject(left, 425, 265);
addObject(jump, 30, 275);
addObject(slide, 70, 275);
}
}
public boolean canMove()
{
return getWorld().getColorAt(getX(),getY()).equals(Color.white);
}
public Coa()
{
setImage("coa-std1.png");
}
public void walk(int x, int y)
{
if (canMove())
{
setLocation(getX()+x,getY()+y);
}else{}
}
public void act()
{
if(Greenfoot.mousePressed(MyWorld.up))
walk(0,-3);
if(Greenfoot.mousePressed(MyWorld.down))
walk(0,3);
if(Greenfoot.mousePressed(MyWorld.left))
walk(-3,0);
if(Greenfoot.mousePressed(MyWorld.right))
walk(3,0);
}

