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

2019/6/9

AUTOMOTIVE for the enemy

joelORTEGA joelORTEGA

2019/6/9

#
how can I make my enemy move on the x axis from position 250 to 444 without stopping, I hope you help me thanks. this is my code but it just moves to the right that I should do. public void act() { boolean T; T=moveLeft(); if(T==true){ moveLeft(); }else{ moveRight(); } if(moveRight()==false){ moveLeft(); } } public boolean moveLeft(){ int x,re; if(getX()<=444 && getX()>=251){ x=2; setLocation(getX()-x,getY()); return(true); } return(false); } public boolean moveRight(){ int x,re; if(getX()>=250 && getX()<=443){ x=6; setLocation(getX()+x,getY()); return(true); } return(false); } }
danpost danpost

2019/6/10

#
Try this:
private int dx = 6;

public void act()
{
    if ((dx > 0 && getX() >= 444) || (dx < 0 && getX() <= 250)) dx = 4-dx;
    setLocation(getX()+dx, getY());
}
joelORTEGA joelORTEGA

2019/6/15

#
thank you very much, that was the code I needed thanks bro
You need to login to post a reply.