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

2017/12/14

Button not working :(

PeloviaOliver PeloviaOliver

2017/12/14

#
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:
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);
    }
}
And in the Coa class (which is equals to hero class), I wrote code as below:
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);
    }
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
danpost danpost

2017/12/14

#
I would think that as long as the Coa object was placed on white to begin with, that it would at least move a little at first; although, you make it seem that it does not move at all. Now, with the code as is, I can see that once the Coa object ends up on some other color (which it is allowed to do, by code), it will never move again. This is do to the location you are checking the color at, which is where the Coa object already is; but, you need to check the location at which it wants to move.
DanPost_FAN69 DanPost_FAN69

2017/12/14

#
danpost wrote...
I would think that as long as the Coa object was placed on white to begin with, that it would at least move a little at first; although, you make it seem that it does not move at all. Now, with the code as is, I can see that once the Coa object ends up on some other color (which it is allowed to do, by code), it will never move again. This is do to the location you are checking the color at, which is where the Coa object already is; but, you need to check the location at which it wants to move.
MY HERO YOU
PeloviaOliver PeloviaOliver

2017/12/14

#
danpost wrote...
I would think that as long as the Coa object was placed on white to begin with, that it would at least move a little at first; although, you make it seem that it does not move at all. Now, with the code as is, I can see that once the Coa object ends up on some other color (which it is allowed to do, by code), it will never move again. This is do to the location you are checking the color at, which is where the Coa object already is; but, you need to check the location at which it wants to move.
Thank you for your reply. So I get that it must be "seen" that Coa is moving. So I changed this part:
public void act()
    {
        if(Greenfoot.mousePressed(MyWorld.up))
            walk(0,-10);
		if(Greenfoot.mousePressed(MyWorld.down))
            walk(0,10);
		if(Greenfoot.mousePressed(MyWorld.left))
            walk(-10,0);
		if(Greenfoot.mousePressed(MyWorld.right))
            walk(10,0);
    }
But sadly nothing changed. And yes, ofc I put Coa on white. Or maybe the color isn't white?
danpost danpost

2017/12/14

#
Maybe you did not understand what I was trying to say above. You need to change the offsets at which you check on the color. If you really want to check that it moves at all, change 'canMove()' to 'true' on line 11.
You need to login to post a reply.